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