Debian Codegraph @

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

raw
1 #include "Devan.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 static constexpr AnimState ShootStart = (AnimState)10;
14 static constexpr AnimState ShootInProgress = (AnimState)11;
15 static constexpr AnimState ShootEnd = (AnimState)12;
16 static constexpr AnimState ShootEnd2 = (AnimState)13;
17 static constexpr AnimState CrouchStart = (AnimState)14; // TODO: Unused
18 static constexpr AnimState CrouchInProgress = (AnimState)15; // TODO: Unused
19 static constexpr AnimState CrouchEnd = (AnimState)16; // TODO: Unused
20 static constexpr AnimState JumpStart = (AnimState)17; // TODO: Unused
21 static constexpr AnimState JumpInProgress = (AnimState)18; // TODO: Unused
22 static constexpr AnimState JumpEnd = (AnimState)19; // TODO: Unused
23 static constexpr AnimState JumpEnd2 = (AnimState)20; // TODO: Unused
24 static constexpr AnimState DisarmedStart = (AnimState)22;
25 static constexpr AnimState DisarmedGunDecor = (AnimState)23;
26 static constexpr AnimState DisorientedStart = (AnimState)24;
27 static constexpr AnimState Disoriented = (AnimState)25;
28 static constexpr AnimState DisorientedWarpOut = (AnimState)26;
29 static constexpr AnimState DevanBullet = (AnimState)27;
30
31 static constexpr AnimState DemonFly = (AnimState)30;
32 static constexpr AnimState DemonTransformStart = (AnimState)31;
33 static constexpr AnimState DemonTransformEnd = (AnimState)32;
34 static constexpr AnimState DemonTurn = (AnimState)33;
35 static constexpr AnimState DemonSpewFireball = (AnimState)34;
36 static constexpr AnimState DemonSpewFireballEnd = (AnimState)35;
37 static constexpr AnimState DemonFireball = (AnimState)36;
38
39 Devan::Devan()
40 : _state(State::Waiting), _stateTime(0.0f), _crouchCooldown(60.0f), _endText(0), _shots(0), _isDemon(false),
41 _isDead(false)
42 {
43 }
44
45 void Devan::Preload(const ActorActivationDetails& details)
46 {
47 PreloadMetadataAsync("Boss/Devan"_s);
48 }
49
50 Task<bool> Devan::OnActivatedAsync(const ActorActivationDetails& details)
51 {
52 _endText = details.Params[1];
53
54 _scoreValue = 10000;
55
56 SetState(ActorState::CollideWithOtherActors, false);
57
58 async_await RequestMetadataAsync("Boss/Devan"_s);
59 SetAnimation(AnimState::Idle);
60
61 _renderer.setDrawEnabled(false);
62
63 async_return true;
64 }
65
66 bool Devan::OnActivatedBoss()
67 {
68 SetHealthByDifficulty(140 * 2);
69
70 _state = State::WarpingIn;
71 _stateTime = 120.0f;
72 _attackTime = 90.0f;
73 _anglePhase = 0.0f;
74 _shots = 0;
75 _isDemon = false;
76 _isDead = false;
77
78 SetState(ActorState::CollideWithOtherActors, true);
79
80 return true;
81 }
82
83 void Devan::OnUpdate(float timeMult)
84 {
85 if (_isDemon) {
86 OnUpdateHitbox();
87 HandleBlinking(timeMult);
88
89 MoveInstantly(Vector2f(_speed.X * timeMult, _speed.Y * timeMult), MoveType::Relative | MoveType::Force);
90 } else {
91 BossBase::OnUpdate(timeMult);
92 }
93
94 if (_frozenTimeLeft > 0.0f) {
95 return;
96 }
97
98 switch (_state) {
99 case State::WarpingIn: {
100 if (_stateTime <= 0.0f) {
101 bool found = false;
102 Vector2f targetPos = Vector2f(FLT_MAX, FLT_MAX);
103
104 auto players = _levelHandler->GetPlayers();
105 for (auto* player : players) {
106 if (player->GetHealth() <= 0) {
107 continue;
108 }
109
110 Vector2f newPos = player->GetPos();
111 if ((_pos - newPos).Length() < (_pos - targetPos).Length()) {
112 targetPos = newPos;
113 found = true;
114 }
115 }
116
117 if (found) {
118 SetFacingLeft(targetPos.X < _pos.X);
119 }
120
121 _renderer.setDrawEnabled(true);
122
123 _state = State::Transition;
124 SetTransition(AnimState::TransitionWarpIn, false, [this]() {
125 _state = State::Idling;
126 _stateTime = 80.0f;
127
128 SetState(ActorState::IsInvulnerable, false);
129 _canHurtPlayer = true;
130 });
131 }
132
133 break;
134 }
135
136 case State::Idling: {
137 if (_stateTime <= 0.0f) {
138 FollowNearestPlayer(State::Running1, Random().NextFloat(60.0f, 120.0f));
139 }
140
141 break;
142 }
143
144 case State::Running1: {
145 if (_crouchCooldown <= 0.0 && ShouldCrouch()) {
146 Crouch();
147 } else if (_health < _maxHealth / 2) {
148 _isDemon = true;
149 _speed.X = 0.0f;
150 _speed.Y = 0.0f;
151
152 SetState(ActorState::IsInvulnerable, true);
153 SetState(ActorState::CanBeFrozen, false);
154
155 std::shared_ptr<DisarmedGun> gun = std::make_shared<DisarmedGun>();
156 std::uint8_t gunParams[1] = { (std::uint8_t)(IsFacingLeft() ? 1 : 0) };
157 gun->OnActivated(ActorActivationDetails(
158 _levelHandler,
159 Vector3i((std::int32_t)_pos.X + (IsFacingLeft() ? 12 : -12), (std::int32_t)_pos.Y - 8, _renderer.layer() + 40),
160 gunParams
161 ));
162 _levelHandler->AddActor(gun);
163
164 _state = State::Transition;
165 SetTransition(DisarmedStart, false, [this]() {
166 SetAnimation(DemonFly);
167 SetTransition(DemonTransformStart, false, [this]() {
168 SetState(ActorState::ApplyGravitation | ActorState::IsInvulnerable, false);
169 _state = State::DemonFlying;
170
171 _lastPos = _pos;
172 _targetPos = _lastPos + Vector2f(0.0f, -200.0f);
173 });
174 });
175 } else if (_stateTime <= 0.0f) {
176 if (Random().NextFloat() < 0.5f) {
177 FollowNearestPlayer(State::Running1, Random().NextFloat(60, 120));
178 } else {
179 FollowNearestPlayer(State::Running2, Random().NextFloat(10, 30));
180 }
181 } else {
182 if (!CanMoveToPosition(_speed.X, 0)) {
183 SetFacingLeft(!IsFacingLeft());
184 _speed.X = (IsFacingLeft() ? -4.0f : 4.0f);
185 }
186 }
187 break;
188 }
189
190 case State::Running2: {
191 if (_stateTime <= 0.0f) {
192 _speed.X = 0.0f;
193
194 _state = State::Transition;
195 SetTransition(AnimState::TransitionRunToIdle, false, [this]() {
196 SetTransition(ShootStart, false, [this]() {
197 _shots = Random().Next(1, 8);
198 Shoot();
199 });
200 });
201 }
202 break;
203 }
204
205 case State::Crouch: {
206 if (_stateTime <= 0.0f) {
207 _state = State::Transition;
208
209 switch (_levelHandler->GetDifficulty()) {
210 case GameDifficulty::Easy: _crouchCooldown = Random().NextFloat(360.0f, 600.0f); break;
211 default:
212 case GameDifficulty::Normal: _crouchCooldown = Random().NextFloat(180.0f, 360.0f); break;
213 case GameDifficulty::Hard: _crouchCooldown = Random().NextFloat(100.0f, 240.0f); break;
214 }
215
216 SetState(ActorState::SkipPerPixelCollisions, false);
217 SetTransition(CrouchEnd, false, [this]() {
218 FollowNearestPlayer(State::Running1, Random().NextFloat(60.0f, 150.0f));
219 });
220 }
221 break;
222 }
223
224 case State::DemonFlying: {
225 if (_attackTime <= 0.0f) {
226 _state = State::DemonSpewingFireball;
227 } else {
228 _attackTime -= timeMult;
229 FollowNearestPlayerDemon(timeMult);
230 }
231 break;
232 }
233
234 case State::DemonSpewingFireball: {
235 _state = State::Transition;
236 SetTransition(DemonSpewFireball, false, [this]() {
237 PlaySfx("SpitFireball"_s);
238
239 std::shared_ptr<Fireball> fireball = std::make_shared<Fireball>();
240 std::uint8_t fireballParams[1] = { (std::uint8_t)(IsFacingLeft() ? 1 : 0) };
241 fireball->OnActivated(ActorActivationDetails(
242 _levelHandler,
243 Vector3i((std::int32_t)_pos.X + (IsFacingLeft() ? -26 : 26), (std::int32_t)_pos.Y - 14, _renderer.layer() + 2),
244 fireballParams
245 ));
246 _levelHandler->AddActor(fireball);
247
248 SetTransition(DemonSpewFireballEnd, false, [this]() {
249 _state = State::DemonFlying;
250
251 _attackTime = Random().NextFloat(100.0f, 240.0f);
252 });
253 });
254 break;
255 }
256
257 case State::Falling: {
258 if (GetState(ActorState::CanJump)) {
259 _state = State::Transition;
260 SetTransition(DisorientedStart, false, [this]() {
261 SetTransition(Disoriented, false, [this]() {
262 SetTransition(Disoriented, false, [this]() {
263 SetTransition(DisorientedWarpOut, false, [this]() {
264 BossBase::OnPerish(nullptr);
265 });
266 });
267 });
268 });
269 }
270 break;
271 }
272 }
273
274 _stateTime -= timeMult;
275 _crouchCooldown -= timeMult;
276 }
277
278 void Devan::OnUpdateHitbox()
279 {
280 BossBase::OnUpdateHitbox();
281
282 if (_state == State::Crouch) {
283 // Smaller hitbox when Devan is crouching
284 AABBInner.T = AABBInner.B - 16.0f;
285 }
286 }
287
288 bool Devan::OnPerish(ActorBase* collider)
289 {
290 if (_isDead) {
291 return false;
292 }
293
294 StringView text = _levelHandler->GetLevelText(_endText);
295 _levelHandler->ShowLevelText(text);
296
297 _isDead = true;
298
299 _speed.X = 0.0f;
300 _speed.Y = 0.0f;
301
302 SetState(ActorState::ApplyGravitation, false);
303
304 _state = State::Transition;
305 SetTransition(DemonTransformEnd, false, [this]() {
306 SetState(ActorState::ApplyGravitation, true);
307
308 _isDemon = false;
309 _state = State::Falling;
310 SetAnimation(AnimState::Freefall);
311 });
312
313 return false;
314 }
315
316 void Devan::FollowNearestPlayer(State newState, float time)
317 {
318 bool found = false;
319 Vector2f targetPos = Vector2f(FLT_MAX, FLT_MAX);
320
321 auto players = _levelHandler->GetPlayers();
322 for (auto* player : players) {
323 if (player->GetHealth() <= 0) {
324 continue;
325 }
326
327 Vector2f newPos = player->GetPos();
328 if ((_pos - newPos).Length() < (_pos - targetPos).Length()) {
329 targetPos = newPos;
330 found = true;
331 }
332 }
333
334 if (found) {
335 _state = newState;
336 _stateTime = time;
337
338 SetFacingLeft(targetPos.X < _pos.X);
339 _speed.X = (IsFacingLeft() ? -4.0f : 4.0f);
340
341 SetAnimation(AnimState::Run);
342 }
343 }
344
345 void Devan::FollowNearestPlayerDemon(float timeMult)
346 {
347 bool found = false;
348 Vector2f foundPos = Vector2f(FLT_MAX, FLT_MAX);
349
350 auto players = _levelHandler->GetPlayers();
351 for (auto* player : players) {
352 if (player->GetHealth() <= 0) {
353 continue;
354 }
355
356 Vector2f newPos = player->GetPos();
357 if ((_pos - newPos).Length() < (_pos - foundPos).Length()) {
358 foundPos = newPos;
359 found = true;
360 }
361 }
362
363 if (found) {
364 bool willFaceLeft = (foundPos.X < _pos.X);
365
366 float xOffset, yOffset, speedMult;
367 if (_levelHandler->GetDifficulty() == GameDifficulty::Easy) {
368 xOffset = 100.0f;
369 yOffset = 70.0f;
370 speedMult = 0.01f;
371 } else {
372 xOffset = 70.0f;
373 yOffset = 30.0f;
374 speedMult = 0.005f;
375 }
376
377 _targetPos = foundPos;
378 _targetPos.X += (willFaceLeft ? xOffset : -xOffset);
379 _targetPos.Y -= yOffset;
380
381 Vector2f speed = ((_targetPos - _lastPos) * speedMult + _lastSpeed * 1.4f) / 2.4f;
382 _lastPos.X += speed.X;
383 _lastPos.Y += speed.Y;
384 _lastSpeed = speed;
385
386 if (IsFacingLeft() != willFaceLeft) {
387 SetFacingLeft(willFaceLeft);
388 SetTransition(DemonTurn, false);
389 }
390 }
391
392 _anglePhase += timeMult * 0.02f;
393
394 MoveInstantly(_lastPos + Vector2f(0.0f, sinf(_anglePhase) * 22.0f), MoveType::Absolute | MoveType::Force);
395 }
396
397 void Devan::Shoot()
398 {
399 PlaySfx("Shoot"_s);
400
401 SetTransition(ShootInProgress, false, [this]() {
402 std::shared_ptr<Bullet> bullet = std::make_shared<Bullet>();
403 std::uint8_t fireballParams[1] = { (std::uint8_t)(IsFacingLeft() ? 1 : 0) };
404 bullet->OnActivated(ActorActivationDetails(
405 _levelHandler,
406 Vector3i((std::int32_t)_pos.X + (IsFacingLeft() ? -24 : 24), (std::int32_t)_pos.Y + 2, _renderer.layer() + 2),
407 fireballParams
408 ));
409 _levelHandler->AddActor(bullet);
410
411 _shots--;
412
413 SetTransition(ShootEnd, false, [this]() {
414 if (_shots > 0) {
415 Shoot();
416 } else {
417 Run();
418 }
419 });
420 });
421 }
422
423 void Devan::Run()
424 {
425 SetTransition(ShootEnd2, false, [this]() {
426 FollowNearestPlayer(State::Running1, Random().NextFloat(60.0f, 150.0f));
427 });
428 }
429
430 void Devan::Crouch()
431 {
432 _speed.X = 0.0f;
433 _state = State::Crouch;
434
435 switch (_levelHandler->GetDifficulty()) {
436 case GameDifficulty::Easy: _stateTime = Random().NextFloat(100.0f, 240.0f); break;
437 default:
438 case GameDifficulty::Normal: _stateTime = Random().NextFloat(60.0f, 120.0f); break;
439 case GameDifficulty::Hard: _stateTime = Random().NextFloat(30.0f, 80.0f); break;
440 }
441
442 SetState(ActorState::SkipPerPixelCollisions, true);
443
444 SetAnimation(CrouchInProgress);
445 SetTransition(CrouchStart, false);
446 }
447
448 bool Devan::ShouldCrouch() const
449 {
450 constexpr float Distance = 64.0f;
451
452 bool shouldCrouch = false;
453 AABBf crouchAabb = AABB;
454 crouchAabb.L -= Distance;
455 crouchAabb.R += Distance;
456
457 _levelHandler->FindCollisionActorsByAABB(this, crouchAabb, [this, &shouldCrouch](ActorBase* actor) {
458 if (auto* shot = runtime_cast<Weapons::ShotBase>(actor)) {
459 float xSpeed = shot->GetSpeed().X;
460 float x = shot->GetPos().X;
461 float xSelf = _pos.X;
462 // Check if the shot is moving towards the boss
463 if (std::abs(xSpeed) > 0.0f && std::signbit(xSelf - x) == std::signbit(xSpeed)) {
464 shouldCrouch = true;
465 return false;
466 }
467 }
468 return true;
469 });
470
471 return shouldCrouch;
472 }
473
474 Devan::DisarmedGun::DisarmedGun()
475 {
476 }
477
478 Task<bool> Devan::DisarmedGun::OnActivatedAsync(const ActorActivationDetails& details)
479 {
480 SetFacingLeft(details.Params[0] != 0);
481 _speed.X = (IsFacingLeft() ? 3.5f : -3.5f);
482 _speed.Y = -2.0f;
483
484 SetState(ActorState::IsInvulnerable | ActorState::SkipPerPixelCollisions, true);
485 SetState(ActorState::CanBeFrozen | ActorState::CollideWithOtherActors | ActorState::CollideWithSolidObjects | ActorState::CollideWithTileset, false);
486
487 _health = INT32_MAX;
488 _timeLeft = 10.0f * FrameTimer::FramesPerSecond;
489
490 async_await RequestMetadataAsync("Boss/Devan"_s);
491 SetAnimation(DisarmedGunDecor);
492
493 async_return true;
494 }
495
496 void Devan::DisarmedGun::OnUpdate(float timeMult)
497 {
498 ActorBase::OnUpdate(timeMult);
499
500 _timeLeft -= timeMult;
501 if (_timeLeft <= 0.0f) {
502 DecreaseHealth(INT32_MAX);
503 }
504 }
505
506 void Devan::DisarmedGun::OnUpdateHitbox()
507 {
508 }
509
510 Devan::Bullet::Bullet()
511 {
512 }
513
514 Task<bool> Devan::Bullet::OnActivatedAsync(const ActorActivationDetails& details)
515 {
516 SetFacingLeft(details.Params[0] != 0);
517 _speed.X = (IsFacingLeft() ? -8.0f : 8.0f);
518
519 SetState(ActorState::IsInvulnerable, true);
520 SetState(ActorState::CanBeFrozen | ActorState::ApplyGravitation, false);
521 CanCollideWithShots = false;
522
523 _health = INT32_MAX;
524
525 async_await RequestMetadataAsync("Boss/Devan"_s);
526 SetAnimation(DevanBullet);
527
528 async_return true;
529 }
530
531 void Devan::Bullet::OnUpdateHitbox()
532 {
533 UpdateHitbox(6, 6);
534 }
535
536 void Devan::Bullet::OnEmitLights(SmallVectorImpl<LightEmitter>& lights)
537 {
538 auto& light = lights.emplace_back();
539 light.Pos = _pos;
540 light.Intensity = 0.8f;
541 light.Brightness = 0.8f;
542 light.RadiusNear = 0.0f;
543 light.RadiusFar = 28.0f;
544 }
545
546 bool Devan::Bullet::OnPerish(ActorBase* collider)
547 {
548 Explosion::Create(_levelHandler, Vector3i((std::int32_t)(_pos.X + _speed.X), (std::int32_t)(_pos.Y + _speed.Y), _renderer.layer() + 2), Explosion::Type::Small);
549
550 return EnemyBase::OnPerish(collider);
551 }
552
553 void Devan::Bullet::OnHitFloor(float timeMult)
554 {
555 PlaySfx("WallPoof"_s);
556 DecreaseHealth(INT32_MAX);
557 }
558
559 void Devan::Bullet::OnHitWall(float timeMult)
560 {
561 PlaySfx("WallPoof"_s);
562 DecreaseHealth(INT32_MAX);
563 }
564
565 void Devan::Bullet::OnHitCeiling(float timeMult)
566 {
567 PlaySfx("WallPoof"_s);
568 DecreaseHealth(INT32_MAX);
569 }
570
571 Devan::Fireball::Fireball()
572 {
573 }
574
575 Task<bool> Devan::Fireball::OnActivatedAsync(const ActorActivationDetails& details)
576 {
577 SetFacingLeft(details.Params[0] != 0);
578 _speed.X = (IsFacingLeft() ? -5.0f : 5.0f);
579 _speed.Y = 3.5f;
580 _timeLeft = 50.0f;
581
582 SetState(ActorState::IsInvulnerable, true);
583 SetState(ActorState::CanBeFrozen | ActorState::ApplyGravitation, false);
584
585 _health = INT32_MAX;
586
587 async_await RequestMetadataAsync("Boss/Devan"_s);
588 SetAnimation(DemonFireball);
589
590 async_return true;
591 }
592
593 void Devan::Fireball::OnUpdateHitbox()
594 {
595 UpdateHitbox(6, 6);
596 }
597
598 void Devan::Fireball::OnEmitLights(SmallVectorImpl<LightEmitter>& lights)
599 {
600 auto& light = lights.emplace_back();
601 light.Pos = _pos;
602 light.Intensity = 0.85f;
603 light.Brightness = 0.4f;
604 light.RadiusNear = 0.0f;
605 light.RadiusFar = 30.0f;
606 }
607
608 bool Devan::Fireball::OnPerish(ActorBase* collider)
609 {
610 Explosion::Create(_levelHandler, Vector3i((std::int32_t)(_pos.X + _speed.X), (std::int32_t)(_pos.Y + _speed.Y), _renderer.layer() + 2), Explosion::Type::SmallDark);
611
612 PlaySfx("Flap"_s);
613
614 return EnemyBase::OnPerish(collider);
615 }
616
617 void Devan::Fireball::OnHitFloor(float timeMult)
618 {
619 DecreaseHealth(INT32_MAX);
620 }
621
622 void Devan::Fireball::OnHitWall(float timeMult)
623 {
624 DecreaseHealth(INT32_MAX);
625 }
626
627 void Devan::Fireball::OnHitCeiling(float timeMult)
628 {
629 DecreaseHealth(INT32_MAX);
630 }
631 }