| 1 |
#include "Bridge.h" |
| 2 |
#include "../../ContentResolver.h" |
| 3 |
#include "../../ILevelHandler.h" |
| 4 |
#include "../../Tiles/TileMap.h" |
| 5 |
#include "../Player.h" |
| 6 |
|
| 7 |
#include "../../../nCine/Graphics/RenderQueue.h" |
| 8 |
|
| 9 |
namespace Jazz2::Actors::Solid |
| 10 |
{ |
| 11 |
Bridge::Bridge() |
| 12 |
: _widths(nullptr), _widthsCount(0), _widthOffset(0), _leftX(0.0f), _leftHeight(0.0f), _rightX(0.0f), _rightHeight(0.0f) |
| 13 |
{ |
| 14 |
} |
| 15 |
|
| 16 |
Bridge::~Bridge() |
| 17 |
{ |
| 18 |
auto players = _levelHandler->GetPlayers(); |
| 19 |
for (auto* player : players) { |
| 20 |
player->CancelCarryingObject(this); |
| 21 |
} |
| 22 |
} |
| 23 |
|
| 24 |
void Bridge::Preload(const ActorActivationDetails& details) |
| 25 |
{ |
| 26 |
BridgeType bridgeType = (BridgeType)details.Params[2]; |
| 27 |
switch (bridgeType) { |
| 28 |
default: |
| 29 |
case BridgeType::Rope: PreloadMetadataAsync("Bridge/Rope"_s); break; |
| 30 |
case BridgeType::Stone: PreloadMetadataAsync("Bridge/Stone"_s); break; |
| 31 |
case BridgeType::Vine: PreloadMetadataAsync("Bridge/Vine"_s); break; |
| 32 |
case BridgeType::StoneRed: PreloadMetadataAsync("Bridge/StoneRed"_s); break; |
| 33 |
case BridgeType::Log: PreloadMetadataAsync("Bridge/Log"_s); break; |
| 34 |
case BridgeType::Gem: PreloadMetadataAsync("Bridge/Gem"_s); break; |
| 35 |
case BridgeType::Lab: PreloadMetadataAsync("Bridge/Lab"_s); break; |
| 36 |
} |
| 37 |
} |
| 38 |
|
| 39 |
Task<bool> Bridge::OnActivatedAsync(const ActorActivationDetails& details) |
| 40 |
{ |
| 41 |
_bridgeWidth = *(uint16_t*)&details.Params[0] * 16; |
| 42 |
_bridgeType = (BridgeType)details.Params[2]; |
| 43 |
// Limit _heightFactor here, because with higher _heightFactor (for example in "04_haunted1") it starts to be inaccurate |
| 44 |
_heightFactor = std::round(std::min((float)_bridgeWidth / details.Params[3], 38.0f)); |
| 45 |
|
| 46 |
_pos.Y -= 6.0f; |
| 47 |
|
| 48 |
SetState(ActorState::SkipPerPixelCollisions, true); |
| 49 |
SetState(ActorState::CanBeFrozen | ActorState::CollideWithTileset | ActorState::ApplyGravitation, false); |
| 50 |
|
| 51 |
switch (_bridgeType) { |
| 52 |
default: |
| 53 |
case BridgeType::Rope: async_await RequestMetadataAsync("Bridge/Rope"_s); _widths = PieceWidthsRope; _widthsCount = std::int32_t(arraySize(PieceWidthsRope)); break; |
| 54 |
case BridgeType::Stone: async_await RequestMetadataAsync("Bridge/Stone"_s); _widths = PieceWidthsStone; _widthsCount = std::int32_t(arraySize(PieceWidthsStone)); break; |
| 55 |
case BridgeType::Vine: async_await RequestMetadataAsync("Bridge/Vine"_s); _widths = PieceWidthsVine; _widthsCount = std::int32_t(arraySize(PieceWidthsVine)); _widthOffset = 8; break; |
| 56 |
case BridgeType::StoneRed: async_await RequestMetadataAsync("Bridge/StoneRed"_s); _widths = PieceWidthsStoneRed; _widthsCount = std::int32_t(arraySize(PieceWidthsStoneRed)); break; |
| 57 |
case BridgeType::Log: async_await RequestMetadataAsync("Bridge/Log"_s); _widths = PieceWidthsLog; _widthsCount = std::int32_t(arraySize(PieceWidthsLog)); break; |
| 58 |
case BridgeType::Gem: async_await RequestMetadataAsync("Bridge/Gem"_s); _widths = PieceWidthsGem; _widthsCount = std::int32_t(arraySize(PieceWidthsGem)); break; |
| 59 |
case BridgeType::Lab: async_await RequestMetadataAsync("Bridge/Lab"_s); _widths = PieceWidthsLab; _widthsCount = std::int32_t(arraySize(PieceWidthsLab)); _widthOffset = 12; break; |
| 60 |
} |
| 61 |
|
| 62 |
SetAnimation(AnimState::Default); |
| 63 |
|
| 64 |
auto& resolver = ContentResolver::Get(); |
| 65 |
if (!resolver.IsHeadless()) { |
| 66 |
std::int32_t widthCovered = _widths[0] / 2 - _widthOffset; |
| 67 |
for (std::int32_t i = 0; widthCovered <= _bridgeWidth + 4; i++) { |
| 68 |
BridgePiece& piece = _pieces.emplace_back(); |
| 69 |
piece.Pos = Vector2f(_pos.X + widthCovered - 16, _pos.Y); |
| 70 |
piece.Command = std::make_unique<RenderCommand>(RenderCommand::Type::Sprite); |
| 71 |
piece.Command->GetMaterial().SetShaderProgramType(Material::ShaderProgramType::Sprite); |
| 72 |
piece.Command->GetMaterial().SetBlendingEnabled(true); |
| 73 |
piece.Command->GetMaterial().ReserveUniformsDataMemory(); |
| 74 |
piece.Command->GetGeometry().SetDrawParameters(GL_TRIANGLE_STRIP, 0, 4); |
| 75 |
|
| 76 |
auto* textureUniform = piece.Command->GetMaterial().Uniform(Material::TextureUniformName); |
| 77 |
if (textureUniform && textureUniform->GetIntValue(0) != 0) { |
| 78 |
textureUniform->SetIntValue(0); // GL_TEXTURE0 |
| 79 |
} |
| 80 |
|
| 81 |
widthCovered += (_widths[i % _widthsCount] + _widths[(i + 1) % _widthsCount]) / 2; |
| 82 |
} |
| 83 |
} |
| 84 |
|
| 85 |
async_return true; |
| 86 |
} |
| 87 |
|
| 88 |
void Bridge::OnUpdate(float timeMult) |
| 89 |
{ |
| 90 |
std::int32_t n = 0; |
| 91 |
Player* foundPlayers[8]; |
| 92 |
float foundX[8]; |
| 93 |
|
| 94 |
auto players = _levelHandler->GetPlayers(); |
| 95 |
for (auto* player : players) { |
| 96 |
auto diff = player->GetPos() - _pos; |
| 97 |
if (n < arraySize(foundPlayers) && diff.X >= -20.0f && diff.X <= _bridgeWidth + 20.0f && |
| 98 |
diff.Y > -27.0f && diff.Y < _heightFactor && player->GetSpeed().Y >= 0.0f) { |
| 99 |
foundX[n] = diff.X; |
| 100 |
foundPlayers[n] = player; |
| 101 |
n++; |
| 102 |
} else { |
| 103 |
player->CancelCarryingObject(this); |
| 104 |
} |
| 105 |
} |
| 106 |
|
| 107 |
if (n >= 2) { |
| 108 |
float left = std::numeric_limits<float>::max(); |
| 109 |
float right = std::numeric_limits<float>::min(); |
| 110 |
|
| 111 |
for (std::int32_t i = 0; i < n; i++) { |
| 112 |
if (left > foundX[i]) { |
| 113 |
left = foundX[i]; |
| 114 |
} |
| 115 |
if (right < foundX[i]) { |
| 116 |
right = foundX[i]; |
| 117 |
} |
| 118 |
} |
| 119 |
|
| 120 |
_leftHeight = (std::abs(_leftX - left) < 64.0f ? lerpByTime(_leftHeight, GetSectionHeight(left), 0.1f, timeMult) : 0.0f); |
| 121 |
_rightHeight = (std::abs(_rightX - right) < 64.0f ? lerpByTime(_rightHeight, GetSectionHeight(right), 0.1f, timeMult) : 0.0f); |
| 122 |
_leftX = left; |
| 123 |
_rightX = right; |
| 124 |
|
| 125 |
for (std::int32_t i = 0; i < n; i++) { |
| 126 |
float height; |
| 127 |
if (foundX[i] <= left) { |
| 128 |
height = _leftHeight; |
| 129 |
} else if (right <= foundX[i]) { |
| 130 |
height = _rightHeight; |
| 131 |
} else { |
| 132 |
height = lerp(_leftHeight, _rightHeight, (foundX[i] - left) / (right - left)); |
| 133 |
} |
| 134 |
|
| 135 |
auto* player = foundPlayers[i]; |
| 136 |
auto playerPos = player->GetPos(); |
| 137 |
player->UpdateCarryingObject(this); |
| 138 |
player->MoveInstantly(Vector2f(playerPos.X, _pos.Y + playerPos.Y - player->AABBInner.B + BaseY + height), |
| 139 |
MoveType::Absolute | MoveType::Force); |
| 140 |
} |
| 141 |
} else if (n == 1) { |
| 142 |
_leftHeight = _rightHeight = lerpByTime(std::max(_leftHeight, _rightHeight), GetSectionHeight(foundX[0]), 0.1f, timeMult); |
| 143 |
_leftX = _rightX = foundX[0]; |
| 144 |
|
| 145 |
for (std::int32_t i = 0; i < n; i++) { |
| 146 |
auto* player = foundPlayers[i]; |
| 147 |
auto playerPos = player->GetPos(); |
| 148 |
player->UpdateCarryingObject(this); |
| 149 |
player->MoveInstantly(Vector2f(playerPos.X, _pos.Y + playerPos.Y - player->AABBInner.B + BaseY + _leftHeight), |
| 150 |
MoveType::Absolute | MoveType::Force); |
| 151 |
} |
| 152 |
} else { |
| 153 |
_leftHeight = lerpByTime(_leftHeight, 0.0f, 0.1f, timeMult); |
| 154 |
_rightHeight = lerpByTime(_rightHeight, 0.0f, 0.1f, timeMult); |
| 155 |
} |
| 156 |
|
| 157 |
auto& resolver = ContentResolver::Get(); |
| 158 |
if (!resolver.IsHeadless()) { |
| 159 |
if (_leftHeight <= 0.001f && _rightHeight <= 0.001f) { |
| 160 |
// Render straight bridge |
| 161 |
std::int32_t widthCovered = _widths[0] / 2 - _widthOffset; |
| 162 |
for (std::int32_t i = 0; widthCovered <= _bridgeWidth + 4; i++) { |
| 163 |
BridgePiece& piece = _pieces[i]; |
| 164 |
piece.Pos = Vector2f(_pos.X + widthCovered - 16, _pos.Y); |
| 165 |
widthCovered += (_widths[i % _widthsCount] + _widths[(i + 1) % _widthsCount]) / 2; |
| 166 |
} |
| 167 |
} else { |
| 168 |
// Render deformed bridge |
| 169 |
std::int32_t widthCovered = _widths[0] / 2 - _widthOffset; |
| 170 |
for (std::int32_t i = 0; widthCovered <= _bridgeWidth + 4; i++) { |
| 171 |
float drop; |
| 172 |
if (widthCovered < _leftX) { |
| 173 |
drop = _leftHeight * sinf(fPiOver2 * widthCovered / _leftX); |
| 174 |
} else if (_rightX < widthCovered) { |
| 175 |
drop = _rightHeight * sinf(fPiOver2 * (_bridgeWidth - widthCovered) / (_bridgeWidth - _rightX)); |
| 176 |
} else { |
| 177 |
drop = lerp(_leftHeight, _rightHeight, (widthCovered - _leftX) / (_rightX - _leftX)); |
| 178 |
} |
| 179 |
|
| 180 |
BridgePiece& piece = _pieces[i]; |
| 181 |
piece.Pos.Y = _pos.Y + drop; |
| 182 |
|
| 183 |
widthCovered += (_widths[i % _widthsCount] + _widths[(i + 1) % _widthsCount]) / 2; |
| 184 |
} |
| 185 |
} |
| 186 |
} |
| 187 |
} |
| 188 |
|
| 189 |
void Bridge::OnUpdateHitbox() |
| 190 |
{ |
| 191 |
AABBInner = AABBf(_pos.X - 16.0f, _pos.Y + BaseY, _pos.X + _bridgeWidth + 16.0f, _pos.Y - BaseY); |
| 192 |
} |
| 193 |
|
| 194 |
bool Bridge::OnDraw(RenderQueue& renderQueue) |
| 195 |
{ |
| 196 |
if (_currentAnimation != nullptr) { |
| 197 |
Vector2i texSize = _currentAnimation->Base->TextureDiffuse->GetSize(); |
| 198 |
|
| 199 |
for (std::int32_t i = 0; i < _pieces.size(); i++) { |
| 200 |
auto* command = _pieces[i].Command.get(); |
| 201 |
|
| 202 |
std::int32_t curAnimFrame = _currentAnimation->FrameOffset + (i % _currentAnimation->FrameCount); |
| 203 |
std::int32_t col = curAnimFrame % _currentAnimation->Base->FrameConfiguration.X; |
| 204 |
std::int32_t row = curAnimFrame / _currentAnimation->Base->FrameConfiguration.X; |
| 205 |
float texScaleX = (float(_currentAnimation->Base->FrameDimensions.X) / float(texSize.X)); |
| 206 |
float texBiasX = (float(_currentAnimation->Base->FrameDimensions.X * col) / float(texSize.X)); |
| 207 |
float texScaleY = (float(_currentAnimation->Base->FrameDimensions.Y) / float(texSize.Y)); |
| 208 |
float texBiasY = (float(_currentAnimation->Base->FrameDimensions.Y * row) / float(texSize.Y)); |
| 209 |
|
| 210 |
auto* instanceBlock = command->GetMaterial().UniformBlock(Material::InstanceBlockName); |
| 211 |
instanceBlock->GetUniform(Material::TexRectUniformName)->SetFloatValue(texScaleX, texBiasX, texScaleY, texBiasY); |
| 212 |
instanceBlock->GetUniform(Material::SpriteSizeUniformName)->SetFloatValue((float)_currentAnimation->Base->FrameDimensions.X, (float)_currentAnimation->Base->FrameDimensions.Y); |
| 213 |
instanceBlock->GetUniform(Material::ColorUniformName)->SetFloatVector(Colorf::White.Data()); |
| 214 |
|
| 215 |
auto pos = _pieces[i].Pos; |
| 216 |
command->SetTransformation(Matrix4x4f::Translation(pos.X - _currentAnimation->Base->FrameDimensions.X / 2, pos.Y - _currentAnimation->Base->FrameDimensions.Y / 2, 0.0f)); |
| 217 |
command->SetLayer(_renderer.layer()); |
| 218 |
command->GetMaterial().SetTexture(*_currentAnimation->Base->TextureDiffuse.get()); |
| 219 |
|
| 220 |
renderQueue.AddCommand(command); |
| 221 |
} |
| 222 |
} |
| 223 |
|
| 224 |
return true; |
| 225 |
} |
| 226 |
|
| 227 |
float Bridge::GetSectionHeight(float x) const |
| 228 |
{ |
| 229 |
float fase = fPi * std::clamp(x / _bridgeWidth, 0.0f, 1.0f); |
| 230 |
return _heightFactor * sinf(fase); |
| 231 |
} |
| 232 |
} |