aboutsummaryrefslogtreecommitdiff
path: root/td/Assets/Scripts/waveSpawner.cs
blob: f79fb9089723b5e83eb13e9be810891387b86dc2 (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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class WaveSpawner : MonoBehaviour {

	public static int EnemiesAlive = 0;

	public Wave[] Waves;

	public Transform SpawnPoint;

	public float TimeBetweenWaves = 5f;
	private float _countdown = 2f;

	public Text WaveCountdownText;


	private int _waveIndex = 0;

	void Update ()
	{
		if (EnemiesAlive > 0)
		{
			return;
		}

		if (_waveIndex == Waves.Length)
		{
			// WIN LEVEL!!!
			this.enabled = false;
		}

		if (_countdown <= 0f)
		{
			StartCoroutine(SpawnWave());
			_countdown = TimeBetweenWaves;
			return;
		}

		_countdown -= Time.deltaTime;

		_countdown = Mathf.Clamp(_countdown, 0f, Mathf.Infinity);

		//waveCountdownText.text = string.Format("{0:00.00}", countdown);
	}

	IEnumerator SpawnWave ()
	{
		Wave wave = Waves[_waveIndex];

		EnemiesAlive = wave.Count;

		for (int i = 0; i < wave.Count; i++)
		{
			SpawnEnemy(wave.Enemy);
			yield return new WaitForSeconds(1f / wave.Rate);
		}

		_waveIndex++;
	}

	void SpawnEnemy (GameObject enemy)
	{
		Instantiate(enemy, SpawnPoint.position, SpawnPoint.rotation);
	}

}

[System.Serializable]
public class Wave {
	public GameObject Enemy;
	public int Count;
	public float Rate;
}