aboutsummaryrefslogtreecommitdiff
path: root/td/Assets/Scripts/EnemySpawner.cs
blob: 5b0249df4ffaa21f90d7eb4d56b578c579323272 (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
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 in designer

	private Transform _parentObject;

	private List<Vector3> _waypoints = new List<Vector3>();
	private int _next = 1;
	private 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);
		}

	}
}