Debian Codegraph @

Sources/Jazz2/Actors/Solid/Pole.cpp

raw
1 #include "Pole.h"
2 #include "../../ILevelHandler.h"
3 #include "../Weapons/ShotBase.h"
4 #include "../Weapons/FreezerShot.h"
5 #include "../Weapons/Thunderbolt.h"
6 #include "../Weapons/TNT.h"
7
8 using namespace Jazz2::Tiles;
9
10 namespace Jazz2::Actors::Solid
11 {
12 Pole::Pole()
13 : _fall(FallDirection::None), _angleVel(0.0f), _angleVelLast(0.0f), _fallTime(0.0f), _bouncesLeft(BouncesMax)
14 {
15 }
16
17 void Pole::Preload(const ActorActivationDetails& details)
18 {
19 std::uint8_t theme = details.Params[0];
20 switch (theme) {
21 default:
22 case 0: PreloadMetadataAsync("Pole/Carrotus"_s); break;
23 case 1: PreloadMetadataAsync("Pole/Diamondus"_s); break;
24 case 2: PreloadMetadataAsync("Pole/DiamondusTree"_s); break;
25 case 3: PreloadMetadataAsync("Pole/Jungle"_s); break;
26 case 4: PreloadMetadataAsync("Pole/Psych"_s); break;
27 }
28 }
29
30 Task<bool> Pole::OnActivatedAsync(const ActorActivationDetails& details)
31 {
32 std::uint8_t theme = details.Params[0];
33 std::int16_t x = *(std::int16_t*)&details.Params[1];
34 std::int16_t y = *(std::int16_t*)&details.Params[3];
35
36 _pos.X += x;
37 _pos.Y += y;
38 _renderer.setLayer(_renderer.layer() - 20);
39
40 SetState(ActorState::CanBeFrozen | ActorState::ApplyGravitation, false);
41
42 bool isSolid = true;
43 switch (theme) {
44 default:
45 case 0: async_await RequestMetadataAsync("Pole/Carrotus"_s); break;
46 case 1: async_await RequestMetadataAsync("Pole/Diamondus"_s); break;
47 case 2: async_await RequestMetadataAsync("Pole/DiamondusTree"_s); isSolid = false; break;
48 case 3: async_await RequestMetadataAsync("Pole/Jungle"_s); break;
49 case 4: async_await RequestMetadataAsync("Pole/Psych"_s); break;
50 }
51
52 if (isSolid) {
53 SetState(ActorState::IsSolidObject, true);
54 }
55
56 SetAnimation(AnimState::Default);
57
58 async_return true;
59 }
60
61 void Pole::OnUpdate(float timeMult)
62 {
63 constexpr float FallMultiplier = 0.0036f;
64 constexpr float Bounce = -0.2f;
65
66 ActorBase::OnUpdate(timeMult);
67
68 if (_fall != FallDirection::Left && _fall != FallDirection::Right) {
69 return;
70 }
71
72 _fallTime += timeMult;
73
74 if (_fall == FallDirection::Right) {
75 if (_angleVel > 0 && IsPositionBlocked()) {
76 if (_bouncesLeft > 0) {
77 if (_bouncesLeft == BouncesMax) {
78 _angleVelLast = _angleVel;
79 PlaySfx("FallEnd"_s, 0.8f);
80 }
81
82 _bouncesLeft--;
83 _angleVel = Bounce * _bouncesLeft * _angleVelLast;
84 } else {
85 _fall = FallDirection::Fallen;
86 if (_fallTime < 10.0f) {
87 SetState(ActorState::IsSolidObject, false);
88 }
89 }
90 } else {
91 _angleVel += FallMultiplier * timeMult;
92 _renderer.setRotation(_renderer.rotation() + _angleVel * timeMult);
93 SetState(ActorState::IsDirty, true);
94 }
95 } else if (_fall == FallDirection::Left) {
96 if (_angleVel < 0 && IsPositionBlocked()) {
97 if (_bouncesLeft > 0) {
98 if (_bouncesLeft == BouncesMax) {
99 _angleVelLast = _angleVel;
100 PlaySfx("FallEnd"_s, 0.8f);
101 }
102
103 _bouncesLeft--;
104 _angleVel = Bounce * _bouncesLeft * _angleVelLast;
105 } else {
106 _fall = FallDirection::Fallen;
107 if (_fallTime < 10.0f) {
108 SetState(ActorState::IsSolidObject, false);
109 }
110 }
111 } else {
112 _angleVel -= FallMultiplier * timeMult;
113 _renderer.setRotation(_renderer.rotation() + _angleVel * timeMult);
114 SetState(ActorState::IsDirty, true);
115 }
116 }
117 }
118
119 void Pole::OnPacketReceived(MemoryStream& packet)
120 {
121 FallDirection fall = (FallDirection)packet.ReadValue<std::uint8_t>();
122 Fall(fall);
123 }
124
125 bool Pole::OnHandleCollision(std::shared_ptr<ActorBase> other)
126 {
127 if (auto* shotBase = runtime_cast<Weapons::ShotBase>(other.get())) {
128 if (shotBase->GetStrength() > 0) {
129 FallDirection fallDirection;
130 if (auto* thunderbolt = runtime_cast<Weapons::Thunderbolt>(shotBase)) {
131 fallDirection = (_pos.X < shotBase->GetPos().X ? FallDirection::Left : FallDirection::Right);
132 } else {
133 fallDirection = (shotBase->GetSpeed().X < 0.0f ? FallDirection::Left : FallDirection::Right);
134 }
135 Fall(fallDirection);
136 shotBase->DecreaseHealth(1);
137 return true;
138 } else if (auto* freezerShot = runtime_cast<Weapons::FreezerShot>(shotBase)) {
139 shotBase->DecreaseHealth(INT32_MAX);
140 return true;
141 }
142 } else if (auto* tnt = runtime_cast<Weapons::TNT>(other.get())) {
143 Fall(tnt->GetPos().X > _pos.X ? FallDirection::Left : FallDirection::Right);
144 return true;
145 }
146
147 return false;
148 }
149
150 bool Pole::CanCauseDamage(ActorBase* collider)
151 {
152 return _levelHandler->IsReforged() || runtime_cast<Weapons::TNT>(collider);
153 }
154
155 void Pole::Fall(FallDirection dir)
156 {
157 if (_fall != FallDirection::None) {
158 return;
159 }
160
161 _fall = dir;
162 SetState(ActorState::IsInvulnerable | ActorState::IsSolidObject, true);
163 PlaySfx("FallStart"_s, 0.6f);
164
165 SendPacket(arrayView({ (std::uint8_t)_fall }));
166 }
167
168 bool Pole::IsPositionBlocked()
169 {
170 constexpr float Ratio1 = 0.96f;
171 constexpr float Ratio2 = 0.8f;
172 constexpr float Ratio3 = 0.6f;
173 constexpr float Ratio4 = 0.3f;
174
175 float angle = _renderer.rotation() - fPiOver2;
176 float rx = cosf(angle);
177 float ry = sinf(angle);
178 float radius = (float)_currentAnimation->Base->FrameDimensions.Y;
179 TileCollisionParams params = { TileDestructType::None, true };
180
181 if (_fallTime > 20) {
182 // Check radius 1
183 {
184 float x = _pos.X + (rx * Ratio1 * radius);
185 float y = _pos.Y + (ry * Ratio1 * radius);
186 AABBf aabb = AABBf(x - 3, y - 3, x + 7, y + 7);
187 if (!_levelHandler->IsPositionEmpty(this, aabb, params)) {
188 return true;
189 }
190 }
191 // Check radius 2
192 {
193 float x = _pos.X + (rx * Ratio2 * radius);
194 float y = _pos.Y + (ry * Ratio2 * radius);
195 AABBf aabb = AABBf(x - 3, y - 3, x + 7, y + 7);
196 if (!_levelHandler->IsPositionEmpty(this, aabb, params)) {
197 return true;
198 }
199 }
200 }
201 // Check radius 3
202 {
203 float x = _pos.X + (rx * Ratio3 * radius);
204 float y = _pos.Y + (ry * Ratio3 * radius);
205 AABBf aabb = AABBf(x - 3, y - 3, x + 7, y + 7);
206 if (!_levelHandler->IsPositionEmpty(this, aabb, params)) {
207 return true;
208 }
209 }
210 // Check radius 4
211 {
212 float x = _pos.X + (rx * Ratio4 * radius);
213 float y = _pos.Y + (ry * Ratio4 * radius);
214 AABBf aabb = AABBf(x - 3, y - 3, x + 7, y + 7);
215 if (!_levelHandler->IsPositionEmpty(this, aabb, params)) {
216 return true;
217 }
218 }
219
220 return false;
221 }
222 }