/
ChompPlayingSubstate.h
268 lines (221 loc) · 6.88 KB
/
ChompPlayingSubstate.h
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
#pragma once
#include "CoreMinimal.h"
#include "ChompPlayingSubstateEnum.h"
#include "UE5Coro.h"
#include "Wave.h"
#include "Misc/Optional.h"
#include "UObject/Object.h"
#include "ChompPlayingSubstate.generated.h"
USTRUCT()
struct FChompPlayingSubstate
{
GENERATED_BODY()
private:
// The duration of time to spend in the EChompPlayingSubstateEnum::Frightened substate.
UPROPERTY(VisibleInstanceOnly)
double FrightenedDurationRef = 0.0;
// The configured WavesRef of ghost behavior.
UPROPERTY(VisibleInstanceOnly)
TArray<FWave> WavesRef;
// Corresponding countdown timers for the configured WavesRef.
UPROPERTY(VisibleInstanceOnly)
TArray<double> WaveCountdownTimers;
// Index of the current wave.
UPROPERTY(VisibleInstanceOnly)
int CurrentWaveIndex = 0;
// Start time of current wave, as reported by UWorld::GetTimeSeconds.
UPROPERTY(VisibleInstanceOnly)
double CurrentWaveStartTime = 0.0;
// Coroutine for when game is in the EChompPlayingSubstateEnum::Frightened substate.
UE5Coro::TCoroutine<> Frightened = UE5Coro::TCoroutine<>::FromResult(nullptr);
// Coroutine for progressing through the configured WavesRef.
UE5Coro::TCoroutine<> WaveProgression = UE5Coro::TCoroutine<>::FromResult(nullptr);
// If the Frightened coroutine is running, this field will contain the number of ghosts consumed within the
// current frightened duration.
//
// Resets upon completion of the Frightened coroutine.
UPROPERTY(VisibleInstanceOnly)
int NumGhostsConsumed = 0;
UPROPERTY(VisibleInstanceOnly)
bool IsWaveProgressionRunning = false;
UE5Coro::TCoroutine<> FrightenAsync(const UWorld* WorldInstance)
{
// Pre-conditions.
check(0 <= CurrentWaveIndex && CurrentWaveIndex < WavesRef.Num());
// Pause the current wave.
if (!WaveProgression.IsDone())
{
WaveProgression.Cancel();
if (const auto Timer = WaveCountdownTimers[CurrentWaveIndex]; Timer > 0.0)
{
const auto ElapsedTime = WorldInstance->GetTimeSeconds() - CurrentWaveStartTime;
WaveCountdownTimers[CurrentWaveIndex] = Timer - ElapsedTime;
}
}
// Wait.
co_await UE5Coro::Latent::Seconds(FrightenedDurationRef);
// When frightened substate is over, reset the number of ghosts.
NumGhostsConsumed = 0;
// Also reboot the WaveProgression coroutine.
WaveProgression = StartAsync(WorldInstance, CurrentWaveIndex);
// Post-conditions.
const auto DebugThis = this;
for (auto i = 0; i < WaveCountdownTimers.Num(); i++)
{
if (const auto [_, Duration] = WavesRef[CurrentWaveIndex]; Duration > 0.0)
{
check(WaveCountdownTimers[CurrentWaveIndex] <= Duration);
}
}
check(NumGhostsConsumed == 0);
}
UE5Coro::TCoroutine<> StartAsync(const UWorld* WorldInstance, const int StartIndex)
{
// Pre-conditions.
check(StartIndex >= 0);
// Reset timers.
if (StartIndex == 0)
{
for (auto i = 0; i < WavesRef.Num(); i++)
{
const auto [_, Duration] = WavesRef[i];
WaveCountdownTimers[i] = Duration;
}
}
// Progress through waves.
//
// Note that it doesn't matter what the last wave is.
for (CurrentWaveIndex = StartIndex; CurrentWaveIndex < WavesRef.Num() - 1; CurrentWaveIndex++)
{
CurrentWaveStartTime = WorldInstance->GetTimeSeconds();
if (const auto Timer = WaveCountdownTimers[CurrentWaveIndex]; Timer > 0.0)
{
co_await UE5Coro::Latent::Seconds(Timer);
WaveCountdownTimers[CurrentWaveIndex] = 0.0;
}
}
// Post-conditions.
const auto DebugThis = this;
check(FrightenedDurationRef > 0.0);
checkf(CurrentWaveIndex == WavesRef.Num() - 1, TEXT("Should be last index."));
check(CurrentWaveStartTime <= WorldInstance->GetTimeSeconds());
for (auto i = 0; i < WaveCountdownTimers.Num() - 1; i++)
{
const auto Timer = WaveCountdownTimers[i];
check(Timer <= 0.0);
}
}
public:
[[nodiscard]] FChompPlayingSubstate()
{
}
[[nodiscard]] FChompPlayingSubstate(
const double FrightenedDurationRef,
const TArray<FWave>& WavesRef
):
FrightenedDurationRef(FrightenedDurationRef),
WavesRef(WavesRef)
{
// Reset timers.
for (const auto& [_, Duration] : WavesRef)
WaveCountdownTimers.Add(Duration);
// Post-conditions.
check(this->FrightenedDurationRef > 0.0);
#ifndef WITH_EDITOR
check(this->WavesRef.Num() > 0);
check(this->WaveCountdownTimers.Num() == this->WavesRef.Num());
#endif
}
void Frighten(const UWorld* WorldInstance)
{
if (!Frightened.IsDone())
Frightened.Cancel();
Frightened = FrightenAsync(WorldInstance);
// Post-conditions.
check(!Frightened.IsDone());
}
EChompPlayingSubstateEnum GetEnum(const bool GetUnderlyingSubstate = false) const
{
// Pre-conditions.
check(0 <= CurrentWaveIndex && CurrentWaveIndex < WavesRef.Num());
if (!GetUnderlyingSubstate && !Frightened.IsDone())
return EChompPlayingSubstateEnum::Frightened;
const auto [PlayingState, Duration] = WavesRef[CurrentWaveIndex];
return PlayingState;
}
void Start(UWorld* WorldInstance)
{
// Pre-conditions.
check(GetEnum() != EChompPlayingSubstateEnum::Frightened);
checkf(WaveProgression.IsDone(), TEXT("Should not have already started the wave progression."));
check(!IsWaveProgressionRunning);
WaveProgression = StartAsync(WorldInstance, 0);
IsWaveProgressionRunning = true;
// Post-conditions.
check(GetEnum() != EChompPlayingSubstateEnum::Frightened);
check(CurrentWaveIndex == 0);
check(IsWaveProgressionRunning);
}
UE5Coro::TCoroutine<> Stop()
{
NumGhostsConsumed = 0;
IsWaveProgressionRunning = false;
if (!Frightened.IsDone())
{
Frightened.Cancel();
}
if (!WaveProgression.IsDone())
{
WaveProgression.Cancel();
}
// Synchronous post-conditions.
check(NumGhostsConsumed == 0);
check(!IsWaveProgressionRunning);
// Async post-conditions.
// Note: these conditions are not true when transitioning from the Game map to the MainMenu map.
// Keeping these lines for future reference, though.
#if false
co_await Frightened;
co_await WaveProgression;
check(Frightened.IsDone());
check(WaveProgression.IsDone());
#endif
co_return;
}
bool IsRunning() const
{
return IsWaveProgressionRunning;
}
void ConsumeGhost()
{
// Pre-conditions.
const auto OldNumGhostsConsumed = NumGhostsConsumed;
NumGhostsConsumed++;
// Post-conditions.
check(NumGhostsConsumed == OldNumGhostsConsumed + 1);
}
int GetNumGhostsConsumed() const
{
// Pre-conditions.
check(
GetEnum() != EChompPlayingSubstateEnum::Frightened
? NumGhostsConsumed == 0
: NumGhostsConsumed >= 0
);
return NumGhostsConsumed;
}
FChompPlayingSubstate& operator=(const FChompPlayingSubstate& Other)
{
if (this == &Other)
return *this;
FrightenedDurationRef = Other.FrightenedDurationRef;
WavesRef = Other.WavesRef;
WaveCountdownTimers = Other.WaveCountdownTimers;
CurrentWaveIndex = Other.CurrentWaveIndex;
CurrentWaveStartTime = Other.CurrentWaveStartTime;
Frightened = Other.Frightened;
WaveProgression = Other.WaveProgression;
NumGhostsConsumed = Other.NumGhostsConsumed;
return *this;
}
};