Debian Codegraph @

Sources/Jazz2/Actors/Environment/Copter.cpp

raw
1 #include "Copter.h"
2 #include "../../ILevelHandler.h"
3 #include "../Player.h"
4
5 namespace Jazz2::Actors::Environment
6 {
7 Copter::Copter()
8 : _phase(0.0f), _state(State::Free)
9 {
10 }
11
12 Copter::~Copter()
13 {
14 #if defined(WITH_AUDIO)
15 if (_noise != nullptr) {
16 _noise->stop();
17 _noise = nullptr;
18 }
19 #endif
20 }
21
22 Task<bool> Copter::OnActivatedAsync(const ActorActivationDetails& details)
23 {
24 _state = (details.Params[0] != 0 ? State::Mounted : State::Free);
25
26 SetState(ActorState::CanBeFrozen | ActorState::ApplyGravitation, false);
27
28 async_await RequestMetadataAsync("Enemy/LizardFloat"_s);
29
30 SetAnimation((AnimState)1);
31
32 _originPos = _pos;
33
34 async_return true;
35 }
36
37 void Copter::OnUpdate(float timeMult)
38 {
39 switch (_state) {
40 case State::Free: {
41 _phase += timeMult * 0.05f;
42 MoveInstantly(_originPos + Vector2f(0.0f, sinf(_phase) * 4.0f), MoveType::Absolute);
43 OnUpdateHitbox();
44 break;
45 }
46 case State::Unmounted: {
47 ActorBase::OnUpdate(timeMult);
48
49 _speed.Y = _levelHandler->GetGravity() * -0.5f;
50 _renderer.setAlphaF(_renderer.alpha() - (0.004f * timeMult));
51 _phase -= timeMult;
52 if (_phase <= 0.0f) {
53 DecreaseHealth(INT32_MAX);
54 }
55 break;
56 }
57 }
58
59 #if defined(WITH_AUDIO)
60 if (_noise != nullptr) {
61 float newGain = _noise->gain() - (_noiseDec * timeMult);
62 if (newGain <= 0.0f) {
63 _noise->stop();
64 _noise = nullptr;
65 } else {
66 _noise->setGain(newGain);
67 _noise->setPosition(Vector3f(_pos.X, _pos.Y, 0.8f));
68 }
69 }
70 #endif
71 }
72
73 void Copter::OnDetach(ActorBase* parent)
74 {
75 DecreaseHealth(INT32_MAX);
76 }
77
78 void Copter::Preload(const ActorActivationDetails& details)
79 {
80 PreloadMetadataAsync("Enemy/LizardFloat"_s);
81 }
82
83 bool Copter::OnHandleCollision(std::shared_ptr<ActorBase> other)
84 {
85 if (_state == State::Free || _state == State::Unmounted) {
86 if (auto* player = runtime_cast<Player>(other.get())) {
87 if (player->GetModifier() == Player::Modifier::None && player->SetModifier(Player::Modifier::LizardCopter, shared_from_this())) {
88 _state = State::Mounted;
89 _renderer.setAlphaF(1.0f);
90
91 PlaySfx("CopterPre"_s);
92 return true;
93 }
94 }
95 }
96
97 return ActorBase::OnHandleCollision(std::move(other));
98 }
99
100 void Copter::Unmount(float timeLeft)
101 {
102 if (_state == State::Mounted) {
103 _state = State::Unmounted;
104 _phase = timeLeft;
105
106 #if defined(WITH_AUDIO)
107 _noise = PlaySfx("Copter"_s, 0.8f, 0.8f);
108 if (_noise != nullptr) {
109 _noise->setLooping(true);
110 _noiseDec = _noise->gain() * 0.005f;
111 }
112 #endif
113
114 SetState(ActorState::ApplyGravitation, true);
115 }
116 }
117 }