Debian Codegraph @

Sources/Jazz2/Actors/Solid/MovingPlatform.cpp

raw
1 #include "MovingPlatform.h"
2 #include "../../ContentResolver.h"
3 #include "../../ILevelHandler.h"
4 #include "../../Tiles/TileMap.h"
5 #include "../Player.h"
6 #include "../Weapons/ShotBase.h"
7
8 #include "../../../nCine/Graphics/RenderQueue.h"
9
10 namespace Jazz2::Actors::Solid
11 {
12 MovingPlatform::MovingPlatform()
13 {
14 }
15
16 MovingPlatform::~MovingPlatform()
17 {
18 auto players = _levelHandler->GetPlayers();
19 for (auto* player : players) {
20 player->CancelCarryingObject(this);
21 }
22 }
23
24 void MovingPlatform::Preload(const ActorActivationDetails& details)
25 {
26 PlatformType platformType = (PlatformType)details.Params[0];
27 switch (platformType) {
28 default:
29 case PlatformType::CarrotusFruit: PreloadMetadataAsync("MovingPlatform/CarrotusFruit"_s); break;
30 case PlatformType::Ball: PreloadMetadataAsync("MovingPlatform/Ball"_s); break;
31 case PlatformType::CarrotusGrass: PreloadMetadataAsync("MovingPlatform/CarrotusGrass"_s); break;
32 case PlatformType::Lab: PreloadMetadataAsync("MovingPlatform/Lab"_s); break;
33 case PlatformType::Sonic: PreloadMetadataAsync("MovingPlatform/Sonic"_s); break;
34 case PlatformType::Spike: PreloadMetadataAsync("MovingPlatform/Spike"_s); break;
35 case PlatformType::SpikeBall: PreloadMetadataAsync("MovingPlatform/SpikeBall"_s); break;
36 }
37 }
38
39 Task<bool> MovingPlatform::OnActivatedAsync(const ActorActivationDetails& details)
40 {
41 _type = (PlatformType)details.Params[0];
42
43 std::uint8_t length = details.Params[3];
44 _speed = *(std::int8_t*)&details.Params[2] * 0.0072f;
45 std::uint8_t sync = details.Params[1];
46 _isSwing = details.Params[4] != 0;
47 _phase = sync * fPiOver2 - _speed * _levelHandler->GetElapsedFrames();
48
49 _originPos = _pos;
50 _lastPos = _originPos;
51
52 IsOneWay = true;
53 SetState(ActorState::CollideWithTileset | ActorState::IsSolidObject | ActorState::ApplyGravitation, false);
54
55 switch (_type) {
56 default:
57 case PlatformType::CarrotusFruit: async_await RequestMetadataAsync("MovingPlatform/CarrotusFruit"_s); break;
58 case PlatformType::Ball: async_await RequestMetadataAsync("MovingPlatform/Ball"_s); break;
59 case PlatformType::CarrotusGrass: async_await RequestMetadataAsync("MovingPlatform/CarrotusGrass"_s); break;
60 case PlatformType::Lab: async_await RequestMetadataAsync("MovingPlatform/Lab"_s); break;
61 case PlatformType::Sonic: async_await RequestMetadataAsync("MovingPlatform/Sonic"_s); break;
62 case PlatformType::Spike: async_await RequestMetadataAsync("MovingPlatform/Spike"_s); break;
63 case PlatformType::SpikeBall:
64 async_await RequestMetadataAsync("MovingPlatform/SpikeBall"_s);
65 _maxHealth = _health = 8;
66 break;
67 }
68
69 SetAnimation((AnimState)0);
70
71 auto& resolver = ContentResolver::Get();
72 for (std::int32_t i = 0; i < length; i++) {
73 ChainPiece& piece = _pieces.emplace_back();
74 if (!resolver.IsHeadless()) {
75 piece.Command = std::make_unique<RenderCommand>(RenderCommand::Type::Sprite);
76 piece.Command->GetMaterial().SetShaderProgramType(Material::ShaderProgramType::Sprite);
77 piece.Command->GetMaterial().SetBlendingEnabled(true);
78 piece.Command->GetMaterial().ReserveUniformsDataMemory();
79 piece.Command->GetGeometry().SetDrawParameters(GL_TRIANGLE_STRIP, 0, 4);
80
81 auto* textureUniform = piece.Command->GetMaterial().Uniform(Material::TextureUniformName);
82 if (textureUniform && textureUniform->GetIntValue(0) != 0) {
83 textureUniform->SetIntValue(0); // GL_TEXTURE0
84 }
85 }
86 }
87
88 async_return true;
89 }
90
91 void MovingPlatform::OnUpdate(float timeMult)
92 {
93 UpdateFrozenState(timeMult);
94
95 _lastPos = _pos;
96 if (_frozenTimeLeft <= 0.0f) {
97 _phase -= _speed * timeMult;
98 if (_speed > 0.0f) {
99 if (_phase < -fTwoPi) {
100 _phase += fTwoPi;
101 }
102 } else if (_speed < 0.0f) {
103 if (_phase > fTwoPi) {
104 _phase -= fTwoPi;
105 }
106 }
107
108 MoveInstantly(GetPhasePosition((std::int32_t)_pieces.size()), MoveType::Absolute | MoveType::Force);
109
110 for (int i = 0; i < _pieces.size(); i++) {
111 _pieces[i].Pos = GetPhasePosition(i);
112 }
113 }
114
115 Vector2f diff = _pos - _lastPos;
116 AABBf aabb = AABBInner;
117 aabb.T -= 4.0f;
118
119 if (_type != PlatformType::SpikeBall) {
120 auto players = _levelHandler->GetPlayers();
121 for (auto* player : players) {
122 if (player->GetCarryingObject() == this) {
123 AABBf aabb3 = aabb;
124 aabb3.T -= 4.0f;
125 if (aabb3.Overlaps(player->AABBInner) && player->GetState(ActorState::ApplyGravitation)) {
126 // Try to adjust Y, because it collides with carrying platform sometimes
127 bool success = false;
128 Vector2f playerPos = player->GetPos();
129 Vector2f adjustedPos = Vector2f(playerPos.X + diff.X, playerPos.Y + diff.Y);
130 for (std::int32_t i = 0; i < 8; i++) {
131 if (player->MoveInstantly(adjustedPos, MoveType::Absolute)) {
132 success = true;
133 break;
134 }
135 adjustedPos.Y -= 0.5f * timeMult;
136 }
137
138 if (!success) {
139 player->MoveInstantly(Vector2f(0.0f, diff.Y), MoveType::Relative);
140 }
141
142 player->UpdateCarryingObject(this);
143 } else {
144 player->CancelCarryingObject();
145 SetState(ActorState::IsSolidObject, false);
146 }
147 } else if (aabb.Overlaps(player->AABBInner) && player->GetSpeed().Y >= diff.Y * timeMult && !player->CanMoveVertically()) {
148 player->UpdateCarryingObject(this);
149 SetState(ActorState::IsSolidObject, true);
150 }
151 }
152
153 if (_type == PlatformType::Spike) {
154 AABBf aabb2 = aabb;
155 aabb2.T += 40.0f;
156 aabb2.B += 20.0f;
157
158 _levelHandler->GetCollidingPlayers(aabb2, [](Actors::ActorBase* actor) {
159 if (auto* player = runtime_cast<Player>(actor)) {
160 if (player->GetSpeed().Y < 0.0f) {
161 player->TakeDamage(1, 2.0f);
162 }
163 }
164 return true;
165 });
166 }
167 } else {
168 _levelHandler->GetCollidingPlayers(aabb, [](Actors::ActorBase* actor) {
169 if (auto* player = runtime_cast<Player>(actor)) {
170 player->TakeDamage(1, 2.0f);
171 }
172 return true;
173 });
174 }
175 }
176
177 bool MovingPlatform::OnHandleCollision(std::shared_ptr<ActorBase> other)
178 {
179 if (_type == PlatformType::SpikeBall && _health > 0) {
180 if (auto* shotBase = runtime_cast<Weapons::ShotBase>(other.get())) {
181 if (shotBase->GetStrength() > 0) {
182 DecreaseHealth(shotBase->GetStrength(), shotBase);
183 shotBase->DecreaseHealth(INT32_MAX);
184 return true;
185 }
186 }
187 }
188
189 return SolidObjectBase::OnHandleCollision(std::move(other));
190 }
191
192 bool MovingPlatform::OnPerish(ActorBase* collider)
193 {
194 CreateParticleDebrisOnPerish(ParticleDebrisEffect::Standard, Vector2f::Zero);
195
196 return SolidObjectBase::OnPerish(collider);
197 }
198
199 void MovingPlatform::OnUpdateHitbox()
200 {
201 SolidObjectBase::OnUpdateHitbox();
202
203 switch (_type) {
204 case PlatformType::CarrotusFruit:
205 AABBInner.L -= 10.0f;
206 AABBInner.T -= 14.0f;
207 AABBInner.R += 10.0f;
208 AABBInner.B = AABBInner.T + 16.0f;
209 break;
210 case PlatformType::Lab:
211 AABBInner.L -= 18.0f;
212 AABBInner.T -= 2.0f;
213 AABBInner.R += 18.0f;
214 AABBInner.B = AABBInner.T + 16.0f;
215 break;
216 case PlatformType::Sonic:
217 AABBInner.L -= 14.0f;
218 AABBInner.T -= 16.0f;
219 AABBInner.R += 14.0f;
220 AABBInner.B = AABBInner.T + 16.0f;
221 break;
222 case PlatformType::Spike:
223 AABBInner.L -= 16.0f;
224 AABBInner.T -= 30.0f;
225 AABBInner.R += 16.0f;
226 AABBInner.B = AABBInner.T + 20.0f;
227 break;
228 case PlatformType::SpikeBall:
229 AABBInner.L -= 8.0f;
230 AABBInner.T -= 8.0f;
231 AABBInner.R += 8.0f;
232 AABBInner.B += 8.0f;
233 break;
234 default:
235 AABBInner.L -= 8.0f;
236 AABBInner.T -= 10.0f;
237 AABBInner.R += 8.0f;
238 AABBInner.B = AABBInner.T + 16.0f;
239 break;
240 }
241 }
242
243 bool MovingPlatform::OnDraw(RenderQueue& renderQueue)
244 {
245 if (!_pieces.empty()) {
246 auto* chainAnim = _metadata->FindAnimation((AnimState)1); // Chain
247 if (chainAnim != nullptr && chainAnim->Base->TextureDiffuse != nullptr) {
248 Vector2i texSize = chainAnim->Base->TextureDiffuse->GetSize();
249
250 for (std::int32_t i = 0; i < (std::int32_t)_pieces.size(); i++) {
251 auto* command = _pieces[i].Command.get();
252
253 std::int32_t curAnimFrame = chainAnim->FrameOffset + (i % chainAnim->FrameCount);
254 std::int32_t col = curAnimFrame % chainAnim->Base->FrameConfiguration.X;
255 std::int32_t row = curAnimFrame / chainAnim->Base->FrameConfiguration.X;
256 float texScaleX = (float(chainAnim->Base->FrameDimensions.X) / float(texSize.X));
257 float texBiasX = (float(chainAnim->Base->FrameDimensions.X * col) / float(texSize.X));
258 float texScaleY = (float(chainAnim->Base->FrameDimensions.Y) / float(texSize.Y));
259 float texBiasY = (float(chainAnim->Base->FrameDimensions.Y * row) / float(texSize.Y));
260
261 auto* instanceBlock = command->GetMaterial().UniformBlock(Material::InstanceBlockName);
262 instanceBlock->GetUniform(Material::TexRectUniformName)->SetFloatValue(texScaleX, texBiasX, texScaleY, texBiasY);
263 instanceBlock->GetUniform(Material::SpriteSizeUniformName)->SetFloatValue((float)chainAnim->Base->FrameDimensions.X, (float)chainAnim->Base->FrameDimensions.Y);
264 instanceBlock->GetUniform(Material::ColorUniformName)->SetFloatVector(Colorf::White.Data());
265
266 auto pos = _pieces[i].Pos;
267 command->SetTransformation(Matrix4x4f::Translation(pos.X - chainAnim->Base->FrameDimensions.X / 2, pos.Y - chainAnim->Base->FrameDimensions.Y / 2, 0.0f));
268 command->SetLayer(_renderer.layer() - 2);
269 command->GetMaterial().SetTexture(*chainAnim->Base->TextureDiffuse.get());
270
271 renderQueue.AddCommand(command);
272 }
273 }
274 }
275
276 return SolidObjectBase::OnDraw(renderQueue);
277 }
278
279 Vector2f MovingPlatform::GetPhasePosition(std::int32_t distance)
280 {
281 float effectivePhase = _phase;
282
283 if (_isSwing) {
284 effectivePhase = fPiOver2 + sinf(effectivePhase) * fPiOver2;
285 } else if (_pieces.size() > 4 && distance > 0 && distance < _pieces.size()) {
286 float shift = sinf(fPi * (float)(distance + 1) / _pieces.size()) * _speed * 4.0f;
287 effectivePhase -= shift;
288 }
289
290 float multiX = cosf(effectivePhase);
291 float multiY = sinf(effectivePhase);
292
293 return Vector2f(
294 std::round(_originPos.X + multiX * distance * 12.0f),
295 std::round(_originPos.Y + multiY * distance * 12.0f)
296 );
297 }
298 }