Debian Codegraph @

Sources/Jazz2/Actors/Enemies/Bosses/Uterus.cpp

raw
1 #include "Uterus.h"
2 #include "../../../ILevelHandler.h"
3 #include "../../Player.h"
4 #include "../../Explosion.h"
5 #include "../../Weapons/ShotBase.h"
6
7 #include "../../../../nCine/Base/Random.h"
8
9 #include <float.h>
10
11 namespace Jazz2::Actors::Bosses
12 {
13 Uterus::Uterus()
14 : _state(StateWaiting), _stateTime(0.0f), _endText(0), _anglePhase(0.0f), _hasShield(false)
15 {
16 }
17
18 Uterus::~Uterus()
19 {
20 for (std::int32_t i = 0; i < std::int32_t(arraySize(_shields)); i++) {
21 if (_shields[i] != nullptr) {
22 _shields[i]->SetState(ActorState::IsDestroyed, true);
23 }
24 }
25 }
26
27 void Uterus::Preload(const ActorActivationDetails& details)
28 {
29 PreloadMetadataAsync("Boss/Uterus"_s);
30 PreloadMetadataAsync("Enemy/Crab"_s);
31 }
32
33 Task<bool> Uterus::OnActivatedAsync(const ActorActivationDetails& details)
34 {
35 _endText = details.Params[1];
36 _originPos = _pos;
37 _lastPos = _pos;
38
39 SetState(ActorState::SkipPerPixelCollisions | ActorState::IsInvulnerable, true);
40 SetState(ActorState::CanBeFrozen | ActorState::ApplyGravitation, false);
41
42 _scoreValue = 3000;
43
44 async_await RequestMetadataAsync("Boss/Uterus"_s);
45 SetAnimation(AnimState::Idle);
46
47 _renderer.setDrawEnabled(false);
48
49 async_return true;
50 }
51
52 bool Uterus::OnActivatedBoss()
53 {
54 SetHealthByDifficulty(50);
55 _renderer.setDrawEnabled(true);
56
57 MoveInstantly(_originPos, MoveType::Absolute | MoveType::Force);
58
59 _state = StateOpen;
60 _stateTime = 240.0f;
61
62 _spawnCrabTime = 500.0f;
63
64 _hasShield = true;
65
66 for (std::int32_t i = 0; i < std::int32_t(arraySize(_shields)); i++) {
67 _shields[i] = std::make_shared<ShieldPart>();
68 _shields[i]->Phase = (fTwoPi * i / std::int32_t(arraySize(_shields)));
69 _shields[i]->OnActivated(ActorActivationDetails(
70 _levelHandler,
71 Vector3i(std::int32_t(_pos.X), std::int32_t(_pos.Y), _renderer.layer() + 2)
72 ));
73 _levelHandler->AddActor(_shields[i]);
74 }
75 return true;
76 }
77
78 void Uterus::OnUpdate(float timeMult)
79 {
80 OnUpdateHitbox();
81 HandleBlinking(timeMult);
82
83 switch (_state) {
84 case StateOpen: {
85 if (_stateTime <= 0.0f) {
86 PlaySfx("Closing"_s);
87
88 _state = StateTransition;
89 SetAnimation((AnimState)1073741825);
90 SetTransition((AnimState)1073741824, false, [this]() {
91 _state = StateClosed;
92 _stateTime = 280.0f;
93
94 SetState(ActorState::IsInvulnerable, true);
95 });
96 }
97
98 if (_spawnCrabTime <= 0.0f) {
99 float force = Random().NextFloat(-15.0f, 15.0f);
100
101 // TODO: Implement Crab spawn animation
102 std::shared_ptr<Enemies::Crab> crab = std::make_shared<Enemies::Crab>();
103 crab->OnActivated(ActorActivationDetails(
104 _levelHandler,
105 Vector3i((std::int32_t)_pos.X, (std::int32_t)_pos.Y, _renderer.layer() - 4)
106 ));
107 crab->AddExternalForce(force, 0.0f);
108 _levelHandler->AddActor(crab);
109
110 _spawnCrabTime = (_hasShield ? Random().NextFloat(160.0f, 220.0f) : Random().NextFloat(120.0f, 200.0f));
111 } else {
112 _spawnCrabTime -= timeMult;
113 }
114 break;
115 }
116
117 case StateClosed: {
118 if (_stateTime <= 0.0f) {
119 PlaySfx("Opening"_s);
120
121 _state = StateTransition;
122 SetAnimation(AnimState::Idle);
123 SetTransition((AnimState)1073741826, false, [this]() {
124 _state = StateOpen;
125 _stateTime = 280.0f;
126
127 SetState(ActorState::IsInvulnerable, _hasShield);
128 });
129 }
130 break;
131 }
132 }
133
134 _stateTime -= timeMult;
135
136 if (_state != StateWaiting) {
137 FollowNearestPlayer(timeMult);
138
139 _anglePhase += timeMult * 0.02f;
140 _renderer.setRotation(fPiOver2 + sinf(_anglePhase) * 0.2f);
141
142 Vector2f pos = _lastPos + Vector2f(cosf(_anglePhase) * 60.0f, sinf(_anglePhase) * 60.0f);
143 MoveInstantly(pos, MoveType::Absolute | MoveType::Force);
144
145 if (_hasShield) {
146 std::int32_t shieldCount = 0;
147 for (std::int32_t i = 0; i < std::int32_t(arraySize(_shields)); i++) {
148 if (_shields[i] != nullptr) {
149 if (_shields[i]->GetHealth() > 0) {
150 if (_shields[i]->FallTime <= 0.0f) {
151 _shields[i]->MoveInstantly(pos + Vector2f(cosf(_anglePhase + _shields[i]->Phase) * 50.0f, sinf(_anglePhase + _shields[i]->Phase) * 50.0f), MoveType::Absolute | MoveType::Force);
152 _shields[i]->Recover(_anglePhase + _shields[i]->Phase);
153 }
154 shieldCount++;
155 } else {
156 _shields[i] = nullptr;
157 }
158 }
159 }
160
161 if (shieldCount == 0) {
162 _hasShield = false;
163 SetState(ActorState::IsInvulnerable, _state != StateOpen);
164 }
165 }
166 }
167
168 OnUpdateHitbox();
169 }
170
171 void Uterus::OnUpdateHitbox()
172 {
173 UpdateHitbox(38, 60);
174 }
175
176 bool Uterus::OnPerish(ActorBase* collider)
177 {
178 Explosion::Create(_levelHandler, Vector3i((std::int32_t)_pos.X, (std::int32_t)_pos.Y, _renderer.layer() + 2), Explosion::Type::SmokePoof);
179 Explosion::Create(_levelHandler, Vector3i((std::int32_t)_pos.X, (std::int32_t)_pos.Y, _renderer.layer() - 2), Explosion::Type::RF);
180
181 CreateParticleDebrisOnPerish(collider);
182 _levelHandler->PlayCommonSfx("Splat"_s, Vector3f(_pos.X, _pos.Y, 0.0f));
183
184 StringView text = _levelHandler->GetLevelText(_endText);
185 _levelHandler->ShowLevelText(text);
186
187 return BossBase::OnPerish(collider);
188 }
189
190 void Uterus::FollowNearestPlayer(float timeMult)
191 {
192 bool found = false;
193 Vector2f targetPos = Vector2f(FLT_MAX, FLT_MAX);
194
195 auto players = _levelHandler->GetPlayers();
196 for (auto* player : players) {
197 if (player->GetHealth() <= 0) {
198 continue;
199 }
200
201 Vector2f newPos = player->GetPos();
202 if ((_pos - newPos).Length() < (_pos - targetPos).Length()) {
203 targetPos = newPos;
204 found = true;
205 }
206 }
207
208 if (found) {
209 targetPos.Y -= 100.0f;
210
211 Vector2f diff = (targetPos - _lastPos);
212 if (diff.Length() > 40) {
213 Vector2f speed = (Vector2f(_speed.X, _speed.Y) + diff.Normalized() * 0.4f).Normalized();
214 float mult = (_hasShield ? 0.8f : 2.0f) * timeMult;
215 switch (_levelHandler->GetDifficulty()) {
216 case GameDifficulty::Easy: mult *= 0.8f; break;
217 case GameDifficulty::Hard: mult *= 1.1f; break;
218 }
219 _lastPos.X += speed.X * mult;
220 _lastPos.Y += speed.Y * mult;
221 }
222 }
223 }
224
225 Task<bool> Uterus::ShieldPart::OnActivatedAsync(const ActorActivationDetails& details)
226 {
227 SetState(ActorState::CollideWithTileset | ActorState::CollideWithSolidObjects | ActorState::ApplyGravitation, false);
228 CanCollideWithShots = false;
229
230 _elasticity = 0.3f;
231
232 _health = 3;
233
234 async_await RequestMetadataAsync("Boss/Uterus"_s);
235 SetAnimation((AnimState)1073741827);
236
237 async_return true;
238 }
239
240 void Uterus::ShieldPart::OnUpdate(float timeMult)
241 {
242 UpdateFrozenState(timeMult);
243
244 if (FallTime > 0.0f) {
245 EnemyBase::OnUpdate(timeMult);
246 FallTime -= timeMult;
247 } else {
248 HandleBlinking(timeMult);
249 }
250 }
251
252 void Uterus::ShieldPart::OnUpdateHitbox()
253 {
254 UpdateHitbox(6, 6);
255 }
256
257 bool Uterus::ShieldPart::OnHandleCollision(std::shared_ptr<ActorBase> other)
258 {
259 if (auto* shotBase = runtime_cast<Weapons::ShotBase>(other.get())) {
260 DecreaseHealth(shotBase->GetStrength(), shotBase);
261
262 FallTime = 400.0f;
263
264 SetState(ActorState::CollideWithTileset | ActorState::CollideWithSolidObjects | ActorState::ApplyGravitation, true);
265
266 if (GetState(ActorState::CanBeFrozen)) {
267 HandleFrozenStateChange(shotBase);
268 }
269 return true;
270 }
271
272 return EnemyBase::OnHandleCollision(std::move(other));
273 }
274
275 bool Uterus::ShieldPart::OnPerish(ActorBase* collider)
276 {
277 CreateParticleDebrisOnPerish(collider);
278 _levelHandler->PlayCommonSfx("Splat"_s, Vector3f(_pos.X, _pos.Y, 0.0f));
279
280 Explosion::Create(_levelHandler, Vector3i((std::int32_t)_pos.X, (std::int32_t)_pos.Y, _renderer.layer() + 2), Explosion::Type::Tiny);
281
282 return EnemyBase::OnPerish(collider);
283 }
284
285 void Uterus::ShieldPart::Recover(float phase)
286 {
287 SetState(ActorState::CollideWithTileset | ActorState::CollideWithSolidObjects | ActorState::ApplyGravitation, false);
288
289 _renderer.setRotation(phase);
290 }
291 }