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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
|
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class SpawnManager : MonoBehaviour
{
public static SpawnManager instance;
public Waypoint[] initialSpawns;
private HashSet<Waypoint> spawnPoints = new HashSet<Waypoint>();
public HashSet<IncomeGenerator> incomeGenerators = new HashSet<IncomeGenerator>();
public HashSet<University> universities = new HashSet<University>();
public HashSet<Mine> mines = new HashSet<Mine>();
public HashSet<House> houses = new HashSet<House>();
public HashSet<GameObject> destroyOnNewLevel = new HashSet<GameObject>();
public HashSet<GameObject> tileSpawnUis = new HashSet<GameObject>();
public HashSet<Enemy> currentEnemies = new HashSet<Enemy>();
public bool combat;
private float levelTime;
[SerializeField]
private Text levelText;
[SerializeField]
private Text scoreText;
[SerializeField]
private int cardDrawFrequency = 5;
[SerializeField]
private float baseRepairChance;
public int level;
public int lastLevel = 30;
[SerializeField]
private GameObject level1Enemy;
[SerializeField]
private GameObject level3Enemy;
[SerializeField]
private GameObject level5Enemy;
[SerializeField]
private GameObject level7Enemy;
[SerializeField]
private GameObject level9Enemy;
[SerializeField]
private GameObject level12Enemy;
[SerializeField]
private GameObject level16Enemy;
[SerializeField]
private GameObject level18Enemy;
[SerializeField]
private GameObject level20Enemy;
[SerializeField]
private GameObject level21Enemy;
[SerializeField]
private GameObject level23Enemy;
[SerializeField]
private GameObject level26Enemy;
[SerializeField]
private GameObject level28Enemy;
[SerializeField]
private GameObject level30Enemy;
[SerializeField]
private GameObject level32Enemy;
[SerializeField]
private GameObject level36Enemy;
[SerializeField]
private GameObject level38Enemy;
[SerializeField]
private GameObject level40Enemy;
[SerializeField]
private GameObject level15Boss;
[SerializeField]
private GameObject level25Boss;
[SerializeField]
private GameObject level35Boss;
[SerializeField]
private GameObject level45Boss;
[SerializeField]
private LightManager lightManager;
[SerializeField]
private int secondStageLightLevel;
[SerializeField]
private int thirdStageLightLevel;
[SerializeField]
private int fourthStageLightLevel;
private void Awake()
{
instance = this;
}
private void Start()
{
Waypoint[] array = initialSpawns;
foreach (Waypoint item in array)
{
spawnPoints.Add(item);
}
levelText.text = "Level: " + level;
scoreText.text = "Score: " + 0;
cardDrawFrequency = 3 - PlayerPrefs.GetInt("CardFreq1", 0) - PlayerPrefs.GetInt("CardFreq2", 0);
baseRepairChance = 34f * (float)(PlayerPrefs.GetInt("TowerRepair1", 0) + PlayerPrefs.GetInt("TowerRepair2", 0) + PlayerPrefs.GetInt("TowerRepair3", 0));
}
private void Update()
{
if (combat && currentEnemies.Count == 0 && !GameManager.instance.gameOver && levelTime > 2f)
{
EndWave();
}
else
{
levelTime += Time.deltaTime;
}
}
private void EndWave()
{
combat = false;
AchievementManager.instance.BeatLevel(level);
int @int = PlayerPrefs.GetInt("XP", 0);
PlayerPrefs.SetInt("XP", @int + level * GameManager.instance.gameMode);
scoreText.text = "Score: " + GameManager.instance.NaturalSum(level) * GameManager.instance.gameMode;
if (level >= lastLevel)
{
GameManager.instance.Victory();
return;
}
ShowSpawnUIs(state: true);
if (level % cardDrawFrequency == 0)
{
ShowSpawnUIs(state: false);
CardManager.instance.DrawCards();
}
if (level == 15 || level == 25 || level == 35)
{
MusicManager.instance.FadeOut(6f);
}
else
{
MusicManager.instance.SetIntensity(action: false);
}
GameObject[] array = new GameObject[destroyOnNewLevel.Count];
destroyOnNewLevel.CopyTo(array);
GameObject[] array2 = array;
foreach (GameObject gameObject in array2)
{
destroyOnNewLevel.Remove(gameObject);
Object.Destroy(gameObject);
}
if (tileSpawnUis.Count == 0)
{
Debug.Log("I GUESS ILL JUST DIE THEN");
AchievementManager.instance.UnlockAchievement("NoMorePaths");
StartNextWave();
}
}
public void StartNextWave()
{
levelTime = 0f;
UpdateSpawnPoints();
float num = 1f - (float)(GameManager.instance.health / GameManager.instance.maxHealth);
if (Random.Range(1f, 100f) <= baseRepairChance * num)
{
GameManager.instance.RepairTower(10);
}
level++;
levelText.text = "Level: " + level;
if (level == secondStageLightLevel)
{
lightManager.ChangeColor(2);
}
else if (level == thirdStageLightLevel)
{
lightManager.ChangeColor(3);
}
else if (level == fourthStageLightLevel)
{
lightManager.ChangeColor(4);
}
if (level == 15 || level == 25 || level == 35 || level == 45)
{
SpawnBoss(level);
}
if (level == 16)
{
MusicManager.instance.PlaySong(1, action: true);
}
else if (level == 26)
{
MusicManager.instance.PlaySong(2, action: true);
}
else if (level == 36)
{
MusicManager.instance.PlaySong(3, action: true);
}
else if (level > 1)
{
MusicManager.instance.SetIntensity(action: true);
}
StartCoroutine(Spawn(level * level));
combat = true;
ShowSpawnUIs(state: false);
foreach (House house in houses)
{
if (house != null)
{
house.CheckTowers();
}
}
foreach (IncomeGenerator incomeGenerator in incomeGenerators)
{
if (incomeGenerator != null)
{
incomeGenerator.GenerateIncome();
}
}
foreach (University university in universities)
{
if (university != null)
{
university.Research();
}
}
foreach (Mine mine in mines)
{
if (mine != null)
{
mine.Repair();
}
}
}
public void ShowSpawnUIs(bool state)
{
foreach (GameObject tileSpawnUi in tileSpawnUis)
{
tileSpawnUi.SetActive(state);
}
}
private void SpawnBoss(int lvl)
{
Waypoint spawnLocation = null;
float num = -1f;
foreach (Waypoint spawnPoint in spawnPoints)
{
if (spawnPoint.distanceFromEnd > num)
{
num = spawnPoint.distanceFromEnd;
spawnLocation = spawnPoint;
}
}
switch (lvl)
{
case 15:
SpawnEnemy(level15Boss, spawnLocation);
break;
case 25:
SpawnEnemy(level25Boss, spawnLocation);
break;
case 35:
SpawnEnemy(level35Boss, spawnLocation);
break;
case 45:
SpawnEnemy(level45Boss, spawnLocation);
break;
}
}
private IEnumerator Spawn(int num)
{
int safetyCount = level * level + 100;
int count = num;
float t = 0.5f / (float)spawnPoints.Count;
while (count > 0)
{
foreach (Waypoint spawnPoint in spawnPoints)
{
if (count <= 0)
{
break;
}
count -= SpawnSelection(spawnPoint);
yield return new WaitForSeconds(t);
}
safetyCount--;
if (safetyCount <= 0)
{
Debug.LogError("Spawn loop might be looping infinitely");
break;
}
}
}
private int SpawnSelection(Waypoint spawnPoint)
{
if (level >= 40 && Random.Range(1f, 100f) < 10f)
{
SpawnEnemy(level40Enemy, spawnPoint);
return 10;
}
if (level >= 38 && Random.Range(1f, 100f) < 10f)
{
SpawnEnemy(level38Enemy, spawnPoint);
return 10;
}
if (level >= 36 && Random.Range(1f, 100f) < 12f)
{
SpawnEnemy(level36Enemy, spawnPoint);
return 9;
}
if (level >= 32 && Random.Range(1f, 100f) < 10f)
{
SpawnEnemy(level32Enemy, spawnPoint);
return 10;
}
if (level >= 30 && Random.Range(1f, 100f) < 10f)
{
SpawnEnemy(level30Enemy, spawnPoint);
return 10;
}
if (level >= 28 && Random.Range(1f, 100f) < 11f)
{
SpawnEnemy(level28Enemy, spawnPoint);
return 10;
}
if (level >= 26 && Random.Range(1f, 100f) < 12f)
{
SpawnEnemy(level26Enemy, spawnPoint);
return 7;
}
if (level >= 23 && Random.Range(1f, 100f) < 9f)
{
SpawnEnemy(level23Enemy, spawnPoint);
return 12;
}
if (level >= 21 && Random.Range(1f, 100f) < 10f)
{
SpawnEnemy(level21Enemy, spawnPoint);
return 10;
}
if (level >= 20 && Random.Range(1f, 100f) < 10f)
{
SpawnEnemy(level20Enemy, spawnPoint);
return 10;
}
if (level >= 18 && Random.Range(1f, 100f) < 12f)
{
SpawnEnemy(level18Enemy, spawnPoint);
return 9;
}
if (level >= 16 && Random.Range(1f, 100f) < 13f)
{
SpawnEnemy(level16Enemy, spawnPoint);
return 8;
}
if (level >= 12 && Random.Range(1f, 100f) < 9f)
{
SpawnEnemy(level12Enemy, spawnPoint);
return 12;
}
if (level >= 9 && Random.Range(1f, 100f) < 12f)
{
SpawnEnemy(level9Enemy, spawnPoint);
return 9;
}
if (level >= 7 && Random.Range(1f, 100f) < 14f)
{
SpawnEnemy(level7Enemy, spawnPoint);
return 7;
}
if (level >= 5 && Random.Range(1f, 100f) < 20f)
{
SpawnEnemy(level5Enemy, spawnPoint);
return 5;
}
if (level >= 3 && Random.Range(1f, 100f) < 34f)
{
SpawnEnemy(level3Enemy, spawnPoint);
return 3;
}
SpawnEnemy(level1Enemy, spawnPoint);
return 1;
}
private void SpawnEnemy(GameObject unit, Waypoint spawnLocation)
{
Enemy component = Object.Instantiate(unit, spawnLocation.transform.position, Quaternion.identity).GetComponent<Enemy>();
component.SetStats();
component.SetFirstSpawnPoint(spawnLocation);
currentEnemies.Add(component);
}
private void UpdateSpawnPoints()
{
List<Waypoint> list = new List<Waypoint>();
List<Waypoint> list2 = new List<Waypoint>();
foreach (Waypoint spawnPoint in spawnPoints)
{
if (!CheckSpawnPoint(spawnPoint))
{
continue;
}
list.Add(spawnPoint);
foreach (Waypoint newSpawnPoint in GetNewSpawnPoints(spawnPoint, 1))
{
list2.Add(newSpawnPoint);
}
}
foreach (Waypoint item in list)
{
spawnPoints.Remove(item);
}
foreach (Waypoint item2 in list2)
{
spawnPoints.Add(item2);
}
}
private bool CheckSpawnPoint(Waypoint point)
{
if (point.GetPreviousWaypoints().Length != 0)
{
return true;
}
return false;
}
private List<Waypoint> GetNewSpawnPoints(Waypoint start, int count)
{
if (count > 100)
{
Debug.LogError("Possible infinite loop while finding new spawn points (count over 100)");
return null;
}
Waypoint[] previousWaypoints = start.GetPreviousWaypoints();
List<Waypoint> list = new List<Waypoint>();
if (previousWaypoints.Length == 0)
{
list.Add(start);
return list;
}
count++;
Waypoint[] array = previousWaypoints;
foreach (Waypoint start2 in array)
{
foreach (Waypoint newSpawnPoint in GetNewSpawnPoints(start2, count))
{
list.Add(newSpawnPoint);
}
}
return list;
}
}
|