| 1 |
#include "RollingRock.h" |
| 2 |
#include "../../ILevelHandler.h" |
| 3 |
#include "../../Events/EventMap.h" |
| 4 |
#include "../Player.h" |
| 5 |
|
| 6 |
using namespace Jazz2::Tiles; |
| 7 |
|
| 8 |
namespace Jazz2::Actors::Environment |
| 9 |
{ |
| 10 |
RollingRock::RollingRock() |
| 11 |
: |
| 12 |
_delayLeft(40.0f), |
| 13 |
_triggered(false), |
| 14 |
_soundCooldown(0.0f) |
| 15 |
{ |
| 16 |
} |
| 17 |
|
| 18 |
void RollingRock::Preload(const ActorActivationDetails& details) |
| 19 |
{ |
| 20 |
PreloadMetadataAsync("Object/RollingRock"_s); |
| 21 |
} |
| 22 |
|
| 23 |
Task<bool> RollingRock::OnActivatedAsync(const ActorActivationDetails& details) |
| 24 |
{ |
| 25 |
_renderer.setLayer(_renderer.layer() + 10); |
| 26 |
|
| 27 |
_id = details.Params[0]; |
| 28 |
_triggerSpeedX = (float)*(int8_t*)&details.Params[1]; |
| 29 |
_triggerSpeedY = (float)*(int8_t*)&details.Params[2]; |
| 30 |
|
| 31 |
SetState(ActorState::CollideWithSolidObjects | ActorState::CollideWithSolidObjectsBelow | ActorState::IsSolidObject | ActorState::IsInvulnerable, true); |
| 32 |
SetState(ActorState::ApplyGravitation, false); |
| 33 |
_elasticity = 0.4f; |
| 34 |
_canHurtPlayer = false; |
| 35 |
|
| 36 |
async_await RequestMetadataAsync("Object/RollingRock"_s); |
| 37 |
SetAnimation(AnimState::Idle); |
| 38 |
|
| 39 |
_renderer.setRotation(_pos.X + _pos.Y); |
| 40 |
|
| 41 |
async_return true; |
| 42 |
} |
| 43 |
|
| 44 |
void RollingRock::OnUpdate(float timeMult) |
| 45 |
{ |
| 46 |
UpdateFrozenState(timeMult); |
| 47 |
|
| 48 |
if (_soundCooldown > 0.0f) { |
| 49 |
_soundCooldown -= timeMult; |
| 50 |
} |
| 51 |
|
| 52 |
if (_frozenTimeLeft > 0.0f) { |
| 53 |
return; |
| 54 |
} |
| 55 |
|
| 56 |
if (GetState(ActorState::ApplyGravitation)) { |
| 57 |
// Rock is triggered |
| 58 |
float currentGravity = _levelHandler->GetGravity(); |
| 59 |
|
| 60 |
_speed.X = std::clamp(_speed.X + _externalForce.X * timeMult, -3.0f, 3.0f) * powf(0.6f, timeMult); |
| 61 |
|
| 62 |
bool up = IsPositionEmpty(_pos.X, _pos.Y - 33.0f); |
| 63 |
bool down = IsPositionEmpty(_pos.X - 4.0f, _pos.Y + 36.0f) && IsPositionEmpty(_pos.X + 4.0f, _pos.Y + 36.0f); |
| 64 |
bool left = IsPositionEmpty(_pos.X - 36.0f, _pos.Y) && IsPositionEmpty(_pos.X - 19.0f, _pos.Y - 19.0f) && IsPositionEmpty(_pos.X - 4.0f, _pos.Y + 33.0f); |
| 65 |
bool right = IsPositionEmpty(_pos.X + 36.0f, _pos.Y) && IsPositionEmpty(_pos.X + 19.0f, _pos.Y + 19.0f) && IsPositionEmpty(_pos.X + 4.0f, _pos.Y + 33.0f); |
| 66 |
|
| 67 |
if (down) { |
| 68 |
if (_pos.Y > _levelHandler->GetWaterLevel()) { |
| 69 |
_speed.Y += currentGravity * 0.25f * timeMult; |
| 70 |
} else { |
| 71 |
_speed.Y += currentGravity * timeMult; |
| 72 |
} |
| 73 |
} |
| 74 |
|
| 75 |
//int hits = 0; |
| 76 |
if ((_speed.X < 0.0f && !left) || (_speed.X > 0.0f && !right)) { |
| 77 |
// Bounce on X |
| 78 |
if (_soundCooldown <= 0.0f && std::abs(_speed.X) > 2.0f) { |
| 79 |
_soundCooldown = 140.0f; |
| 80 |
PlaySfx("Hit"_s, 0.6f, 0.4f); |
| 81 |
} |
| 82 |
|
| 83 |
_speed.X = _speed.X * -0.5f; |
| 84 |
_externalForce.X = _externalForce.X * -0.5f; |
| 85 |
//hits |= 0x1; |
| 86 |
} |
| 87 |
if ((_speed.Y < 0.0f && !up) || (_speed.Y > 0.0f && !down)) { |
| 88 |
// Bounce on Y |
| 89 |
if (_soundCooldown <= 0.0f && std::abs(_speed.Y) > 2.0f) { |
| 90 |
_soundCooldown = 140.0f; |
| 91 |
PlaySfx("Hit"_s, 0.6f, 0.4f); |
| 92 |
} |
| 93 |
|
| 94 |
_speed.Y = _speed.Y * -0.5f; |
| 95 |
//hits |= 0x2; |
| 96 |
} |
| 97 |
|
| 98 |
MoveInstantly(Vector2f(_speed.X * timeMult, _speed.Y * timeMult), MoveType::Relative | MoveType::Force); |
| 99 |
|
| 100 |
if (std::abs(_speed.X) < 0.2f && !down) { |
| 101 |
_speed = Vector2f::Zero; |
| 102 |
_externalForce = Vector2f::Zero; |
| 103 |
SetState(ActorState::ApplyGravitation, false); |
| 104 |
_canHurtPlayer = false; |
| 105 |
} |
| 106 |
|
| 107 |
_renderer.setRotation(_renderer.rotation() + _speed.X * 0.02f * timeMult); |
| 108 |
} |
| 109 |
|
| 110 |
// Trigger |
| 111 |
if (_triggered && _delayLeft > 0.0f) { |
| 112 |
_delayLeft -= timeMult; |
| 113 |
|
| 114 |
if (_delayLeft <= 0.0f) { |
| 115 |
SetState(ActorState::ApplyGravitation, true); |
| 116 |
_canHurtPlayer = true; |
| 117 |
|
| 118 |
_speed.X = _triggerSpeedX; |
| 119 |
_speed.Y = _triggerSpeedY; |
| 120 |
_externalForce.X = (_triggerSpeedX < 0.0f ? -4.0f : 4.0f); |
| 121 |
|
| 122 |
if (_soundCooldown <= 0.0f) { |
| 123 |
_soundCooldown = 140.0f; |
| 124 |
PlaySfx("Hit"_s, 0.6f, 0.4f); |
| 125 |
} |
| 126 |
} |
| 127 |
} |
| 128 |
} |
| 129 |
|
| 130 |
void RollingRock::OnUpdateHitbox() |
| 131 |
{ |
| 132 |
UpdateHitbox(50, 50); |
| 133 |
} |
| 134 |
|
| 135 |
bool RollingRock::OnHandleCollision(std::shared_ptr<ActorBase> other) |
| 136 |
{ |
| 137 |
if (auto* rollingRock = runtime_cast<RollingRock>(other.get())) { |
| 138 |
float dx = (rollingRock->_pos.X - _pos.X); |
| 139 |
float dy = (rollingRock->_pos.Y - _pos.Y); |
| 140 |
float distance = Vector2f(dx, dy).Length(); |
| 141 |
if (distance < 74.0f) { |
| 142 |
float relSpeeds = (std::abs(_speed.X) + std::abs(rollingRock->_speed.X)) * 0.5f; |
| 143 |
|
| 144 |
if (dx > 0.0f) { |
| 145 |
_speed.X = -relSpeeds; |
| 146 |
_externalForce.X = -std::abs(_externalForce.X); |
| 147 |
} else { |
| 148 |
_speed.X = relSpeeds; |
| 149 |
_externalForce.X = std::abs(_externalForce.X); |
| 150 |
} |
| 151 |
if (dx < 0.0f) { |
| 152 |
rollingRock->_speed.X = -relSpeeds; |
| 153 |
rollingRock->_externalForce.X = -std::abs(rollingRock->_externalForce.X); |
| 154 |
} else { |
| 155 |
rollingRock->_speed.X = relSpeeds; |
| 156 |
rollingRock->_externalForce.X = std::abs(rollingRock->_externalForce.X); |
| 157 |
} |
| 158 |
|
| 159 |
if (relSpeeds > 1.0f) { |
| 160 |
_triggered = true; |
| 161 |
} |
| 162 |
|
| 163 |
SetState(ActorState::CanBeFrozen, true); |
| 164 |
} |
| 165 |
return true; |
| 166 |
} else if (auto* player = runtime_cast<Player>(other.get())) { |
| 167 |
if (_triggered) { |
| 168 |
float dx = (player->GetPos().X - _pos.X); |
| 169 |
float dy = (player->GetPos().Y - _pos.Y); |
| 170 |
float distance = Vector2f(dx, dy).Length(); |
| 171 |
|
| 172 |
if (distance < 50.0f || dy > 0.0f) { |
| 173 |
float relSpeeds = (std::abs(_speed.X) + std::abs(player->GetSpeed().X)) * 0.5f; |
| 174 |
if (relSpeeds < 1.0f) { |
| 175 |
relSpeeds = (60.0f - distance) * 0.2f; |
| 176 |
} |
| 177 |
|
| 178 |
if (dx > 0.0f) { |
| 179 |
_speed.X = -2.0f; |
| 180 |
_externalForce.X = -std::abs(_externalForce.X); |
| 181 |
} else { |
| 182 |
_speed.X = 2.0f; |
| 183 |
_externalForce.X = std::abs(_externalForce.X); |
| 184 |
} |
| 185 |
if (dy > 0.0f) { |
| 186 |
_externalForce.Y -= 2.0f; |
| 187 |
} |
| 188 |
SetState(ActorState::ApplyGravitation, true); |
| 189 |
} |
| 190 |
} |
| 191 |
} |
| 192 |
|
| 193 |
return EnemyBase::OnHandleCollision(std::move(other)); |
| 194 |
} |
| 195 |
|
| 196 |
void RollingRock::OnTriggeredEvent(EventType eventType, std::uint8_t* eventParams) |
| 197 |
{ |
| 198 |
if (!_triggered && eventType == EventType::RollingRockTrigger && eventParams[0] == _id) { |
| 199 |
_triggered = true; |
| 200 |
|
| 201 |
_levelHandler->ShakeCameraViewNear(_pos, 60.0f); |
| 202 |
|
| 203 |
// TODO: Shake amplitude |
| 204 |
/*float distanceToPlayer; |
| 205 |
if (distanceToPlayer < 400.0f) { |
| 206 |
float amplitude = std::min((400.0f - distanceToPlayer) / (256.0f * 8.0f), 40.0f); |
| 207 |
_levelHandler->ShakeCameraView(amplitude); |
| 208 |
}*/ |
| 209 |
} |
| 210 |
} |
| 211 |
|
| 212 |
bool RollingRock::IsPositionEmpty(float x, float y) |
| 213 |
{ |
| 214 |
TileCollisionParams params = { TileDestructType::None, _speed.Y >= 0.0f }; |
| 215 |
return _levelHandler->IsPositionEmpty(this, AABBf(x - 1.0f, y - 1.0f, x + 1.0f, y + 1.0f), params); |
| 216 |
} |
| 217 |
} |