-
Notifications
You must be signed in to change notification settings - Fork 0
/
SCR_RoomManager.cs
542 lines (444 loc) · 17.9 KB
/
SCR_RoomManager.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
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
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Playables;
using Random = UnityEngine.Random;
public class SCR_RoomManager : MonoBehaviour,IRoom
{
private SCR_RoomInformation roomInformation;
private List<GameObject> enemyObjects = new List<GameObject>();
private GameObject player1, player2;
private List<GameObject> initialNodes = new List<GameObject>();
private List<GameObject> spawnNodes = new List<GameObject>();
[Header("Types of enemy room configuration")]
[SerializeField]
private List<SCR_RoomConfiguration> roomPossibilitiesList;
List<GameObject> grunts = new List<GameObject>();
private SCR_RoomConfiguration selectedConfiguration;
[Header("Parent of every initial placement node")]
[SerializeField]
private GameObject initialNodeParent;
[Header("Parent of every spawn placement node")]
[SerializeField]
private GameObject spawnNodeParent;
[Header("List containing each room exit")]
[SerializeField]
List<GameObject> enterTriggers = new List<GameObject>();
[Header("Speed in which gates will close")]
[SerializeField]
private float gateTimer = 1.0f;
private bool levelStarted;
private int numberOfAgentsTargetingPlayer1 = 0, numberOfAgentsTargetingPlayer2 =0, PotentialNumberOfAgentsAttackingPlayer1 = 0;
private GameObject buffEnemy;
public bool CheckIsPlayerTargetable(bool mIsPlayer1)
{
bool possible = false;
if (mIsPlayer1)
{
if (numberOfAgentsTargetingPlayer1 < PotentialNumberOfAgentsAttackingPlayer1)
{
possible = true;
}
}
else
{
if (numberOfAgentsTargetingPlayer2 < (enemyObjects.Count -PotentialNumberOfAgentsAttackingPlayer1))
{
possible = true;
}
}
return possible;
}
public void updatePlayer1AgentValue(bool mEnemyDead)
{
switch (mEnemyDead)
{
case true: numberOfAgentsTargetingPlayer1--;break;
case false: numberOfAgentsTargetingPlayer1++; break;
}
}
public void updatePlayer2AgentValue(bool mEnemyDead)
{
switch (mEnemyDead)
{
case true: numberOfAgentsTargetingPlayer2--; break;
case false: numberOfAgentsTargetingPlayer2++; break;
}
}
void Awake()
{
roomInformation = Resources.Load<SCR_RoomInformation>("Room Information");
disableVisualComponentNodes();
if (blimpSpotlightLight != null)
{
blimpSpotlightLight.gameObject.SetActive(false);
}
}
public void SpawnRoom()
{
player1 = GameObject.FindGameObjectWithTag("Player1");
player2 = GameObject.FindGameObjectWithTag("Player2");
SpawnLevel();
PotentialNumberOfAgentsAttackingPlayer1 = enemyObjects.Count / 2;
}
void disableVisualComponentNodes()
{
if (initialNodeParent != null)
{
for (int i = 0; i < initialNodeParent.transform.childCount; i++)
{
GameObject current;
current = initialNodeParent.transform.GetChild(i).gameObject;
initialNodes.Add(current);
}
}
if (spawnNodeParent != null)
{
for (int i = 0; i < spawnNodeParent.transform.childCount; i++)
{
GameObject current;
current = spawnNodeParent.transform.GetChild(i).gameObject;
spawnNodes.Add(current);
}
}
for (int i = 0; i < initialNodes.Count; i++)
{
initialNodes[i].GetComponent<MeshRenderer>().enabled = false;
}
/*for (int i = 0; i < spawnNodes.Count; i++)
{
//spawnNodes[i].GetComponent<MeshRenderer>().enabled = false;
}*/
}
///Will spawn a random configuration into the current level based upon the created nodes
void SpawnLevel()
{
selectedConfiguration = roomPossibilitiesList[Random.Range(0, roomPossibilitiesList.Count)];
if (initialNodeParent != null)
{
for (int i = 0; i < selectedConfiguration.initialEnemyCount; i++)
{
STR_InitialSpawnID currentSpawnID = selectedConfiguration.ReturnEnemy(i);
EnemyType currentType = currentSpawnID.type;
GameObject enemyToSpawn = returnEnemyPrefab(currentType);
Vector3 spawnPos = initialNodes[currentSpawnID.node].transform.position;
spawnPos.y = spawnPos.y + (enemyToSpawn.transform.lossyScale.y / 2);
GameObject spawn = Instantiate(enemyToSpawn, spawnPos, Quaternion.identity);
spawn.transform.parent = transform;
if (currentType != EnemyType.Buff)
{
enemyObjects.Add(spawn);
}
else
{
buffEnemy = spawn;
}
IEnemy currentEnemyInterface = (IEnemy)spawn.GetComponent(typeof(IEnemy));
currentEnemyInterface.SetRoomManager(this.gameObject);
}
}
for (int i = 0; i < enterTriggers.Count; i++)
{
if (enterTriggers[i] != null)
{
enterTriggers[i].GetComponent<IFightRoomTrigger>().setRoomManager(this.gameObject);
}
}
}
//Will assign each grunt enemy within the scene a movement type based upon their distance to the player objects
void AssignTypes()
{
grunts= new List<GameObject>();
for (int i = 0; i < enemyObjects.Count; i++)
{
IEnemy currentEnemyInterface = (IEnemy)enemyObjects[i].GetComponent(typeof(IEnemy));
EnemyType currentEnemyType = currentEnemyInterface.ReturnEnemyType();
if (currentEnemyType == EnemyType.Grunt)
{
grunts.Add(enemyObjects[i]);
}
}
int sortedGrunts = 0;
while (grunts.Count > sortedGrunts)
{
float distance = 100000.0f;
int selectedGrunt = 0;
for (int i = 0; i < grunts.Count; i++)
{
Vector3 pos = grunts[i].transform.position;
float enemyDistance = 10000.0f;
if (player1 != null && player1.activeInHierarchy)
{
enemyDistance = Vector3.Distance(pos, player1.transform.position);
}
if (player2 != null && player2.activeInHierarchy)
{
if (Vector3.Distance(pos, player2.transform.position) < enemyDistance)
{
enemyDistance = Vector3.Distance(pos, player2.transform.position);
}
}
if (enemyDistance < distance)
{
distance = enemyDistance;
selectedGrunt = i;
}
}
if (sortedGrunts < roomInformation.waves.maxmimumSpeed-1)
{
SCR_GruntSpiderAI gruntClass = grunts[selectedGrunt].GetComponent<SCR_GruntSpiderAI>();
STR_GruntTypes maxSpeedType = roomInformation.gruntSpeedTypes[0];
gruntClass.SetGrunt(maxSpeedType.speed, maxSpeedType.enemyType, maxSpeedType.priority);
}
else if (sortedGrunts >= (roomInformation.waves.maxmimumSpeed-1) && sortedGrunts < ((roomInformation.waves.maxmimumSpeed-1) + roomInformation.waves.middleSpeed))
{
SCR_GruntSpiderAI gruntClass = grunts[selectedGrunt].GetComponent<SCR_GruntSpiderAI>();
STR_GruntTypes maxSpeedType = roomInformation.gruntSpeedTypes[1];
gruntClass.SetGrunt(maxSpeedType.speed, maxSpeedType.enemyType, maxSpeedType.priority);
}
else
{
SCR_GruntSpiderAI gruntClass = grunts[selectedGrunt].GetComponent<SCR_GruntSpiderAI>();
STR_GruntTypes maxSpeedType = roomInformation.gruntSpeedTypes[2];
gruntClass.SetGrunt(maxSpeedType.speed, maxSpeedType.enemyType, maxSpeedType.priority);
}
grunts.RemoveAt(selectedGrunt);
sortedGrunts++;
}
}
//returns a gameObject prefab based on a enemy type, allowing for the correct enemy to be spawned
GameObject returnEnemyPrefab(EnemyType currentType)
{
GameObject enemyPrefab = roomInformation.typesOfEnemies[0];
for (int j = 0; j < roomInformation.typesOfEnemies.Count; j++)
{
IEnemy currentEnemyInterface = (IEnemy)roomInformation.typesOfEnemies[j].GetComponent(typeof(IEnemy));
EnemyType currentEnemyType = currentEnemyInterface.ReturnEnemyType();
if (currentEnemyType == currentType)
{
enemyPrefab = roomInformation.typesOfEnemies[j];
break;
}
}
return enemyPrefab;
}
//function which will begin the level, blocking the players within the current room until it is completed
public void BeginLevel()
{
if (!levelStarted)
{
SCR_Boss.instance.BossAttack(false);
SCR_AudioManager.instance.Play("DoorClose");
SCR_AudioManager.instance.PlayFightMusic();
SCR_GameManager.instance.PlayerFighting = true;
AssignTargets();
AssignTypes();
StartCoroutine(playLevel());
MoveWalls();
levelStarted = true;
}
}
//move the walls of the room based upon whether the level has been completed
void MoveWalls()
{
for (int i = 0; i < enterTriggers.Count; i++)
{
IFightRoomTrigger current = enterTriggers[i].GetComponent<IFightRoomTrigger>();
current.ClosePrimary(levelStarted, gateTimer);
if (!levelStarted)
{
current.CloseSecondary(true, gateTimer);
}
}
}
public void CloseWalls()
{
for (int i = 0; i < enterTriggers.Count; i++)
{
if (enterTriggers[i] != null)
{
IFightRoomTrigger current = enterTriggers[i].GetComponent<IFightRoomTrigger>();
current.ClosePrimary(false, 0.0f);
current.CloseSecondary(false, 0.0f);
}
}
}
public void OpenWalls()
{
for (int i = 0; i < enterTriggers.Count; i++)
{
if (enterTriggers[i] != null)
{
IFightRoomTrigger current = enterTriggers[i].GetComponent<IFightRoomTrigger>();
current.ClosePrimary(true, 0.0f);
current.CloseSecondary(false, 0.0f);
}
}
}
//Set the intially spawned enemies to attack a target
void AssignTargets()
{
for (int i = 0; i < enemyObjects.Count; i++)
{
IEnemy currentEnemyInterface = (IEnemy)enemyObjects[i].GetComponent(typeof(IEnemy));
currentEnemyInterface.StartAI();
}
if (buffEnemy != null)
{
IEnemy currentEnemyInterface = (IEnemy)buffEnemy.GetComponent(typeof(IEnemy));
currentEnemyInterface.StartAI();
}
}
//function which will remove an enemy from the current list whenever they are killed
public void RemoveEnemy(GameObject mDeadEnemy)
{
SCR_GameManager.instance.EnemyKilled();
enemyObjects.Remove(mDeadEnemy);
}
public void DebuffEnemies()
{
for (int i = 0; i < enemyObjects.Count; i++)
{
IEnemy currentEnemyInterface = (IEnemy)enemyObjects[i].GetComponent(typeof(IEnemy));
currentEnemyInterface.BuffEnemy(BuffTypes.None);
}
}
public void BuffEnemies(Vector3 mWaveOrigin, float mScanDistance)
{
BuffTypes currentType = buffEnemy.GetComponent<SCR_BuffSpider>().returnBuffType();
for (int i = 0; i < enemyObjects.Count; i++)
{
float distanceFromWaveOrigin = Vector3.Distance(mWaveOrigin, enemyObjects[i].transform.position);
if (distanceFromWaveOrigin <= mScanDistance)
{
IEnemy currentEnemyInterface = (IEnemy)enemyObjects[i].GetComponent(typeof(IEnemy));
currentEnemyInterface.BuffEnemy(currentType);
}
}
}
[Header("Blimp Spotlight")]
[SerializeField] PlayableDirector blimpSpotlightPlayableDirector;
[SerializeField] Light blimpSpotlightLight;
[SerializeField] float blimpSpotlightFadeOutTime = 2;
//co routine which will play out the level, spawning in the pooled enemies
IEnumerator playLevel()
{
// Play the blimp spotlight animation if it exists
if (blimpSpotlightPlayableDirector != null)
{
blimpSpotlightLight.gameObject.SetActive(true);
blimpSpotlightPlayableDirector.Play();
}
while (selectedConfiguration.poolSize > 0 || enemyObjects.Count > 0)
{
if (SCR_Boss.instance.bossAttacking)
{
SCR_Boss.instance.BossAttack(false);
}
if (enemyObjects.Count < selectedConfiguration.maxSpawnSize && selectedConfiguration.poolSize > 0)
{
int rng = Random.Range(0, selectedConfiguration.poolSize);
bool pointChosen = false;
int nodeSpawn = 0;
GameObject currentNode = this.gameObject;
while (!pointChosen)
{
nodeSpawn = Random.Range(0, spawnNodes.Count);
currentNode = spawnNodes[nodeSpawn];
if (!currentNode.GetComponent<SCR_SpiderPortal>().currentlySpawning)
{
pointChosen = true;
}
yield return null;
}
currentNode.GetComponent<SCR_SpiderPortal>().OpenPortal(0.5f);
GameObject spawn = Instantiate(returnEnemyPrefab(selectedConfiguration.ReturnPoolsType(rng)), currentNode.transform.position + (Vector3.down * 3), Quaternion.identity);
spawn.transform.parent = transform;
enemyObjects.Add(spawn);
AssignTypes();
IEnemy currentEnemyInterface = (IEnemy)spawn.GetComponent(typeof(IEnemy));
currentEnemyInterface.SetRoomManager(this.gameObject);
if (buffEnemy != null)
{
currentEnemyInterface.BuffEnemy(buffEnemy.GetComponent<SCR_BuffSpider>().returnBuffType());
}
float timer = Random.Range(2.5f, 4.0f);
StartCoroutine(currentEnemyInterface.spawnEnemy(currentNode, timer));
}
yield return null;
}
SCR_AudioManager.instance.Play("DoorOpen");
for (int i = 0; i < enterTriggers.Count; i++)
{
IFightRoomTrigger current = enterTriggers[i].GetComponent<IFightRoomTrigger>();
current.ClosePrimary(true, gateTimer);
}
if (buffEnemy != null)
{
buffEnemy.GetComponent<SCR_BuffSpider>().PowerDown();
}
SCR_AudioManager.instance.PlayWalkMusic();
SCR_GameManager.instance.PlayerFighting = false;
if (player1 & player2)
{
SCR_PlayerWorldSpaceUI player1WorldSpaceUIScr = player1.GetComponent<SCR_PlayerWorldSpaceUI>();
SCR_PlayerWorldSpaceUI player2WorldSpaceUIScr = player2.GetComponent<SCR_PlayerWorldSpaceUI>();
if (player1WorldSpaceUIScr)
{
player1WorldSpaceUIScr.HealPrompt(true);
}
if (player2WorldSpaceUIScr)
{
player2WorldSpaceUIScr.HealPrompt(true);
}
yield return new WaitForSeconds(4.0f);
if (player1WorldSpaceUIScr)
{
player1WorldSpaceUIScr.HealPrompt(false);
}
if (player2WorldSpaceUIScr)
{
player2WorldSpaceUIScr.HealPrompt(false);
}
}
// Fade out the blimp light
if (blimpSpotlightLight != null)
StartCoroutine(FadeOutBlimpSpotlight());
}
IEnumerator FadeOutBlimpSpotlight()
{
float time = 0;
float startIntensity = blimpSpotlightLight.intensity;
WaitForEndOfFrame wait = new WaitForEndOfFrame();
while (time < blimpSpotlightFadeOutTime)
{
time += Time.deltaTime;
blimpSpotlightLight.intensity = Mathf.SmoothStep(startIntensity, 0, time / blimpSpotlightFadeOutTime);
yield return wait;
}
blimpSpotlightPlayableDirector.Stop();
}
public void AddEnemy(GameObject mEnemy)
{
enemyObjects.Add(mEnemy);
IEnemy currentEnemyInterface = (IEnemy)mEnemy.GetComponent(typeof(IEnemy));
currentEnemyInterface.SetRoomManager(this.gameObject);
currentEnemyInterface.StartAI();
}
}
[System.Serializable]
//struct which stores the types of grunts possible
public struct STR_GruntTypes
{
public float speed;
public GruntTypes enemyType;
public int priority;
}
[System.Serializable]
//struct which stores how many of each type of grunt can exist
public struct STR_GruntWaves
{
public int maxmimumSpeed;
public int middleSpeed;
}