| 1 |
#include "PinballBumper.h" |
| 2 |
#include "../../ILevelHandler.h" |
| 3 |
#include "../Player.h" |
| 4 |
|
| 5 |
namespace Jazz2::Actors::Solid |
| 6 |
{ |
| 7 |
PinballBumper::PinballBumper() |
| 8 |
: _cooldown(0.0f), _lightIntensity(0.0f), _lightBrightness(0.0f) |
| 9 |
{ |
| 10 |
} |
| 11 |
|
| 12 |
void PinballBumper::Preload(const ActorActivationDetails& details) |
| 13 |
{ |
| 14 |
PreloadMetadataAsync("Object/PinballBumper"_s); |
| 15 |
} |
| 16 |
|
| 17 |
Task<bool> PinballBumper::OnActivatedAsync(const ActorActivationDetails& details) |
| 18 |
{ |
| 19 |
std::uint8_t theme = details.Params[0]; |
| 20 |
|
| 21 |
SetState(ActorState::CollideWithTileset | ActorState::IsSolidObject | ActorState::ApplyGravitation, false); |
| 22 |
|
| 23 |
async_await RequestMetadataAsync("Object/PinballBumper"_s); |
| 24 |
|
| 25 |
SetAnimation((AnimState)theme); |
| 26 |
|
| 27 |
async_return true; |
| 28 |
} |
| 29 |
|
| 30 |
void PinballBumper::OnUpdate(float timeMult) |
| 31 |
{ |
| 32 |
if (_frozenTimeLeft > 0.0f) { |
| 33 |
return; |
| 34 |
} |
| 35 |
|
| 36 |
if (_cooldown <= 0.0f) { |
| 37 |
_levelHandler->FindCollisionActorsByRadius(_pos.X, _pos.Y, 16.0f, [this, timeMult](ActorBase* actor) { |
| 38 |
if (auto* player = runtime_cast<Player>(actor)) { |
| 39 |
_cooldown = 16.0f; |
| 40 |
|
| 41 |
SetTransition(_currentAnimation->State | (AnimState)0x200, true); |
| 42 |
PlaySfx("Hit"_s, 0.8f); |
| 43 |
|
| 44 |
constexpr float forceMult = 12.0f; |
| 45 |
Vector2f force = (player->GetPos() - _pos).Normalize() * forceMult; |
| 46 |
if (!_levelHandler->IsReforged()) { |
| 47 |
force.Y *= 0.9f; |
| 48 |
} |
| 49 |
|
| 50 |
// Move the player back |
| 51 |
player->MoveInstantly(-player->_speed * timeMult, MoveType::Relative); |
| 52 |
|
| 53 |
// Reset speed if the force acts on the other side |
| 54 |
if (force.X < 0.0f) { |
| 55 |
if (player->_speed.X > 0.0f) player->_speed.X = 0.0f; |
| 56 |
if (player->_externalForce.X > 0.0f) player->_externalForce.X = 0.0f; |
| 57 |
} else if (force.X > 0.0f) { |
| 58 |
if (player->_speed.X < 0.0f) player->_speed.X = 0.0f; |
| 59 |
if (player->_externalForce.X < 0.0f) player->_externalForce.X = 0.0f; |
| 60 |
} |
| 61 |
if (force.Y < 0.0f) { |
| 62 |
if (player->_speed.Y > 0.0f) player->_speed.Y = 0.0f; |
| 63 |
if (player->_externalForce.Y > 0.0f) player->_externalForce.Y = 0.0f; |
| 64 |
} else if (force.Y > 0.0f) { |
| 65 |
if (player->_speed.Y < 0.0f) player->_speed.Y = 0.0f; |
| 66 |
if (player->_externalForce.Y < 0.0f) player->_externalForce.Y = 0.0f; |
| 67 |
} |
| 68 |
|
| 69 |
player->_speed.X += force.X * 0.4f; |
| 70 |
player->_speed.Y += force.Y * 0.4f; |
| 71 |
|
| 72 |
if (player->_activeModifier == Player::Modifier::None) { |
| 73 |
if (player->_copterFramesLeft > 0.0f) { |
| 74 |
player->_copterFramesLeft = 1.0f; |
| 75 |
} |
| 76 |
|
| 77 |
player->_externalForce.X += force.X * 0.04f; |
| 78 |
player->_externalForce.Y += force.Y * 0.04f; |
| 79 |
player->_externalForceCooldown = 10.0f; |
| 80 |
player->_controllable = true; |
| 81 |
player->SetState(ActorState::CanJump, false); |
| 82 |
player->EndDamagingMove(); |
| 83 |
} |
| 84 |
|
| 85 |
// TODO: Check this |
| 86 |
player->AddScore(500); |
| 87 |
} |
| 88 |
return true; |
| 89 |
}); |
| 90 |
} else { |
| 91 |
_cooldown -= timeMult; |
| 92 |
} |
| 93 |
|
| 94 |
if (_lightIntensity > 0.0f) { |
| 95 |
_lightIntensity -= timeMult * 0.01f; |
| 96 |
if (_lightIntensity < 0.0f) { |
| 97 |
_lightIntensity = 0.0f; |
| 98 |
} |
| 99 |
} |
| 100 |
if (_lightBrightness > 0.0f) { |
| 101 |
_lightBrightness -= timeMult * 0.02f; |
| 102 |
|
| 103 |
if (_lightIntensity < 0.0f) { |
| 104 |
_lightIntensity = 0.0f; |
| 105 |
} |
| 106 |
} |
| 107 |
} |
| 108 |
|
| 109 |
void PinballBumper::OnEmitLights(SmallVectorImpl<LightEmitter>& lights) |
| 110 |
{ |
| 111 |
if (_lightIntensity > 0.0f) { |
| 112 |
auto& light = lights.emplace_back(); |
| 113 |
light.Pos = _pos; |
| 114 |
light.Intensity = _lightIntensity; |
| 115 |
light.Brightness = _lightBrightness; |
| 116 |
light.RadiusNear = 24.0f; |
| 117 |
light.RadiusFar = 60.0f; |
| 118 |
} |
| 119 |
} |
| 120 |
} |