aboutsummaryrefslogtreecommitdiff
path: root/td/Assets/Scripts/EnemySpawner.cs
blob: ee0d3d6fac2a6866c987bef38d218c6772220092 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class EnemySpawner : MonoBehaviour {
	/* This is a class that spawns an enemy with a random interval
	 * it is not very good, but is what it needs to be for testing purposes */
	// TODO Add wave system with increasing difficulty

	public Enemy enemyPrefab;
	public Transform pathWay;
	[Header("Scripting vars")]
	public player player;            // Reference to the player object, should be set when instantiating

	private Transform parentObject;

	List<Vector3> waypoints = new List<Vector3>();
	int next = 1;
	int n = 0;

	void Awake() {
		foreach (Transform child in pathWay) {
			waypoints.Add (child.position);
		}
	}

	void Start() {
		parentObject = transform.Find ("enemies").gameObject.GetComponent <Transform> ();
	}

	void Update () {
		n++;

		if (n == next) {
			n = 0;
			next = (int)Random.Range (50, 400);

			Enemy newEnemy = Instantiate (enemyPrefab, new Vector3(0, 0, 0), Quaternion.identity, parentObject);
			Enemy script = newEnemy.GetComponent <Enemy> ();
			Transform transform = newEnemy.GetComponent <Transform>();

			script.waypoints = waypoints;
			script.speed = Random.Range (0.3f, 1.2f);
			script.player = player;
			transform.position = new Vector3 (0.93f, 0.483f, 0f);
		}

	}
}