summaryrefslogtreecommitdiff
path: root/Other/AstarPathfindingDemo/Packages/com.arongranberg.astar/ExampleScenes/Scenes/OldExamples/Example18_RTS/RTSWaveSpawner.cs
blob: a9b80efa04f347ed81411b7a5fc7647b414de1c1 (plain)
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
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
using System.Linq;

namespace Pathfinding.Examples.RTS {
	[HelpURL("https://arongranberg.com/astar/documentation/stable/rtswavespawner.html")]
	public class RTSWaveSpawner : MonoBehaviour {
		public Wave[] waves;

		public Transform target;
		public Text waveCounter;
		public int team = 2;

		[System.Serializable]
		public class Wave {
			public GameObject prefab;
			public Transform spawnPoint;
			public float delay;
			public int count;
			public int health;
		}

		// Use this for initialization
		IEnumerator Start () {
			float lastWave = 0;
			float multiplier = 1;

			for (int it = 0;; it++) {
				for (int i = 0; i < waves.Length; i++) {
					while (true) {
						float remaining = waves[i].delay - (Time.time - lastWave);
						if (remaining <= 0) break;
						waveCounter.text = Mathf.RoundToInt(remaining).ToString();
						yield return null;
					}
					waveCounter.text = "!";

					int from = i;
					while (i + 1 < waves.Length && waves[i+1].delay == 0) i++;

					var toSpawn = waves.Skip(from).Take(i - from + 1).ToArray();
					var names = toSpawn.Select(w => w.spawnPoint.gameObject.name).Distinct().OrderBy(s => s).ToArray();
					var message = "Incoming enemies from ";
					for (int j = 0; j < names.Length; j++) {
						if (j > 0) message += j == names.Length - 1 ? " and " : ", ";
						message += names[j];
					}
					Debug.Log(message);

					foreach (var wave in toSpawn) {
						for (int j = 0; j < wave.count; j++) {
							var rnd = Random.insideUnitSphere * 10;
							rnd.y = 0;
							var go = GameObject.Instantiate(wave.prefab, wave.spawnPoint.position + rnd, wave.spawnPoint.rotation) as GameObject;
							var unit = go.GetComponent<RTSUnit>();
							unit.team = team;
							unit.maxHealth = unit.health = wave.health * multiplier;
							unit.SetDestination(target.position, MovementMode.AttackMove);
						}
					}

					yield return new WaitForSeconds(10);
					lastWave = Time.time;
				}

				multiplier *= 2;
			}
		}

		// Update is called once per frame
		void Update () {
		}
	}
}