| 1 |
#include "Robot.h" |
| 2 |
#include "../../../ILevelHandler.h" |
| 3 |
#include "../../Player.h" |
| 4 |
#include "../../Explosion.h" |
| 5 |
|
| 6 |
#include "../../../../nCine/Base/Random.h" |
| 7 |
|
| 8 |
#include <float.h> |
| 9 |
|
| 10 |
namespace Jazz2::Actors::Bosses |
| 11 |
{ |
| 12 |
Robot::Robot() |
| 13 |
: _state(StateWaiting), _stateTime(0.0f), _shots(0) |
| 14 |
{ |
| 15 |
} |
| 16 |
|
| 17 |
void Robot::Preload(const ActorActivationDetails& details) |
| 18 |
{ |
| 19 |
PreloadMetadataAsync("Boss/Robot"_s); |
| 20 |
} |
| 21 |
|
| 22 |
Task<bool> Robot::OnActivatedAsync(const ActorActivationDetails& details) |
| 23 |
{ |
| 24 |
_scoreValue = 2000; |
| 25 |
_originPos = Vector2f(_pos.X, _pos.Y - 300.0f); |
| 26 |
|
| 27 |
async_await RequestMetadataAsync("Boss/Robot"_s); |
| 28 |
SetAnimation(AnimState::Idle); |
| 29 |
|
| 30 |
SetFacingLeft(true); |
| 31 |
|
| 32 |
_renderer.setDrawEnabled(false); |
| 33 |
|
| 34 |
async_return true; |
| 35 |
} |
| 36 |
|
| 37 |
void Robot::Activate() |
| 38 |
{ |
| 39 |
SetHealthByDifficulty(100); |
| 40 |
|
| 41 |
_renderer.setDrawEnabled(true); |
| 42 |
_state = StateCopter; |
| 43 |
|
| 44 |
MoveInstantly(_originPos, MoveType::Absolute); |
| 45 |
|
| 46 |
SetAnimation((AnimState)1073741828); |
| 47 |
} |
| 48 |
|
| 49 |
void Robot::Deactivate() |
| 50 |
{ |
| 51 |
_renderer.setDrawEnabled(false); |
| 52 |
_state = StateWaiting; |
| 53 |
} |
| 54 |
|
| 55 |
void Robot::OnUpdate(float timeMult) |
| 56 |
{ |
| 57 |
EnemyBase::OnUpdate(timeMult); |
| 58 |
|
| 59 |
if (_frozenTimeLeft > 0.0f) { |
| 60 |
return; |
| 61 |
} |
| 62 |
|
| 63 |
switch (_state) { |
| 64 |
case StateCopter: { |
| 65 |
if (GetState(ActorState::CanJump)) { |
| 66 |
_speed.Y = 0.0f; |
| 67 |
|
| 68 |
_state = StateTransition; |
| 69 |
SetTransition((AnimState)1073741829, false, [this]() { |
| 70 |
FollowNearestPlayer(StateRunning1, Random().NextFloat(20, 40)); |
| 71 |
}); |
| 72 |
} else { |
| 73 |
_speed.Y -= (_levelHandler->IsReforged() ? 0.27f : 0.21f) * timeMult; |
| 74 |
} |
| 75 |
break; |
| 76 |
} |
| 77 |
case StateRunning1: { |
| 78 |
if (_stateTime <= 0.0f) { |
| 79 |
FollowNearestPlayer( |
| 80 |
Random().NextFloat() < 0.65f ? StateRunning1 : StateRunning2, |
| 81 |
Random().NextFloat(10, 30)); |
| 82 |
} |
| 83 |
break; |
| 84 |
} |
| 85 |
|
| 86 |
case StateRunning2: { |
| 87 |
if (_stateTime <= 0.0f) { |
| 88 |
_speed.X = 0.0f; |
| 89 |
|
| 90 |
_state = StateTransition; |
| 91 |
PlaySfx("AttackStart"_s); |
| 92 |
SetAnimation(AnimState::Idle); |
| 93 |
SetTransition((AnimState)1073741824, false, [this]() { |
| 94 |
_shots = Random().Next(1, 4); |
| 95 |
Shoot(); |
| 96 |
}); |
| 97 |
} |
| 98 |
break; |
| 99 |
} |
| 100 |
|
| 101 |
case StatePreparingToRun: { |
| 102 |
if (_stateTime <= 0.0f) { |
| 103 |
FollowNearestPlayer(StateRunning1, Random().NextFloat(10, 30)); |
| 104 |
} |
| 105 |
break; |
| 106 |
} |
| 107 |
} |
| 108 |
|
| 109 |
_stateTime -= timeMult; |
| 110 |
} |
| 111 |
|
| 112 |
void Robot::OnHealthChanged(ActorBase* collider) |
| 113 |
{ |
| 114 |
EnemyBase::OnHealthChanged(collider); |
| 115 |
|
| 116 |
std::int32_t n = Random().Next(1, 4); |
| 117 |
for (std::int32_t i = 0; i < n; i++) { |
| 118 |
CreateSpriteDebris((AnimState)Random().Fast(100, 109), 1); |
| 119 |
} |
| 120 |
|
| 121 |
PlaySfx("Shrapnel"_s); |
| 122 |
} |
| 123 |
|
| 124 |
bool Robot::OnPerish(ActorBase* collider) |
| 125 |
{ |
| 126 |
CreateParticleDebrisOnPerish(ParticleDebrisEffect::Standard, Vector2f::Zero); |
| 127 |
|
| 128 |
for (std::int32_t i = 0; i < 8; i++) { |
| 129 |
CreateSpriteDebris((AnimState)Random().Fast(100, 109), 1); |
| 130 |
} |
| 131 |
|
| 132 |
_levelHandler->PlayCommonSfx("Splat"_s, Vector3f(_pos.X, _pos.Y, 0.0f)); |
| 133 |
|
| 134 |
return EnemyBase::OnPerish(collider); |
| 135 |
} |
| 136 |
|
| 137 |
void Robot::FollowNearestPlayer(std::int32_t newState, float time) |
| 138 |
{ |
| 139 |
bool found = false; |
| 140 |
Vector2f targetPos = Vector2f(FLT_MAX, FLT_MAX); |
| 141 |
|
| 142 |
auto players = _levelHandler->GetPlayers(); |
| 143 |
for (auto* player : players) { |
| 144 |
if (player->GetHealth() <= 0) { |
| 145 |
continue; |
| 146 |
} |
| 147 |
|
| 148 |
Vector2f newPos = player->GetPos(); |
| 149 |
if ((_pos - newPos).SqrLength() < (_pos - targetPos).SqrLength()) { |
| 150 |
targetPos = newPos; |
| 151 |
found = true; |
| 152 |
} |
| 153 |
} |
| 154 |
|
| 155 |
if (found) { |
| 156 |
_state = newState; |
| 157 |
_stateTime = time; |
| 158 |
|
| 159 |
SetFacingLeft(targetPos.X < _pos.X); |
| 160 |
|
| 161 |
float mult = Random().NextFloat(0.6f, 0.9f); |
| 162 |
_speed.X = (IsFacingLeft() ? -3.0f : 3.0f) * mult; |
| 163 |
_renderer.AnimDuration = _currentAnimation->AnimDuration / mult; |
| 164 |
|
| 165 |
PlaySfx("Run"_s); |
| 166 |
SetAnimation(AnimState::Run); |
| 167 |
} |
| 168 |
} |
| 169 |
|
| 170 |
void Robot::Shoot() |
| 171 |
{ |
| 172 |
if (_state == StateWaiting) { |
| 173 |
return; |
| 174 |
} |
| 175 |
|
| 176 |
std::shared_ptr<SpikeBall> spikeBall = std::make_shared<SpikeBall>(); |
| 177 |
uint8_t spikeBallParams[1] = { (uint8_t)(IsFacingLeft() ? 1 : 0) }; |
| 178 |
spikeBall->OnActivated(ActorActivationDetails( |
| 179 |
_levelHandler, |
| 180 |
Vector3i((std::int32_t)_pos.X, (std::int32_t)_pos.Y - 32, _renderer.layer() + 2), |
| 181 |
spikeBallParams |
| 182 |
)); |
| 183 |
_levelHandler->AddActor(spikeBall); |
| 184 |
|
| 185 |
_shots--; |
| 186 |
|
| 187 |
PlaySfx("Attack"_s); |
| 188 |
SetTransition((AnimState)1073741825, false, [this]() { |
| 189 |
if (_shots > 0) { |
| 190 |
PlaySfx("AttackShutter"_s); |
| 191 |
Shoot(); |
| 192 |
} else { |
| 193 |
Run(); |
| 194 |
} |
| 195 |
}); |
| 196 |
} |
| 197 |
|
| 198 |
void Robot::Run() |
| 199 |
{ |
| 200 |
if (_state == StateWaiting) { |
| 201 |
return; |
| 202 |
} |
| 203 |
|
| 204 |
PlaySfx("AttackEnd"_s); |
| 205 |
SetTransition((AnimState)1073741826, false, [this]() { |
| 206 |
_state = StatePreparingToRun; |
| 207 |
_stateTime = 10.0f; |
| 208 |
}); |
| 209 |
} |
| 210 |
|
| 211 |
Task<bool> Robot::SpikeBall::OnActivatedAsync(const ActorActivationDetails& details) |
| 212 |
{ |
| 213 |
SetFacingLeft(details.Params[0] != 0); |
| 214 |
|
| 215 |
SetState(ActorState::IsInvulnerable | ActorState::SkipPerPixelCollisions, true); |
| 216 |
SetState(ActorState::CanBeFrozen, false); |
| 217 |
CanCollideWithShots = false; |
| 218 |
|
| 219 |
_health = INT32_MAX; |
| 220 |
_speed.X = (IsFacingLeft() ? -8.0f : 8.0f); |
| 221 |
|
| 222 |
async_await RequestMetadataAsync("Boss/Robot"_s); |
| 223 |
SetAnimation((AnimState)1073741827); |
| 224 |
|
| 225 |
async_return true; |
| 226 |
} |
| 227 |
|
| 228 |
void Robot::SpikeBall::OnUpdateHitbox() |
| 229 |
{ |
| 230 |
UpdateHitbox(8, 8); |
| 231 |
} |
| 232 |
|
| 233 |
void Robot::SpikeBall::OnEmitLights(SmallVectorImpl<LightEmitter>& lights) |
| 234 |
{ |
| 235 |
auto& light = lights.emplace_back(); |
| 236 |
light.Pos = _pos; |
| 237 |
light.Intensity = 0.1f; |
| 238 |
light.Brightness = 0.8f; |
| 239 |
light.RadiusNear = 0.0f; |
| 240 |
light.RadiusFar = 22.0f; |
| 241 |
} |
| 242 |
|
| 243 |
bool Robot::SpikeBall::OnPerish(ActorBase* collider) |
| 244 |
{ |
| 245 |
Explosion::Create(_levelHandler, Vector3i((std::int32_t)(_pos.X + _speed.X), (std::int32_t)(_pos.Y + _speed.Y), _renderer.layer() + 2), Explosion::Type::SmallDark); |
| 246 |
|
| 247 |
return EnemyBase::OnPerish(collider); |
| 248 |
} |
| 249 |
|
| 250 |
void Robot::SpikeBall::OnHitFloor(float timeMult) |
| 251 |
{ |
| 252 |
DecreaseHealth(INT32_MAX); |
| 253 |
} |
| 254 |
|
| 255 |
void Robot::SpikeBall::OnHitWall(float timeMult) |
| 256 |
{ |
| 257 |
DecreaseHealth(INT32_MAX); |
| 258 |
} |
| 259 |
|
| 260 |
void Robot::SpikeBall::OnHitCeiling(float timeMult) |
| 261 |
{ |
| 262 |
DecreaseHealth(INT32_MAX); |
| 263 |
} |
| 264 |
} |