| 1 |
#include "SwingingVine.h" |
| 2 |
#include "../../ContentResolver.h" |
| 3 |
#include "../../ILevelHandler.h" |
| 4 |
#include "../../Events/EventMap.h" |
| 5 |
#include "../Player.h" |
| 6 |
|
| 7 |
#include "../../../nCine/Graphics/RenderQueue.h" |
| 8 |
|
| 9 |
namespace Jazz2::Actors::Environment |
| 10 |
{ |
| 11 |
SwingingVine::SwingingVine() |
| 12 |
: _angle(0.0f), _phase(0.0f), _justTurned(false) |
| 13 |
{ |
| 14 |
} |
| 15 |
|
| 16 |
SwingingVine::~SwingingVine() |
| 17 |
{ |
| 18 |
auto players = _levelHandler->GetPlayers(); |
| 19 |
for (auto* player : players) { |
| 20 |
player->CancelCarryingObject(this); |
| 21 |
} |
| 22 |
} |
| 23 |
|
| 24 |
Task<bool> SwingingVine::OnActivatedAsync(const ActorActivationDetails& details) |
| 25 |
{ |
| 26 |
SetState(ActorState::SkipPerPixelCollisions, true); |
| 27 |
SetState(ActorState::CanBeFrozen | ActorState::CollideWithTileset | ActorState::ApplyGravitation, false); |
| 28 |
|
| 29 |
async_await RequestMetadataAsync("Object/SwingingVine"_s); |
| 30 |
|
| 31 |
SetAnimation(AnimState::Default); |
| 32 |
|
| 33 |
_phase = _originTile.X + _originTile.Y * 0.5f; |
| 34 |
|
| 35 |
_renderer.AnimPaused = true; |
| 36 |
|
| 37 |
auto& resolver = ContentResolver::Get(); |
| 38 |
if (!resolver.IsHeadless()) { |
| 39 |
if (_currentAnimation != nullptr) { |
| 40 |
_currentAnimation->Base->TextureDiffuse->SetWrap(SamplerWrapping::Repeat); |
| 41 |
} |
| 42 |
|
| 43 |
for (std::int32_t i = 0; i < ChunkCount; i++) { |
| 44 |
_chunks[i] = std::make_unique<RenderCommand>(RenderCommand::Type::Sprite); |
| 45 |
_chunks[i]->GetMaterial().SetShaderProgramType(Material::ShaderProgramType::Sprite); |
| 46 |
_chunks[i]->GetMaterial().SetBlendingEnabled(true); |
| 47 |
_chunks[i]->GetMaterial().ReserveUniformsDataMemory(); |
| 48 |
_chunks[i]->GetGeometry().SetDrawParameters(GL_TRIANGLE_STRIP, 0, 4); |
| 49 |
_chunks[i]->GetMaterial().SetBlendingFactors(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); |
| 50 |
|
| 51 |
auto* textureUniform = _chunks[i]->GetMaterial().Uniform(Material::TextureUniformName); |
| 52 |
if (textureUniform && textureUniform->GetIntValue(0) != 0) { |
| 53 |
textureUniform->SetIntValue(0); // GL_TEXTURE0 |
| 54 |
} |
| 55 |
} |
| 56 |
} |
| 57 |
|
| 58 |
async_return true; |
| 59 |
} |
| 60 |
|
| 61 |
void SwingingVine::OnUpdate(float timeMult) |
| 62 |
{ |
| 63 |
ActorBase::OnUpdate(timeMult); |
| 64 |
|
| 65 |
float currentPhase = _phase + 0.04f * _levelHandler->GetElapsedFrames(); |
| 66 |
for (std::int32_t i = 0; i < ChunkCount; i++) { |
| 67 |
_angle = sinf(currentPhase - i * (0.64f / ChunkCount)) * 1.2f + fPiOver2; |
| 68 |
|
| 69 |
float distance = ChunkSize * powf(i, 0.95f); |
| 70 |
_chunkPos[i].X = _pos.X + cosf(_angle) * distance; |
| 71 |
_chunkPos[i].Y = _pos.Y + sinf(_angle) * distance; |
| 72 |
} |
| 73 |
|
| 74 |
auto& lastChunk = _chunkPos[ChunkCount - 1]; |
| 75 |
AABBInner = AABBf(lastChunk.X - 10.0f, lastChunk.Y - 10.0f, lastChunk.X + 10.0f, lastChunk.Y + 10.0f); |
| 76 |
|
| 77 |
auto players = _levelHandler->GetPlayers(); |
| 78 |
for (auto* player : players) { |
| 79 |
if (player->GetCarryingObject() == this) { |
| 80 |
float chunkAngle = sinf(currentPhase - ChunkCount * 0.08f) * 0.6f; |
| 81 |
Vector2f prevPos = player->GetPos(); |
| 82 |
Vector2 newPos = lastChunk + Vector2(chunkAngle * -22.0f, 20.0f + std::abs(chunkAngle) * -10.0f); |
| 83 |
player->MoveInstantly(newPos, MoveType::Absolute); |
| 84 |
|
| 85 |
if (_justTurned) { |
| 86 |
// TODO: `SwingingVine::OnUpdate()` is called after `Player::OnUpdate()`, so this must be called one frame later |
| 87 |
player->SetFacingLeft(!player->IsFacingLeft()); |
| 88 |
_justTurned = false; |
| 89 |
} else if (player->IsFacingLeft()) { |
| 90 |
if (newPos.X > prevPos.X) { |
| 91 |
player->_renderer.AnimTime = 0.0f; |
| 92 |
_justTurned = true; |
| 93 |
} |
| 94 |
} else { |
| 95 |
if (newPos.X < prevPos.X) { |
| 96 |
player->_renderer.AnimTime = 0.0f; |
| 97 |
_justTurned = true; |
| 98 |
} |
| 99 |
} |
| 100 |
|
| 101 |
player->_renderer.setRotation(chunkAngle); |
| 102 |
} |
| 103 |
} |
| 104 |
|
| 105 |
SetState(ActorState::IsDirty, true); |
| 106 |
} |
| 107 |
|
| 108 |
void SwingingVine::OnUpdateHitbox() |
| 109 |
{ |
| 110 |
} |
| 111 |
|
| 112 |
bool SwingingVine::OnDraw(RenderQueue& renderQueue) |
| 113 |
{ |
| 114 |
if (_currentAnimation != nullptr) { |
| 115 |
auto& resBase = _currentAnimation->Base; |
| 116 |
Vector2i texSize = resBase->TextureDiffuse->GetSize(); |
| 117 |
float currentPhase = _phase + 0.04f * _levelHandler->GetElapsedFrames(); |
| 118 |
|
| 119 |
for (std::int32_t i = 0; i < ChunkCount; i++) { |
| 120 |
auto command = _chunks[i].get(); |
| 121 |
|
| 122 |
float chunkTexSize = ChunkSize / texSize.Y; |
| 123 |
float chunkAngle = sinf(currentPhase - i * 0.08f) * 1.2f; |
| 124 |
|
| 125 |
auto instanceBlock = command->GetMaterial().UniformBlock(Material::InstanceBlockName); |
| 126 |
instanceBlock->GetUniform(Material::TexRectUniformName)->SetFloatValue(1.0f, 0.0f, chunkTexSize, chunkTexSize * i); |
| 127 |
instanceBlock->GetUniform(Material::SpriteSizeUniformName)->SetFloatValue(texSize.X, ChunkSize); |
| 128 |
instanceBlock->GetUniform(Material::ColorUniformName)->SetFloatVector(Colorf::White.Data()); |
| 129 |
|
| 130 |
Matrix4x4f worldMatrix = Matrix4x4f::Translation(_chunkPos[i].X - texSize.X / 2, _chunkPos[i].Y - ChunkSize / 2, 0.0f); |
| 131 |
worldMatrix.RotateZ(chunkAngle); |
| 132 |
command->SetTransformation(worldMatrix); |
| 133 |
command->SetLayer(_renderer.layer()); |
| 134 |
command->GetMaterial().SetTexture(*resBase->TextureDiffuse); |
| 135 |
|
| 136 |
renderQueue.AddCommand(command); |
| 137 |
} |
| 138 |
} |
| 139 |
|
| 140 |
return true; |
| 141 |
} |
| 142 |
|
| 143 |
bool SwingingVine::OnHandleCollision(std::shared_ptr<ActorBase> other) |
| 144 |
{ |
| 145 |
if (auto* player = runtime_cast<Player>(other.get())) { |
| 146 |
if (player->_springCooldown <= 0.0f) { |
| 147 |
player->UpdateCarryingObject(this, SuspendType::SwingingVine); |
| 148 |
} |
| 149 |
return true; |
| 150 |
} |
| 151 |
|
| 152 |
return false; |
| 153 |
} |
| 154 |
|
| 155 |
void SwingingVine::Preload(const ActorActivationDetails& details) |
| 156 |
{ |
| 157 |
PreloadMetadataAsync("Object/SwingingVine"_s); |
| 158 |
} |
| 159 |
} |