-
Notifications
You must be signed in to change notification settings - Fork 0
/
Hivemind_GameState.cpp
398 lines (337 loc) · 9.88 KB
/
Hivemind_GameState.cpp
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
// Fill out your copyright notice in the Description page of Project Settings.
#include "Hivemind_GameState.h"
//Added
#include "GameFramework/PlayerState.h"
#include "HivemindGameModeBase.h"
#include "Kismet/GameplayStatics.h"
#include "../Characters/Aliens/Character/AlienCharacter.h"
#include "../Components/HealthComponent.h"
#include "../Characters/Aliens/Link_System/AlienPowerSource.h"
#include "Kismet/GameplayStatics.h"
#include "Sound/SoundCue.h"
#include "Components/SkeletalMeshComponent.h"
AHivemind_GameState::AHivemind_GameState()
{
//Initialise
CurrentAmountTime = 9'999.f;
CurrentAmountCollectedParts = 0;
CurrentState = FGameStates::Start;
CurrentAmountRespawnsLeft = 0;
//Disable damage
bCanBeDamaged = false;
//Disable tick
PrimaryActorTick.bStartWithTickEnabled = false;
PrimaryActorTick.bCanEverTick = false;
//Audio
MusicFadeLevel = 0.f;
}
void AHivemind_GameState::HandleBeginPlay()
{
//Get players
{
//Get player states from which we will get players
TArray<APlayerState*> PlayerStates = PlayerArray;
//Go over collected states
for (APlayerState* const State : PlayerStates)
{
if (State)
{
//If the pawn is valid, add it to our saved array
APawn* const Pawn = State->GetPawn();
if (Pawn)
{
AAlienCharacter* const Alien = Cast<AAlienCharacter>(Pawn);
AlienArray.AddUnique(Alien);
}
}
}
}
//Set player colours
for (int32 i{}; i < AlienArray.Num(); ++i)
{
AlienArray[i]->GetMesh()->SetCustomDepthStencilValue(i + 1);
switch (i)
{
case 0:
AlienArray[i]->SetPlayerColour(FColor::Red);
break;
case 1:
AlienArray[i]->SetPlayerColour(FColor::Green);
break;
case 2:
AlienArray[i]->SetPlayerColour(FColor::Blue);
break;
case 3:
AlienArray[i]->SetPlayerColour(FColor::Purple);
break;
default:
AlienArray[i]->SetPlayerColour(FColor::MakeRandomColor());
break;
}
}
//Call super after getting players to make sure everything is valid
Super::HandleBeginPlay();
//Set state to begin
SetCurrentState(FGameStates::Start);
//Log
UE_LOG(LogTemp, Warning, L"HandleBeginPlay called");
}
#pragma region GettersSetters
FGameStates AHivemind_GameState::GetCurrentState() const
{
return CurrentState;
}
void AHivemind_GameState::SetCurrentState(FGameStates NewState)
{
//Remember to set new state
CurrentState = NewState;
//Ask the gamemode to handle the change of states
AHivemindGameModeBase* const GameMode = Cast<AHivemindGameModeBase>(AuthorityGameMode);
if (GameMode)
{
GameMode->HandleStateChange(NewState);
}
}
float AHivemind_GameState::GetCurrentAmountTime() const
{
return CurrentAmountTime;
}
void AHivemind_GameState::SetCurrentAmountTime(float Value)
{
CurrentAmountTime = Value;
}
float AHivemind_GameState::IncreaseCurrentTime(float Value)
{
//Add to time and return after incremented amount
CurrentAmountTime += Value;
return CurrentAmountTime;
}
int AHivemind_GameState::GetCurrentAmountCollectedParts() const
{
return CurrentAmountCollectedParts;
}
void AHivemind_GameState::SetCurrentAmountCollectedParts(int Amount)
{
CurrentAmountCollectedParts = Amount;
//Since the amount of parts was updated, check if it would be equal/superior to required amount
CheckCurrentAmountCollectedParts();
}
int AHivemind_GameState::IncreaseCurrentAmountCollectedParts(int Amount)
{
//Increase amount of collected parts
CurrentAmountCollectedParts += Amount;
//Since the amount of parts was updated, check if it would be equal/superior to required amount
CheckCurrentAmountCollectedParts();
//Return new amount
return CurrentAmountCollectedParts;
}
const TArray<AAlienCharacter*>& AHivemind_GameState::GetAlienArray() const
{
return AlienArray;
}
int AHivemind_GameState::GetAlienAliveCount() const
{
//Retrive amount of aliens that are not dead, but can be downed
int Count = 0;
for (AAlienCharacter* const Char : AlienArray)
{
if (Char)
{
UHealthComponent* const Health = Cast<UHealthComponent>(Char->GetComponentByClass(UHealthComponent::StaticClass()));
if (Health)
{
if (!Health->IsDead())
{
++Count;
}
}
}
}
return Count;
}
int AHivemind_GameState::GetAlienUpCount() const
{
//Retrive amount of aliens that are still running around, not dead or downed
int Count = 0;
for (AAlienCharacter* const Char : AlienArray)
{
if (Char)
{
UHealthComponent* const Health = Cast<UHealthComponent>(Char->GetComponentByClass(UHealthComponent::StaticClass()));
if (Health)
{
if ( !(Health->IsDead() || Health->IsDowned()) )
{
++Count;
}
}
}
}
return Count;
}
void AHivemind_GameState::NotifyAlienDowned(AAlienCharacter* const Char)
{
//Can only notify when playing, doesn't matter otherwise
if (CurrentState == FGameStates::Play)
{
if(GetAlienUpCount() == 0)
{
SetCurrentState(FGameStates::Loss);
}
}
}
void AHivemind_GameState::NotifyAlienDeath(AAlienCharacter* const Char)
{
//Can only notify when playing, doesn't matter otherwise
if (CurrentState == FGameStates::Play)
{
//Remove time
const AHivemindGameModeBase* const Mode = GetDefaultGameMode<AHivemindGameModeBase>();
if (Mode)
{
IncreaseCurrentTime(-Mode->GetDeathTimePenalty());
OnTimeLostDelegate.Broadcast(-Mode->GetDeathTimePenalty());
}
//Respawn feature down below, will need to be tested if it should stay in or not
if (CurrentAmountRespawnsLeft > 0)
{
if (Char)
{
Char->Respawn();
}
CurrentAmountRespawnsLeft--;
//Notify delegates
OnRespawnUseDelegate.Broadcast();
}
//If no players left, game over
if (GetAlienAliveCount() == 0)
{
SetCurrentState(FGameStates::Loss);
}
}
}
int AHivemind_GameState::GetCurrentAmountRespawnsLeft() const
{
return CurrentAmountRespawnsLeft;
}
void AHivemind_GameState::SetCurrentAmountRespawnsLeft(int Amount)
{
CurrentAmountRespawnsLeft = Amount;
}
int AHivemind_GameState::GetCurrentAmountEscapingPlayers() const
{
return CurrentAmountEscapingPlayers;
}
int AHivemind_GameState::IncreaseCurrentAmountEscapingPlayers(int Amount)
{
//Increase and return new amount
CurrentAmountEscapingPlayers += Amount;
//Limit was reached, we won
if (CurrentAmountEscapingPlayers >= GetAlienAliveCount())
{
SetCurrentState(FGameStates::Win);
}
return CurrentAmountEscapingPlayers;
}
bool AHivemind_GameState::IsEnemyAlerted() const
{
return CurrentAmountAlertGuards != 0;
}
int AHivemind_GameState::IncreaseCurrentAmountAlertGuards(int Amount)
{
//If first guard to be alerted, broadcast an alert
if (Amount > 0 && CurrentAmountAlertGuards == 0)
{
OnGuardsAlertDelegate.Broadcast();
if (Sound_Alert) { UGameplayStatics::PlaySound2D(this, Sound_Alert); }
UE_LOG(LogTemp, Warning, L"OnGuardsAlertDelegate broadcast...");
}
//If last guard to be alert and we go back to 0, then broadcast back to calm
else if (Amount < 0 && CurrentAmountAlertGuards != 0 && (CurrentAmountAlertGuards + Amount) == 0)
{
OnGuardsLostDelegate.Broadcast();
if (Sound_Lost) { UGameplayStatics::PlaySound2D(this, Sound_Lost); }
UE_LOG(LogTemp, Warning, L"OnGuardsLostDelegate broadcast...");
}
//Change + return
CurrentAmountAlertGuards += Amount;
return CurrentAmountAlertGuards;
}
float AHivemind_GameState::GetMusicFadeLevel() const
{
return MusicFadeLevel;
}
void AHivemind_GameState::SetMusicFadeLevel(float Value)
{
MusicFadeLevel = Value;
}
int AHivemind_GameState::GetCurrentAmountGuardsKilled() const
{
return CurrentAmountGuardsKilled;
}
void AHivemind_GameState::IncreaseCurrentAmountGuardsKilled(int Amount)
{
CurrentAmountGuardsKilled += Amount;
}
float AHivemind_GameState::GetCurrentTotalTime() const
{
return CurrentTotalTime;
}
void AHivemind_GameState::IncreaseCurrentTotalTime(float DeltaTime)
{
CurrentTotalTime += DeltaTime;
}
#pragma endregion GettersSetters
void AHivemind_GameState::AddHealthToPlayers(float Amount)
{
for (AAlienCharacter* const Char : AlienArray)
{
//Don't add health to dead/downed players, otherwise you risk flinging the power source hardcore and it's just weird
UHealthComponent* const HealthComp = Cast<UHealthComponent>(Char->GetComponentByClass(UHealthComponent::StaticClass()));
if ( !(HealthComp->IsDead() || HealthComp->IsDowned()) )
{
HealthComp->AddHealth(Amount);
}
}
}
void AHivemind_GameState::TogglePause(APlayerController* const PController)
{
bool NewValue = !UGameplayStatics::IsGamePaused(this);
//Change state and broadcast
UGameplayStatics::SetGamePaused(this, NewValue);
OnGamePausedDelegate.Broadcast(NewValue, PController);
//Sound
if (Sound_Pause && Sound_Unpause)
{
UGameplayStatics::PlaySound2D(this, ((NewValue) ? Sound_Pause : Sound_Unpause));
}
AHivemindGameModeBase* const GameMode = Cast<AHivemindGameModeBase>(UGameplayStatics::GetGameMode(this));
if (GameMode) { GameMode->HandleMusicLowPass(NewValue); }
}
void AHivemind_GameState::NotifyPartCollected(float HealthBonus)
{
//Get gamemode for time bonus
const AHivemindGameModeBase* const GameMode = GetDefaultGameMode<AHivemindGameModeBase>();
if (GameMode)
{
IncreaseCurrentAmountCollectedParts(1);
IncreaseCurrentTime(GameMode->GetPartsTimeBonus());
AddHealthToPlayers(HealthBonus);
//Notify part collected + time obtained
OnTimeGainedDelegate.Broadcast(GameMode->GetPartsTimeBonus());
OnPartCollectedDelegate.Broadcast();
}
}
void AHivemind_GameState::CheckCurrentAmountCollectedParts()
{
//Get gamemode
const AHivemindGameModeBase* const GameMode = GetDefaultGameMode<AHivemindGameModeBase>();
if (GameMode)
{
//If amount is correct, players win
if (GameMode->GetAmountPartsToRepair() <= CurrentAmountCollectedParts)
{
SetCurrentState(FGameStates::Escape);
}
}
}