Debian Codegraph @

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

raw
1 #include "Bubba.h"
2 #include "../../../ILevelHandler.h"
3 #include "../../Player.h"
4 #include "../../Explosion.h"
5
6 #include "../../../../nCine/Base/Random.h"
7
8 #include <float.h>
9
10 namespace Jazz2::Actors::Bosses
11 {
12 Bubba::Bubba()
13 : _state(State::Waiting), _stateTime(0.0f), _tornadoCooldown(120.0f), _endText(0)
14 {
15 }
16
17 void Bubba::Preload(const ActorActivationDetails& details)
18 {
19 PreloadMetadataAsync("Boss/Bubba"_s);
20 }
21
22 Task<bool> Bubba::OnActivatedAsync(const ActorActivationDetails& details)
23 {
24 _endText = details.Params[1];
25 _scoreValue = 4000;
26
27 SetState(ActorState::CollideWithTilesetReduced, true);
28
29 async_await RequestMetadataAsync("Boss/Bubba"_s);
30 SetAnimation(AnimState::Idle);
31
32 async_return true;
33 }
34
35 bool Bubba::OnActivatedBoss()
36 {
37 SetHealthByDifficulty(93);
38 FollowNearestPlayer();
39 return true;
40 }
41
42 void Bubba::OnUpdate(float timeMult)
43 {
44 BossBase::OnUpdate(timeMult);
45
46 if (_tornadoCooldown > 0.0f) {
47 _tornadoCooldown -= timeMult;
48 }
49
50 // Process level bounds
51 Recti levelBounds = _levelHandler->GetLevelBounds();
52 if (_pos.X < levelBounds.X) {
53 _pos.X = float(levelBounds.X);
54 } else if (_pos.X > levelBounds.X + levelBounds.W) {
55 _pos.X = float(levelBounds.X + levelBounds.W);
56 }
57
58 if (_frozenTimeLeft > 0.0f) {
59 return;
60 }
61
62 switch (_state) {
63 case State::Jumping: {
64 if (_speed.Y >= 0.0f) {
65 _state = State::Falling;
66 SetAnimation(AnimState::Fall);
67 }
68 break;
69 }
70
71 case State::Falling: {
72 if (GetState(ActorState::CanJump)) {
73 _speed.Y = 0.0f;
74 _speed.X = 0.0f;
75
76 _state = State::Transition;
77 SetTransition(AnimState::TransitionFallToIdle, false, [this]() {
78 float rand = Random().NextFloat();
79 bool spewFileball = (rand < 0.35f);
80 bool tornado = (rand < 0.65f && _tornadoCooldown <= 0.0f);
81 if (spewFileball) {
82 PlaySfx("Sneeze"_s);
83
84 SetTransition(AnimState::Shoot, false, [this]() {
85 float x = (IsFacingLeft() ? -16.0f : 16.0f);
86 float y = -5.0f;
87
88 std::shared_ptr<Fireball> fireball = std::make_shared<Fireball>();
89 uint8_t fireballParams[1] = { (uint8_t)(IsFacingLeft() ? 1 : 0) };
90 fireball->OnActivated(ActorActivationDetails(
91 _levelHandler,
92 Vector3i((std::int32_t)(_pos.X + x), (std::int32_t)(_pos.Y + y), _renderer.layer() - 2),
93 fireballParams
94 ));
95 _levelHandler->AddActor(fireball);
96
97 SetTransition(AnimState::TransitionShootToIdle, false, [this]() {
98 FollowNearestPlayer();
99 });
100 });
101 } else if (tornado) {
102 TornadoToNearestPlayer();
103 } else {
104 FollowNearestPlayer();
105 }
106 });
107 }
108 break;
109 }
110
111 case State::Tornado: {
112 if (_stateTime <= 0.0f) {
113 float cooldownMin, cooldownMax;
114 switch (_levelHandler->GetDifficulty()) {
115 case GameDifficulty::Easy:
116 cooldownMin = 600.0f;
117 cooldownMax = 1200.0f;
118 break;
119 default:
120 case GameDifficulty::Normal:
121 cooldownMin = 300.0f;
122 cooldownMax = 600.0f;
123 break;
124 case GameDifficulty::Hard:
125 cooldownMin = 60.0f;
126 cooldownMax = 480.0f;
127 break;
128 }
129
130 _tornadoCooldown = Random().NextFloat(cooldownMin, cooldownMax);
131 _state = State::Transition;
132 SetTransition((AnimState)1073741832, false, [this]() {
133 SetState(ActorState::CollideWithTilesetReduced | ActorState::ApplyGravitation, true);
134
135 _state = State::Falling;
136
137 if (_tornadoNoise != nullptr) {
138 _tornadoNoise->stop();
139 _tornadoNoise = nullptr;
140 }
141 SetAnimation((AnimState)1073741833);
142 });
143 }
144
145 break;
146 }
147
148 case State::Dying: {
149 float time = (_renderer.AnimTime / _renderer.AnimDuration);
150 _renderer.setColor(Colorf(1.0f, 1.0f, 1.0f, 1.0f - (time * time * time * time)));
151 break;
152 }
153 }
154
155 _stateTime -= timeMult;
156 }
157
158 void Bubba::OnUpdateHitbox()
159 {
160 UpdateHitbox(20, 24);
161 }
162
163 bool Bubba::OnPerish(ActorBase* collider)
164 {
165 // It must be done here, because the player may not exist after animation callback
166 AddScoreToCollider(collider);
167
168 ForceCancelTransition();
169
170 CreateParticleDebrisOnPerish(ParticleDebrisEffect::Standard, Vector2f::Zero);
171 _levelHandler->PlayCommonSfx("Splat"_s, Vector3f(_pos.X, _pos.Y, 0.0f));
172
173 StringView text = _levelHandler->GetLevelText(_endText);
174 _levelHandler->ShowLevelText(text);
175
176 _speed.X = 0.0f;
177 _speed.Y = -2.0f;
178 _externalForce.X = 0.0f;
179 _externalForce.Y = 0.0f;
180 _internalForceY = 0.0f;
181 _frozenTimeLeft = std::min(1.0f, _frozenTimeLeft);
182
183 SetState(ActorState::CollideWithTileset | ActorState::CollideWithTilesetReduced | ActorState::CollideWithOtherActors | ActorState::ApplyGravitation, false);
184
185 _state = State::Dying;
186 SetTransition(AnimState::TransitionDeath, false, [this, collider]() {
187 BossBase::OnPerish(collider);
188 });
189
190 return false;
191 }
192
193 void Bubba::OnHitWall(float timeMult)
194 {
195 if (_state == State::Tornado && _stateTime > 1.0f) {
196 _stateTime = 1.0f;
197 }
198 }
199
200 void Bubba::FollowNearestPlayer()
201 {
202 bool found = false;
203 bool isFacingLeft = false;
204
205 float randomValue = Random().NextFloat();
206
207 if (randomValue < 0.1f) {
208 found = true;
209 isFacingLeft = true;
210 } else if (randomValue > 0.9f) {
211 found = true;
212 isFacingLeft = false;
213 } else {
214 Vector2f targetPos = Vector2f(FLT_MAX, FLT_MAX);
215 auto players = _levelHandler->GetPlayers();
216 for (auto* player : players) {
217 if (player->GetHealth() <= 0) {
218 continue;
219 }
220
221 Vector2f newPos = player->GetPos();
222 if ((_pos - newPos).Length() < (_pos - targetPos).Length()) {
223 targetPos = newPos;
224 found = true;
225 }
226 }
227 isFacingLeft = (targetPos.X < _pos.X);
228 }
229
230 if (found) {
231 _state = State::Jumping;
232 _stateTime = 26;
233
234 SetFacingLeft(isFacingLeft);
235
236 _speed.X = (isFacingLeft ? -1.3f : 1.3f);
237 _speed.Y = -5.5f;
238 _internalForceY = -0.8f;
239
240 PlaySfx("Jump"_s);
241
242 SetTransition((AnimState)1073741825, false);
243 SetAnimation(AnimState::Jump);
244 }
245 }
246
247 void Bubba::TornadoToNearestPlayer()
248 {
249 bool found = false;
250 Vector2f targetPos = Vector2f(FLT_MAX, FLT_MAX);
251
252 auto players = _levelHandler->GetPlayers();
253 for (auto* player : players) {
254 if (player->GetHealth() <= 0) {
255 continue;
256 }
257
258 Vector2f newPos = player->GetPos();
259 if ((_pos - newPos).SqrLength() < (_pos - targetPos).SqrLength()) {
260 targetPos = newPos;
261 found = true;
262 }
263 }
264
265 if (found) {
266 _state = State::Tornado;
267 _stateTime = 60.0f;
268
269 SetState(ActorState::CollideWithTilesetReduced | ActorState::ApplyGravitation, false);
270
271 Vector2f diff = (targetPos - _pos);
272
273 _tornadoNoise = PlaySfx("Tornado"_s);
274 SetTransition((AnimState)1073741830, false, [this, diff]() {
275 _speed.X = (diff.X / _stateTime);
276 _speed.Y = (diff.Y / _stateTime);
277 _internalForceY = 0.0f;
278 _externalForce.Y = 0.0f;
279
280 SetAnimation((AnimState)1073741831);
281 });
282 }
283 }
284
285 Task<bool> Bubba::Fireball::OnActivatedAsync(const ActorActivationDetails& details)
286 {
287 SetFacingLeft(details.Params[0] != 0);
288 _speed.X = (IsFacingLeft() ? -4.8f : 4.8f);
289 _timeLeft = 50.0f;
290
291 SetState(ActorState::IsInvulnerable, true);
292 SetState(ActorState::CanBeFrozen | ActorState::ApplyGravitation, false);
293 CanCollideWithShots = false;
294
295 _health = INT32_MAX;
296
297 async_await RequestMetadataAsync("Boss/Bubba"_s);
298 SetAnimation((AnimState)1073741834);
299
300 async_return true;
301 }
302
303 void Bubba::Fireball::OnUpdate(float timeMult)
304 {
305 MoveInstantly(Vector2f(_speed.X * timeMult, _speed.Y * timeMult), MoveType::Relative | MoveType::Force);
306
307 if (_timeLeft <= 0.0f) {
308 DecreaseHealth(INT32_MAX);
309 } else {
310 _timeLeft -= timeMult;
311 }
312 }
313
314 void Bubba::Fireball::OnUpdateHitbox()
315 {
316 UpdateHitbox(18, 18);
317 }
318
319 void Bubba::Fireball::OnEmitLights(SmallVectorImpl<LightEmitter>& lights)
320 {
321 auto& light = lights.emplace_back();
322 light.Pos = _pos;
323 light.Intensity = 0.85f;
324 light.Brightness = 0.4f;
325 light.RadiusNear = 0.0f;
326 light.RadiusFar = 30.0f;
327 }
328
329 bool Bubba::Fireball::OnHandleCollision(std::shared_ptr<ActorBase> other)
330 {
331 if (auto* player = runtime_cast<Player>(other.get())) {
332 DecreaseHealth(INT32_MAX);
333 }
334
335 return ActorBase::OnHandleCollision(std::move(other));
336 }
337
338 bool Bubba::Fireball::OnPerish(ActorBase* collider)
339 {
340 Explosion::Create(_levelHandler, Vector3i((std::int32_t)(_pos.X + _speed.X), (std::int32_t)(_pos.Y + _speed.Y), _renderer.layer() + 2), Explosion::Type::RF);
341
342 return EnemyBase::OnPerish(collider);
343 }
344 }