| 1 |
#include "TurtleBoss.h" |
| 2 |
#include "../../../ILevelHandler.h" |
| 3 |
#include "../../Player.h" |
| 4 |
#include "../../Explosion.h" |
| 5 |
#include "../TurtleShell.h" |
| 6 |
|
| 7 |
#include "../../../../nCine/Base/Random.h" |
| 8 |
|
| 9 |
#include <float.h> |
| 10 |
|
| 11 |
namespace Jazz2::Actors::Bosses |
| 12 |
{ |
| 13 |
TurtleBoss::TurtleBoss() |
| 14 |
: _state(StateWaiting), _stateTime(0.0f), _endText(0), _maceTime(0.0f) |
| 15 |
{ |
| 16 |
} |
| 17 |
|
| 18 |
TurtleBoss::~TurtleBoss() |
| 19 |
{ |
| 20 |
if (_mace != nullptr) { |
| 21 |
_mace->DecreaseHealth(INT32_MAX); |
| 22 |
_mace = nullptr; |
| 23 |
} |
| 24 |
} |
| 25 |
|
| 26 |
void TurtleBoss::Preload(const ActorActivationDetails& details) |
| 27 |
{ |
| 28 |
PreloadMetadataAsync("Boss/TurtleBoss"_s); |
| 29 |
PreloadMetadataAsync("Boss/TurtleBossShell"_s); |
| 30 |
} |
| 31 |
|
| 32 |
Task<bool> TurtleBoss::OnActivatedAsync(const ActorActivationDetails& details) |
| 33 |
{ |
| 34 |
_endText = details.Params[1]; |
| 35 |
_originPos = _pos; |
| 36 |
_scoreValue = 5000; |
| 37 |
|
| 38 |
async_await RequestMetadataAsync("Boss/TurtleBoss"_s); |
| 39 |
SetAnimation(AnimState::Idle); |
| 40 |
|
| 41 |
SetFacingLeft(true); |
| 42 |
|
| 43 |
async_return true; |
| 44 |
} |
| 45 |
|
| 46 |
bool TurtleBoss::OnActivatedBoss() |
| 47 |
{ |
| 48 |
SetHealthByDifficulty(100); |
| 49 |
MoveInstantly(_originPos, MoveType::Absolute | MoveType::Force); |
| 50 |
FollowNearestPlayer(StateWalking1, Random().NextFloat(120.0f, 160.0f)); |
| 51 |
return true; |
| 52 |
} |
| 53 |
|
| 54 |
void TurtleBoss::OnUpdate(float timeMult) |
| 55 |
{ |
| 56 |
BossBase::OnUpdate(timeMult); |
| 57 |
|
| 58 |
if (_frozenTimeLeft > 0.0f) { |
| 59 |
return; |
| 60 |
} |
| 61 |
|
| 62 |
switch (_state) { |
| 63 |
case StateWalking1: { |
| 64 |
if (_stateTime <= 0.0f) { |
| 65 |
FollowNearestPlayer(StateWalking2, 16); |
| 66 |
} else if (!CanMoveToPosition(_speed.X, 0)) { |
| 67 |
SetFacingLeft(!IsFacingLeft()); |
| 68 |
_speed.X = -_speed.X; |
| 69 |
} |
| 70 |
break; |
| 71 |
} |
| 72 |
|
| 73 |
case StateWalking2: { |
| 74 |
if (_stateTime <= 0.0f) { |
| 75 |
_speed.X = 0.0f; |
| 76 |
|
| 77 |
PlaySfx("AttackStart"_s); |
| 78 |
|
| 79 |
_state = StateTransition; |
| 80 |
SetAnimation(AnimState::Idle); |
| 81 |
SetTransition((AnimState)1073741824, false, [this]() { |
| 82 |
_mace = std::make_shared<Mace>(); |
| 83 |
_mace->OnActivated(ActorActivationDetails( |
| 84 |
_levelHandler, |
| 85 |
Vector3i((std::int32_t)_pos.X, (std::int32_t)_pos.Y, _renderer.layer() + 2) |
| 86 |
)); |
| 87 |
_levelHandler->AddActor(_mace); |
| 88 |
|
| 89 |
SetTransition((AnimState)1073741825, false, [this]() { |
| 90 |
_state = StateAttacking; |
| 91 |
_stateTime = 10.0f; |
| 92 |
_maceTime = 480.0f; |
| 93 |
}); |
| 94 |
}); |
| 95 |
} else if (!CanMoveToPosition(_speed.X, 0)) { |
| 96 |
_speed.X = 0; |
| 97 |
|
| 98 |
SetAnimation(AnimState::Idle); |
| 99 |
} |
| 100 |
break; |
| 101 |
} |
| 102 |
|
| 103 |
case StateAttacking: { |
| 104 |
_maceTime -= timeMult; |
| 105 |
if (_maceTime <= 0.0f && _mace != nullptr) { |
| 106 |
_mace->DecreaseHealth(INT32_MAX); |
| 107 |
_mace = nullptr; |
| 108 |
|
| 109 |
SetTransition((AnimState)1073741826, false, [this]() { |
| 110 |
FollowNearestPlayer(StateWalking1, Random().NextFloat(80.0f, 160.0f)); |
| 111 |
}); |
| 112 |
} |
| 113 |
break; |
| 114 |
} |
| 115 |
} |
| 116 |
|
| 117 |
_stateTime -= timeMult; |
| 118 |
} |
| 119 |
|
| 120 |
bool TurtleBoss::OnHandleCollision(std::shared_ptr<ActorBase> other) |
| 121 |
{ |
| 122 |
if (_state == StateAttacking && _stateTime <= 0.0f) { |
| 123 |
if (auto* mace = runtime_cast<Mace>(other.get())) { |
| 124 |
if (mace == _mace.get()) { |
| 125 |
_mace->DecreaseHealth(INT32_MAX); |
| 126 |
_mace = nullptr; |
| 127 |
|
| 128 |
PlaySfx("AttackEnd"_s); |
| 129 |
|
| 130 |
SetTransition((AnimState)1073741826, false, [this]() { |
| 131 |
FollowNearestPlayer(StateWalking1, Random().NextFloat(80.0f, 160.0f)); |
| 132 |
}); |
| 133 |
return true; |
| 134 |
} |
| 135 |
} |
| 136 |
} |
| 137 |
|
| 138 |
return EnemyBase::OnHandleCollision(std::move(other)); |
| 139 |
} |
| 140 |
|
| 141 |
bool TurtleBoss::OnPerish(ActorBase* collider) |
| 142 |
{ |
| 143 |
float shellSpeedY; |
| 144 |
if (_pos.Y > _levelHandler->GetWaterLevel()) { |
| 145 |
shellSpeedY = -0.65f; |
| 146 |
} else if (_levelHandler->IsReforged()) { |
| 147 |
shellSpeedY = -1.1f; |
| 148 |
} else { |
| 149 |
shellSpeedY = -0.98f; |
| 150 |
} |
| 151 |
|
| 152 |
std::shared_ptr<Enemies::TurtleShell> shell = std::make_shared<Enemies::TurtleShell>(); |
| 153 |
uint8_t shellParams[9]; |
| 154 |
*(float*)&shellParams[0] = _speed.X * 1.1f; |
| 155 |
*(float*)&shellParams[4] = shellSpeedY; |
| 156 |
shellParams[8] = 2; |
| 157 |
shell->OnActivated(ActorActivationDetails( |
| 158 |
_levelHandler, |
| 159 |
Vector3i((std::int32_t)_pos.X, (std::int32_t)_pos.Y, _renderer.layer()), |
| 160 |
shellParams |
| 161 |
)); |
| 162 |
_levelHandler->AddActor(shell); |
| 163 |
|
| 164 |
Explosion::Create(_levelHandler, Vector3i((std::int32_t)_pos.X, (std::int32_t)_pos.Y, _renderer.layer() - 2), Explosion::Type::SmokeGray); |
| 165 |
|
| 166 |
CreateParticleDebrisOnPerish(ParticleDebrisEffect::Standard, Vector2f::Zero); |
| 167 |
_levelHandler->PlayCommonSfx("Splat"_s, Vector3f(_pos.X, _pos.Y, 0.0f)); |
| 168 |
|
| 169 |
StringView text = _levelHandler->GetLevelText(_endText); |
| 170 |
_levelHandler->ShowLevelText(text); |
| 171 |
|
| 172 |
return BossBase::OnPerish(collider); |
| 173 |
} |
| 174 |
|
| 175 |
void TurtleBoss::FollowNearestPlayer(std::int32_t newState, float time) |
| 176 |
{ |
| 177 |
bool found = false; |
| 178 |
Vector2f targetPos = Vector2f(FLT_MAX, FLT_MAX); |
| 179 |
|
| 180 |
auto players = _levelHandler->GetPlayers(); |
| 181 |
for (auto* player : players) { |
| 182 |
if (player->GetHealth() <= 0) { |
| 183 |
continue; |
| 184 |
} |
| 185 |
|
| 186 |
Vector2f newPos = player->GetPos(); |
| 187 |
if ((_pos - newPos).SqrLength() < (_pos - targetPos).SqrLength()) { |
| 188 |
targetPos = newPos; |
| 189 |
found = true; |
| 190 |
} |
| 191 |
} |
| 192 |
|
| 193 |
if (found) { |
| 194 |
_state = newState; |
| 195 |
_stateTime = time; |
| 196 |
|
| 197 |
SetFacingLeft(targetPos.X < _pos.X); |
| 198 |
|
| 199 |
_speed.X = (IsFacingLeft() ? -1.6f : 1.6f); |
| 200 |
|
| 201 |
SetAnimation(AnimState::Walk); |
| 202 |
} |
| 203 |
} |
| 204 |
|
| 205 |
TurtleBoss::Mace::~Mace() |
| 206 |
{ |
| 207 |
#if defined(WITH_AUDIO) |
| 208 |
if (_sound != nullptr) { |
| 209 |
_sound->stop(); |
| 210 |
_sound = nullptr; |
| 211 |
} |
| 212 |
#endif |
| 213 |
} |
| 214 |
|
| 215 |
Task<bool> TurtleBoss::Mace::OnActivatedAsync(const ActorActivationDetails& details) |
| 216 |
{ |
| 217 |
SetState(ActorState::IsInvulnerable, true); |
| 218 |
SetState(ActorState::CollideWithTileset | ActorState::CollideWithSolidObjects | ActorState::CanBeFrozen | ActorState::ApplyGravitation, false); |
| 219 |
CanCollideWithShots = false; |
| 220 |
|
| 221 |
_health = INT32_MAX; |
| 222 |
|
| 223 |
async_await RequestMetadataAsync("Boss/TurtleBoss"_s); |
| 224 |
SetAnimation((AnimState)1073741827); |
| 225 |
|
| 226 |
_originPos = _pos; |
| 227 |
|
| 228 |
FollowNearestPlayer(); |
| 229 |
|
| 230 |
#if defined(WITH_AUDIO) |
| 231 |
_sound = PlaySfx("Mace"_s, 0.7f); |
| 232 |
if (_sound != nullptr) { |
| 233 |
_sound->setLooping(true); |
| 234 |
} |
| 235 |
#endif |
| 236 |
|
| 237 |
async_return true; |
| 238 |
} |
| 239 |
|
| 240 |
void TurtleBoss::Mace::OnUpdate(float timeMult) |
| 241 |
{ |
| 242 |
MoveInstantly(Vector2f(_speed.X * timeMult, _speed.Y * timeMult), MoveType::Relative | MoveType::Force); |
| 243 |
|
| 244 |
#if defined(WITH_AUDIO) |
| 245 |
if (_sound != nullptr) { |
| 246 |
_sound->setPosition(Vector3f(_pos.X, _pos.Y, 0.8f)); |
| 247 |
} |
| 248 |
#endif |
| 249 |
|
| 250 |
if (_returning) { |
| 251 |
Vector2f diff = (_targetSpeed - _speed); |
| 252 |
if (diff.SqrLength() > 1.0f) { |
| 253 |
_speed.X += diff.X * 0.04f; |
| 254 |
_speed.Y += diff.Y * 0.04f; |
| 255 |
} |
| 256 |
|
| 257 |
} else { |
| 258 |
if (_returnTime > 0.0f) { |
| 259 |
_returnTime -= timeMult; |
| 260 |
} else { |
| 261 |
_returning = true; |
| 262 |
|
| 263 |
_targetSpeed = (_originPos - _pos) / (TotalTime / 2); |
| 264 |
} |
| 265 |
} |
| 266 |
} |
| 267 |
|
| 268 |
void TurtleBoss::Mace::OnUpdateHitbox() |
| 269 |
{ |
| 270 |
UpdateHitbox(18, 18); |
| 271 |
} |
| 272 |
|
| 273 |
void TurtleBoss::Mace::FollowNearestPlayer() |
| 274 |
{ |
| 275 |
bool found = false; |
| 276 |
Vector2f targetPos = Vector2f(FLT_MAX, FLT_MAX); |
| 277 |
|
| 278 |
auto players = _levelHandler->GetPlayers(); |
| 279 |
for (auto* player : players) { |
| 280 |
if (player->GetHealth() <= 0) { |
| 281 |
continue; |
| 282 |
} |
| 283 |
|
| 284 |
Vector2f newPos = player->GetPos(); |
| 285 |
if ((_pos - newPos).SqrLength() < (_pos - targetPos).SqrLength()) { |
| 286 |
targetPos = newPos; |
| 287 |
found = true; |
| 288 |
} |
| 289 |
} |
| 290 |
|
| 291 |
if (found) { |
| 292 |
SetFacingLeft(targetPos.X < _originPos.X); |
| 293 |
|
| 294 |
_returnTime = (TotalTime / 2); |
| 295 |
|
| 296 |
Vector2f diff = (targetPos - _originPos); |
| 297 |
float length = diff.Length(); |
| 298 |
if (length > 500.0f) { |
| 299 |
diff = diff * 500.0f / length; |
| 300 |
} |
| 301 |
|
| 302 |
_speed.X = (diff.X / _returnTime); |
| 303 |
_speed.Y = (diff.Y / _returnTime); |
| 304 |
} |
| 305 |
} |
| 306 |
} |