-
Notifications
You must be signed in to change notification settings - Fork 2
/
EnemySpawnHandler.cs
242 lines (211 loc) · 7.48 KB
/
EnemySpawnHandler.cs
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class EnemySpawnHandler : MonoBehaviour
{
private enum Options {Endless, Waves}
[Header("Settings")]
[SerializeField] private Options _Option = Options.Endless;
[SerializeField] private int _Seed = 0;
[SerializeField] private bool _SetRandomSeed = true;
[Header("Object Pool")]
[SerializeField] private ObjectPool _ObjectPool = null;
[Header("Enemies")]
[SerializeField] private EnemySpawnHandler_Enemy[] _Enemies = null;
[Header("SpawnLocations")]
[SerializeField] private Transform[] _SpawnLocations = null;
[Header("Settings - Endless")]
[SerializeField] private float _SpawnRate = 5; // Seconds between spawning
[SerializeField] private float _SpawnRateEncrease = 0.05f; // Decrease time between spawning per sec
[SerializeField] private bool _RandomEnemy = true;
[SerializeField] private bool _RandomSpawn = true;
[Header("Settings - Waves")]
[SerializeField] private EnemySpawnHandler_WaveSettings _Waves = null;
[SerializeField] private bool _WaitForAllEnemiesKilled = true;
private float _Timer = 0;
private int _CurrentWave = 0;
private int _CheckWave = 999;
private float _TimerBetweenWaves = 0;
private float _SpawnSpeed = 0;
private int _EnemiesAlive = 0;
private void Start()
{
if (_SetRandomSeed)
Random.InitState(Random.Range(0, 10000));
else
Random.InitState(_Seed);
if (_Waves.WaveOption == EnemySpawnHandler_WaveSettings.WaveOptions.Generate)
GenerateWaves();
if (_Waves.WaveOption == EnemySpawnHandler_WaveSettings.WaveOptions.Endless)
{
_Waves.WaveAmount = 1;
GenerateWaves();
GenerateWaves(1);
}
}
void Update()
{
_Timer += 1 * Time.deltaTime;
switch (_Option)
{
case Options.Endless:
Update_Endless();
break;
case Options.Waves:
Update_Waves();
break;
}
}
//Update
private void Update_Endless()
{
if (_Timer >= _SpawnRate)
{
int randomenemyid = 0;
int randomspawnid = 0;
if (_RandomEnemy)
randomenemyid = Random.Range(0, _Enemies.Length);
if (_RandomSpawn)
randomspawnid = Random.Range(0, _SpawnLocations.Length);
Spawn(randomenemyid, randomspawnid);
_Timer = 0;
}
_SpawnRate -= _SpawnRateEncrease * Time.deltaTime;
}
private void Update_Waves()
{
if (_CurrentWave < _Waves.Waves.Count)
{
if (_CheckWave != _CurrentWave)
{
if (_WaitForAllEnemiesKilled)
{
EnemiesAlive();
if (_EnemiesAlive == 0)
_TimerBetweenWaves += 1 * Time.deltaTime;
}
else
_TimerBetweenWaves += 1 * Time.deltaTime;
if (_TimerBetweenWaves >= _Waves.TimeBetweenWaves)
{
_TimerBetweenWaves = 0;
_CheckWave = _CurrentWave;
_SpawnSpeed = _Waves.Waves[_CurrentWave].SpawnDuration / _Waves.Waves[_CurrentWave].TotalEnemies;
if (_Waves.WaveOption == EnemySpawnHandler_WaveSettings.WaveOptions.Endless)
GenerateWaves(_CurrentWave+2);
}
}
else
{
//Spawn
if (_Waves.Waves[_CurrentWave].TotalEnemies > 0)
{
if (_Timer > _SpawnSpeed)
{
bool spawncheck = false;
while (!spawncheck)
{
int spawnid = Random.Range(0, _Enemies.Length);
if (_Waves.Waves[_CurrentWave].EnemyID[spawnid] > 0)
{
Spawn(spawnid, Random.Range(0, _SpawnLocations.Length));
_Waves.Waves[_CheckWave].EnemyID[spawnid]--;
_Waves.Waves[_CurrentWave].TotalEnemies--;
spawncheck = true;
}
}
_Timer = 0;
}
}
else
{
_CurrentWave++;
}
}
}
}
//Generate Waves
private void GenerateWaves(int waveid = 0)
{
int enemytypes = _Enemies.Length;
for (int i = 0; i < _Waves.WaveAmount; i++)
{
EnemySpawnHandler_Wave newwave = new EnemySpawnHandler_Wave();
int enemyamount = 0;
if (waveid == 0)
enemyamount = Mathf.RoundToInt(_Waves.EnemyAmount * ((_Waves.EnemyIncreaseAmount * i) + 1));
else
enemyamount = Mathf.RoundToInt(_Waves.EnemyAmount * ((_Waves.EnemyIncreaseAmount * waveid) + 1));
//Set enemy amount
newwave.EnemyID = new int[enemytypes];
int checkenemyamount = 0;
newwave.TotalEnemies = enemyamount;
while (checkenemyamount < enemyamount)
{
for (int j = 0; j < enemytypes; j++)
{
if (_Enemies[j].StartWave <= i)
{
int addamount = 0;
if (enemyamount < 2)
addamount = Random.Range(0, enemyamount);
else
addamount = Random.Range(0, Mathf.RoundToInt(enemyamount*0.5f));
if (enemyamount > checkenemyamount + addamount)
{
newwave.EnemyID[j] += addamount;
checkenemyamount += addamount;
}
else
{
newwave.EnemyID[j] += enemyamount - checkenemyamount;
checkenemyamount = enemyamount;
continue;
}
}
}
}
_Waves.Waves.Add(newwave);
}
}
public void Spawn(int enemyid, int spawnid)
{
GameObject obj = _ObjectPool.GetObjectPrefabName(_Enemies[enemyid].EnemyPrefab.name, false);
obj.transform.position = _SpawnLocations[spawnid].position;
obj.SetActive(true);
}
private void EnemiesAlive()
{
_EnemiesAlive = GameObject.FindGameObjectsWithTag("Enemy").Length;
}
}
[System.Serializable]
public class EnemySpawnHandler_Enemy
{
public string EnemyName;
public GameObject EnemyPrefab;
[Header("Settings")]
public int StartWave;
}
[System.Serializable]
public class EnemySpawnHandler_WaveSettings
{
public enum WaveOptions {Endless, Manually, Generate}
public WaveOptions WaveOption;
[Header("Endless")]
public float EnemyIncreaseAmount;
[Header("Manual")]
public List<EnemySpawnHandler_Wave> Waves;
[Header("Generate")]
public int WaveAmount;
public int EnemyAmount;
[Header("Other")]
public float TimeBetweenWaves;
}
[System.Serializable]
public class EnemySpawnHandler_Wave
{
public int[] EnemyID;
public float SpawnDuration = 5;
[HideInInspector] public int TotalEnemies;
}