| 1 |
#include "Spring.h" |
| 2 |
#include "../../ILevelHandler.h" |
| 3 |
#include "../../Tiles/TileMap.h" |
| 4 |
#include "../Weapons/ShieldFireShot.h" |
| 5 |
#include "../Weapons/ToasterShot.h" |
| 6 |
#include "../Weapons/Thunderbolt.h" |
| 7 |
|
| 8 |
using namespace Jazz2::Tiles; |
| 9 |
|
| 10 |
namespace Jazz2::Actors::Environment |
| 11 |
{ |
| 12 |
Spring::Spring() |
| 13 |
: _cooldown(0.0f) |
| 14 |
{ |
| 15 |
} |
| 16 |
|
| 17 |
void Spring::Preload(const ActorActivationDetails& details) |
| 18 |
{ |
| 19 |
PreloadMetadataAsync("Object/Spring"_s); |
| 20 |
} |
| 21 |
|
| 22 |
Vector2f Spring::Activate() |
| 23 |
{ |
| 24 |
if (_state == State::Frozen || _cooldown > 0.0f || _frozenTimeLeft > 0.0f) { |
| 25 |
return Vector2f::Zero; |
| 26 |
} |
| 27 |
|
| 28 |
_cooldown = (_delay > 0 ? _delay : 6.0f); |
| 29 |
|
| 30 |
SetTransition(_currentAnimation->State | (AnimState)0x200, false); |
| 31 |
switch (_orientation) { |
| 32 |
case Orientation::Bottom: |
| 33 |
PlaySfx("Vertical"_s); |
| 34 |
return Vector2f(0, -_strength); |
| 35 |
case Orientation::Top: |
| 36 |
PlaySfx("VerticalReversed"_s); |
| 37 |
return Vector2f(0, _strength); |
| 38 |
case Orientation::Right: |
| 39 |
case Orientation::Left: |
| 40 |
PlaySfx("Horizontal"_s); |
| 41 |
return Vector2f(_strength * (_orientation == Orientation::Right ? 1 : -1), 0); |
| 42 |
default: |
| 43 |
return Vector2f::Zero; |
| 44 |
} |
| 45 |
} |
| 46 |
|
| 47 |
Task<bool> Spring::OnActivatedAsync(const ActorActivationDetails& details) |
| 48 |
{ |
| 49 |
_type = details.Params[0]; |
| 50 |
_orientation = (Orientation)details.Params[1]; |
| 51 |
KeepSpeedX = ((details.Params[2] & 0x04) != 0); |
| 52 |
KeepSpeedY = ((details.Params[2] & 0x08) != 0); |
| 53 |
bool applyGravitation = (details.Params[2] & 0x01) != 0; // Jazz² Resurrection only |
| 54 |
_state = ((details.Params[2] & 0x02) != 0 ? State::Frozen : State::Default); |
| 55 |
_delay = details.Params[3]; |
| 56 |
|
| 57 |
SetState(ActorState::SkipPerPixelCollisions, true); |
| 58 |
|
| 59 |
async_await RequestMetadataAsync("Object/Spring"_s); |
| 60 |
|
| 61 |
Vector2f tileCorner = Vector2f((std::int32_t)(_pos.X / Tiles::TileSet::DefaultTileSize) * Tiles::TileSet::DefaultTileSize, |
| 62 |
(std::int32_t)(_pos.Y / Tiles::TileSet::DefaultTileSize) * Tiles::TileSet::DefaultTileSize); |
| 63 |
if (_orientation > Orientation::Left) { |
| 64 |
// JJ2 horizontal springs held no data about which way they were facing. |
| 65 |
// For compatibility, correct orientation is evaluated during runtime. |
| 66 |
AABBf aabb = AABBf(_pos.X + 6.0f, _pos.Y - 2.0f, _pos.X + 26.0f, _pos.Y); |
| 67 |
TileCollisionParams params = { TileDestructType::None, true }; |
| 68 |
_orientation = (_levelHandler->TileMap()->IsTileEmpty(aabb, params) != (_orientation == (Orientation)5) ? Orientation::Right : Orientation::Left); |
| 69 |
} |
| 70 |
|
| 71 |
std::int32_t orientationBit = 0; |
| 72 |
switch (_orientation) { |
| 73 |
case Orientation::Bottom: |
| 74 |
MoveInstantly(Vector2f(tileCorner.X + 16, tileCorner.Y + 8), MoveType::Absolute | MoveType::Force); |
| 75 |
break; |
| 76 |
case Orientation::Right: |
| 77 |
MoveInstantly(Vector2f(tileCorner.X + 16, tileCorner.Y + 16), MoveType::Absolute | MoveType::Force); |
| 78 |
orientationBit = 1; |
| 79 |
break; |
| 80 |
case Orientation::Top: |
| 81 |
MoveInstantly(Vector2f(tileCorner.X + 16, tileCorner.Y + 8), MoveType::Absolute | MoveType::Force); |
| 82 |
orientationBit = 2; |
| 83 |
break; |
| 84 |
case Orientation::Left: |
| 85 |
MoveInstantly(Vector2f(tileCorner.X + 16, tileCorner.Y + 16), MoveType::Absolute | MoveType::Force); |
| 86 |
orientationBit = 1; |
| 87 |
SetFacingLeft(true); |
| 88 |
break; |
| 89 |
} |
| 90 |
|
| 91 |
SetState(ActorState::ApplyGravitation, applyGravitation); |
| 92 |
|
| 93 |
// Red starts at 1 in "Object/Spring" |
| 94 |
SetAnimation((AnimState)(((_type + 1) << 10) | (orientationBit << 12))); |
| 95 |
_renderer.setLayer(_renderer.layer() - 8); |
| 96 |
|
| 97 |
if (_orientation == Orientation::Right || _orientation == Orientation::Left) { |
| 98 |
// Horizontal springs all seem to have the same strength. |
| 99 |
// This constant strength gives about the correct amount of horizontal push. |
| 100 |
_strength = 9.5f; |
| 101 |
} else { |
| 102 |
// Vertical springs should work as follows: |
| 103 |
// Red spring lifts the player 9 tiles, green 14, and blue 19. |
| 104 |
// Vertical strength currently works differently from horizontal, that explains |
| 105 |
// the otherwise inexplicable difference of scale between the two types. |
| 106 |
switch (_type) { |
| 107 |
case 0: // Red |
| 108 |
_strength = 1.25f; |
| 109 |
break; |
| 110 |
case 1: // Green |
| 111 |
_strength = (_levelHandler->IsReforged() ? 1.50f : 1.52f); |
| 112 |
break; |
| 113 |
case 2: // Blue |
| 114 |
_strength = (_levelHandler->IsReforged() ? 1.68f : 1.72f); |
| 115 |
break; |
| 116 |
} |
| 117 |
} |
| 118 |
|
| 119 |
if (GetState(ActorState::ApplyGravitation)) { |
| 120 |
OnUpdateHitbox(); |
| 121 |
|
| 122 |
// Apply instant gravitation |
| 123 |
std::int32_t i = 10; |
| 124 |
while (i-- > 0 && MoveInstantly(Vector2f(0.0f, 4.0f), MoveType::Relative)) { |
| 125 |
// Nothing to do... |
| 126 |
} |
| 127 |
while (i-- > 0 && MoveInstantly(Vector2f(0.0f, 1.0f), MoveType::Relative)) { |
| 128 |
// Nothing to do... |
| 129 |
} |
| 130 |
} |
| 131 |
|
| 132 |
if (_state == State::Frozen) { |
| 133 |
_renderer.Initialize(ActorRendererType::FrozenMask); |
| 134 |
SetState(ActorState::CanBeFrozen, false); |
| 135 |
} |
| 136 |
|
| 137 |
async_return true; |
| 138 |
} |
| 139 |
|
| 140 |
void Spring::OnUpdate(float timeMult) |
| 141 |
{ |
| 142 |
ActorBase::OnUpdate(timeMult); |
| 143 |
|
| 144 |
if (_cooldown > 0.0f) { |
| 145 |
_cooldown -= timeMult; |
| 146 |
} |
| 147 |
if (_state == State::Heated) { |
| 148 |
// Cannot be directly in `Spring::OnHandleCollision()` due to bug in `BaseSprite::updateRenderCommand()`, |
| 149 |
// it would be called before `BaseSprite::updateRenderCommand()` but after `SceneNode::transform()` |
| 150 |
_renderer.Initialize(ActorRendererType::Default); |
| 151 |
_state = State::Default; |
| 152 |
} |
| 153 |
} |
| 154 |
|
| 155 |
void Spring::OnUpdateHitbox() |
| 156 |
{ |
| 157 |
switch (_orientation) { |
| 158 |
case Orientation::Right: |
| 159 |
AABBInner = AABBf(_pos.X - 8, _pos.Y - 10, _pos.X, _pos.Y + 10); |
| 160 |
break; |
| 161 |
case Orientation::Left: |
| 162 |
AABBInner = AABBf(_pos.X, _pos.Y - 10, _pos.X + 8, _pos.Y + 10); |
| 163 |
break; |
| 164 |
default: |
| 165 |
case Orientation::Bottom: |
| 166 |
case Orientation::Top: |
| 167 |
AABBInner = AABBf(_pos.X - 10, _pos.Y, _pos.X + 10, _pos.Y + 8); |
| 168 |
break; |
| 169 |
} |
| 170 |
} |
| 171 |
|
| 172 |
bool Spring::OnHandleCollision(std::shared_ptr<ActorBase> other) |
| 173 |
{ |
| 174 |
if (_state == State::Frozen) { |
| 175 |
ActorBase* actorBase = other.get(); |
| 176 |
if (runtime_cast<Weapons::ToasterShot>(actorBase) || runtime_cast<Weapons::Thunderbolt>(actorBase) || |
| 177 |
runtime_cast<Weapons::ShieldFireShot>(actorBase)) { |
| 178 |
_state = State::Heated; |
| 179 |
SetState(ActorState::CanBeFrozen, true); |
| 180 |
} |
| 181 |
} |
| 182 |
|
| 183 |
return ActorBase::OnHandleCollision(std::move(other)); |
| 184 |
} |
| 185 |
} |