| 1 |
#include "Bolly.h" |
| 2 |
#include "../../../ILevelHandler.h" |
| 3 |
#include "../Crab.h" |
| 4 |
#include "../../Player.h" |
| 5 |
#include "../../Explosion.h" |
| 6 |
#include "../../Weapons/ShotBase.h" |
| 7 |
|
| 8 |
#include "../../../../nCine/Base/Random.h" |
| 9 |
|
| 10 |
#include <float.h> |
| 11 |
|
| 12 |
namespace Jazz2::Actors::Bosses |
| 13 |
{ |
| 14 |
Bolly::Bolly() |
| 15 |
: _state(StateWaiting), _stateTime(0.0f), _endText(0), _noiseCooldown(0.0f), _rocketsLeft(0), _chainPhase(0.0f) |
| 16 |
{ |
| 17 |
} |
| 18 |
|
| 19 |
Bolly::~Bolly() |
| 20 |
{ |
| 21 |
if (_bottom != nullptr) { |
| 22 |
_bottom->SetState(ActorState::IsDestroyed, true); |
| 23 |
} |
| 24 |
/*if (_turret != nullptr) { |
| 25 |
_turret->SetState(ActorState::IsDestroyed, true); |
| 26 |
}*/ |
| 27 |
for (std::int32_t i = 0; i < std::int32_t(arraySize(_chain)); i++) { |
| 28 |
if (_chain[i] != nullptr) { |
| 29 |
_chain[i]->SetState(ActorState::IsDestroyed, true); |
| 30 |
} |
| 31 |
} |
| 32 |
} |
| 33 |
|
| 34 |
void Bolly::Preload(const ActorActivationDetails& details) |
| 35 |
{ |
| 36 |
PreloadMetadataAsync("Boss/Bolly"_s); |
| 37 |
} |
| 38 |
|
| 39 |
Task<bool> Bolly::OnActivatedAsync(const ActorActivationDetails& details) |
| 40 |
{ |
| 41 |
_endText = details.Params[1]; |
| 42 |
_originPos = _pos; |
| 43 |
|
| 44 |
SetState(ActorState::CollideWithTileset | ActorState::CollideWithSolidObjects | ActorState::CanBeFrozen | ActorState::ApplyGravitation, false); |
| 45 |
|
| 46 |
_scoreValue = 3000; |
| 47 |
|
| 48 |
async_await RequestMetadataAsync("Boss/Bolly"_s); |
| 49 |
SetAnimation(AnimState::Idle); |
| 50 |
|
| 51 |
_bottom = std::make_shared<BollyPart>(); |
| 52 |
std::uint8_t bottomParams[1] = { 1 }; |
| 53 |
_bottom->OnActivated(ActorActivationDetails( |
| 54 |
_levelHandler, |
| 55 |
Vector3i((std::int32_t)_pos.X, (std::int32_t)_pos.Y, _renderer.layer() + 2), |
| 56 |
bottomParams |
| 57 |
)); |
| 58 |
_levelHandler->AddActor(_bottom); |
| 59 |
|
| 60 |
/*_turret = std::make_shared<BollyPart>(); |
| 61 |
uint8_t turretParams[1] = { 2 }; |
| 62 |
_turret->OnActivated({ |
| 63 |
.LevelHandler = _levelHandler, |
| 64 |
.Pos = Vector3i((int)_pos.X, (int)_pos.Y, _renderer.layer() + 6), |
| 65 |
.Params = turretParams |
| 66 |
}); |
| 67 |
_levelHandler->AddActor(_turret);*/ |
| 68 |
|
| 69 |
std::int32_t chainLength = (_levelHandler->GetDifficulty() < GameDifficulty::Hard ? NormalChainLength : HardChainLength); |
| 70 |
for (std::int32_t i = 0; i < chainLength; i++) { |
| 71 |
_chain[i] = std::make_shared<BollyPart>(); |
| 72 |
std::uint8_t chainParams[1] = { (std::uint8_t)((i % 3) == 2 ? 3 : 4) }; |
| 73 |
_chain[i]->OnActivated(ActorActivationDetails( |
| 74 |
_levelHandler, |
| 75 |
Vector3i((std::int32_t)_pos.X, (std::int32_t)_pos.Y, _renderer.layer() - ((i % 3) == 2 ? 2 : 4)), |
| 76 |
chainParams |
| 77 |
)); |
| 78 |
_levelHandler->AddActor(_chain[i]); |
| 79 |
} |
| 80 |
|
| 81 |
async_return true; |
| 82 |
} |
| 83 |
|
| 84 |
bool Bolly::OnActivatedBoss() |
| 85 |
{ |
| 86 |
SetHealthByDifficulty(42); |
| 87 |
MoveInstantly(_originPos, MoveType::Absolute | MoveType::Force); |
| 88 |
FollowNearestPlayer(StateFlying, 100.0f); |
| 89 |
return true; |
| 90 |
} |
| 91 |
|
| 92 |
void Bolly::OnUpdate(float timeMult) |
| 93 |
{ |
| 94 |
HandleBlinking(timeMult); |
| 95 |
|
| 96 |
MoveInstantly(Vector2f(_speed.X * timeMult, _speed.Y * timeMult), MoveType::Relative | MoveType::Force); |
| 97 |
|
| 98 |
switch (_state) { |
| 99 |
case StateFlying: { |
| 100 |
if (_stateTime <= 0.0f) { |
| 101 |
if (Random().NextFloat() < 0.1f) { |
| 102 |
_state = StatePrepairingToAttack; |
| 103 |
_stateTime = 120.0f; |
| 104 |
_speed.X = 0; |
| 105 |
_speed.Y = 0; |
| 106 |
} else { |
| 107 |
_state = StateNewDirection; |
| 108 |
_stateTime = 50.0f; |
| 109 |
} |
| 110 |
} |
| 111 |
break; |
| 112 |
} |
| 113 |
|
| 114 |
case StateNewDirection: { |
| 115 |
if (_stateTime <= 0.0f) { |
| 116 |
FollowNearestPlayer(StateFlying, 1.0f); |
| 117 |
} |
| 118 |
break; |
| 119 |
} |
| 120 |
|
| 121 |
case StatePrepairingToAttack: { |
| 122 |
if (_stateTime <= 0.0f) { |
| 123 |
_state = StateAttacking; |
| 124 |
_stateTime = 20.0f; |
| 125 |
_rocketsLeft = 5; |
| 126 |
|
| 127 |
PlaySfx("PreAttack"_s); |
| 128 |
} |
| 129 |
break; |
| 130 |
} |
| 131 |
|
| 132 |
case StateAttacking: { |
| 133 |
if (_stateTime <= 0.0f) { |
| 134 |
if (_rocketsLeft > 0) { |
| 135 |
_stateTime = 20.0f; |
| 136 |
|
| 137 |
FireRocket(); |
| 138 |
_rocketsLeft--; |
| 139 |
|
| 140 |
PlaySfx("Attack"_s); |
| 141 |
} else { |
| 142 |
_state = StateNewDirection; |
| 143 |
_stateTime = 100.0f; |
| 144 |
|
| 145 |
PlaySfx("PostAttack"_s); |
| 146 |
} |
| 147 |
} |
| 148 |
break; |
| 149 |
} |
| 150 |
} |
| 151 |
|
| 152 |
if (_bottom != nullptr) { |
| 153 |
_bottom->MoveInstantly(Vector2f(_pos.X + _speed.X * timeMult, _pos.Y + _speed.Y * timeMult), MoveType::Absolute | MoveType::Force); |
| 154 |
_bottom->SetFacingLeft(IsFacingLeft()); |
| 155 |
} |
| 156 |
/*if (_turret != nullptr) { |
| 157 |
UpdateTurret(timeMult); |
| 158 |
}*/ |
| 159 |
|
| 160 |
float distance = 30.0f; |
| 161 |
for (std::int32_t i = 0; i < std::int32_t(arraySize(_chain)); i++) { |
| 162 |
if (_chain[i] != nullptr) { |
| 163 |
float angle = sinf(_chainPhase - i * 0.08f) * 1.2f + fPiOver2; |
| 164 |
|
| 165 |
Vector2f piecePos = _pos; |
| 166 |
piecePos.X += cosf(angle) * distance; |
| 167 |
piecePos.Y += sinf(angle) * distance; |
| 168 |
_chain[i]->MoveInstantly(piecePos, MoveType::Absolute | MoveType::Force); |
| 169 |
|
| 170 |
distance += _chain[i]->Size; |
| 171 |
} |
| 172 |
} |
| 173 |
|
| 174 |
if (_noiseCooldown > 0.0f) { |
| 175 |
_noiseCooldown -= timeMult; |
| 176 |
} else { |
| 177 |
_noiseCooldown = 120.0f; |
| 178 |
PlaySfx("Noise"_s, 0.2f); |
| 179 |
} |
| 180 |
|
| 181 |
_stateTime -= timeMult; |
| 182 |
_chainPhase += timeMult * 0.06f; |
| 183 |
} |
| 184 |
|
| 185 |
bool Bolly::OnPerish(ActorBase* collider) |
| 186 |
{ |
| 187 |
Explosion::Create(_levelHandler, Vector3i(std::int32_t(_pos.X), std::int32_t(_pos.Y), _renderer.layer() + 2), Explosion::Type::Large); |
| 188 |
|
| 189 |
CreateParticleDebrisOnPerish(collider); |
| 190 |
_levelHandler->PlayCommonSfx("Splat"_s, Vector3f(_pos.X, _pos.Y, 0.0f)); |
| 191 |
|
| 192 |
StringView text = _levelHandler->GetLevelText(_endText); |
| 193 |
_levelHandler->ShowLevelText(text); |
| 194 |
|
| 195 |
return BossBase::OnPerish(collider); |
| 196 |
} |
| 197 |
|
| 198 |
/*void Bolly::UpdateTurret(float timeMult) |
| 199 |
{ |
| 200 |
bool found = false; |
| 201 |
Vector2f targetPos = Vector2f(FLT_MAX, FLT_MAX); |
| 202 |
|
| 203 |
auto& players = _levelHandler->GetPlayers(); |
| 204 |
for (auto& player : players) { |
| 205 |
Vector2f newPos = player->GetPos(); |
| 206 |
if ((_pos - newPos).Length() < (_pos - targetPos).Length()) { |
| 207 |
targetPos = newPos; |
| 208 |
found = true; |
| 209 |
} |
| 210 |
} |
| 211 |
|
| 212 |
if (found) { |
| 213 |
bool facingLeft = (targetPos.X < _pos.X); |
| 214 |
Vector2f diff = (targetPos - _pos).Normalized(); |
| 215 |
_turret->_renderer.setRotation(facingLeft ? atan2f(-diff.Y, -diff.X) : atan2f(diff.Y, diff.X)); |
| 216 |
_turret->SetFacingLeft(facingLeft); |
| 217 |
} |
| 218 |
|
| 219 |
_turret->MoveInstantly(Vector2f(_pos.X + (IsFacingLeft() ? 10.0f : -10.0f) + _speed.X * timeMult, _pos.Y + 10.0f + _speed.Y * timeMult), MoveType::Absolute | MoveType::Force); |
| 220 |
}*/ |
| 221 |
|
| 222 |
void Bolly::FollowNearestPlayer(std::int32_t newState, float time) |
| 223 |
{ |
| 224 |
bool found = false; |
| 225 |
Vector2f targetPos = Vector2f(FLT_MAX, FLT_MAX); |
| 226 |
|
| 227 |
auto players = _levelHandler->GetPlayers(); |
| 228 |
for (auto* player : players) { |
| 229 |
if (player->GetHealth() <= 0) { |
| 230 |
continue; |
| 231 |
} |
| 232 |
|
| 233 |
Vector2f newPos = player->GetPos(); |
| 234 |
if ((_pos - newPos).SqrLength() < (_pos - targetPos).SqrLength()) { |
| 235 |
targetPos = newPos; |
| 236 |
found = true; |
| 237 |
} |
| 238 |
} |
| 239 |
|
| 240 |
if (found) { |
| 241 |
_state = newState; |
| 242 |
_stateTime = time; |
| 243 |
|
| 244 |
targetPos.Y += Random().FastFloat(-100.0f, 20.0f); |
| 245 |
|
| 246 |
SetFacingLeft(targetPos.X < _pos.X); |
| 247 |
|
| 248 |
float speedMultiplier = (_levelHandler->GetDifficulty() < GameDifficulty::Hard ? 0.6f : 0.8f); |
| 249 |
Vector2f speed = (targetPos - _pos).Normalized(); |
| 250 |
_speed.X = speed.X * speedMultiplier; |
| 251 |
_speed.Y = speed.Y * speedMultiplier; |
| 252 |
} |
| 253 |
} |
| 254 |
|
| 255 |
void Bolly::FireRocket() |
| 256 |
{ |
| 257 |
bool found = false; |
| 258 |
Vector2f targetPos = Vector2f(FLT_MAX, FLT_MAX); |
| 259 |
|
| 260 |
auto players = _levelHandler->GetPlayers(); |
| 261 |
for (auto* player : players) { |
| 262 |
if (player->GetHealth() <= 0) { |
| 263 |
continue; |
| 264 |
} |
| 265 |
|
| 266 |
Vector2f newPos = player->GetPos(); |
| 267 |
if ((_pos - newPos).SqrLength() < (_pos - targetPos).SqrLength()) { |
| 268 |
targetPos = newPos; |
| 269 |
found = true; |
| 270 |
} |
| 271 |
} |
| 272 |
|
| 273 |
if (found) { |
| 274 |
Vector2f diff = (targetPos - _pos).Normalized(); |
| 275 |
|
| 276 |
std::shared_ptr<Rocket> rocket = std::make_shared<Rocket>(); |
| 277 |
rocket->OnActivated(ActorActivationDetails( |
| 278 |
_levelHandler, |
| 279 |
Vector3i((std::int32_t)_pos.X + (IsFacingLeft() ? 10 : -10), (std::int32_t)_pos.Y + 10, _renderer.layer() - 4) |
| 280 |
)); |
| 281 |
rocket->_renderer.setRotation(atan2f(diff.Y, diff.X)); |
| 282 |
_levelHandler->AddActor(rocket); |
| 283 |
} |
| 284 |
} |
| 285 |
|
| 286 |
Task<bool> Bolly::BollyPart::OnActivatedAsync(const ActorActivationDetails& details) |
| 287 |
{ |
| 288 |
uint8_t partType = details.Params[0]; |
| 289 |
|
| 290 |
SetState(ActorState::IsInvulnerable, true); |
| 291 |
SetState(ActorState::CollideWithTileset | ActorState::CollideWithSolidObjects | ActorState::CanBeFrozen | ActorState::ApplyGravitation, false); |
| 292 |
|
| 293 |
_health = INT32_MAX; |
| 294 |
|
| 295 |
async_await RequestMetadataAsync("Boss/Bolly"_s); |
| 296 |
SetAnimation((AnimState)partType); |
| 297 |
|
| 298 |
switch (partType) { |
| 299 |
case 1: // Bottom |
| 300 |
SetState(ActorState::IsSolidObject, true); |
| 301 |
SetState(ActorState::CollideWithOtherActors, false); |
| 302 |
CanCollideWithShots = true; |
| 303 |
Size = 0.0f; |
| 304 |
break; |
| 305 |
case 2: // Turret |
| 306 |
SetState(ActorState::CollideWithOtherActors, false); |
| 307 |
CanCollideWithShots = false; |
| 308 |
Size = 0.0f; |
| 309 |
break; |
| 310 |
case 3: // Chain 1 |
| 311 |
CanCollideWithShots = false; |
| 312 |
Size = 14.0f; |
| 313 |
break; |
| 314 |
case 4: // Chain 2 |
| 315 |
CanCollideWithShots = false; |
| 316 |
Size = 7.0f; |
| 317 |
break; |
| 318 |
} |
| 319 |
|
| 320 |
async_return true; |
| 321 |
} |
| 322 |
|
| 323 |
void Bolly::BollyPart::OnUpdate(float timeMult) |
| 324 |
{ |
| 325 |
} |
| 326 |
|
| 327 |
Task<bool> Bolly::Rocket::OnActivatedAsync(const ActorActivationDetails& details) |
| 328 |
{ |
| 329 |
SetState(ActorState::IsInvulnerable, true); |
| 330 |
SetState(ActorState::CanBeFrozen | ActorState::ApplyGravitation, false); |
| 331 |
CanCollideWithShots = false; |
| 332 |
|
| 333 |
_timeLeft = 300.0f; |
| 334 |
_health = INT32_MAX; |
| 335 |
|
| 336 |
async_await RequestMetadataAsync("Boss/Bolly"_s); |
| 337 |
SetAnimation((AnimState)5); |
| 338 |
|
| 339 |
async_return true; |
| 340 |
} |
| 341 |
|
| 342 |
void Bolly::Rocket::OnUpdate(float timeMult) |
| 343 |
{ |
| 344 |
float angle = _renderer.rotation(); |
| 345 |
_speed.X += cosf(angle) * 0.14f * timeMult; |
| 346 |
_speed.Y += sinf(angle) * 0.14f * timeMult; |
| 347 |
|
| 348 |
EnemyBase::OnUpdate(timeMult); |
| 349 |
|
| 350 |
if (_timeLeft <= 0.0f) { |
| 351 |
DecreaseHealth(INT32_MAX); |
| 352 |
} else { |
| 353 |
_timeLeft -= timeMult; |
| 354 |
} |
| 355 |
} |
| 356 |
|
| 357 |
void Bolly::Rocket::OnUpdateHitbox() |
| 358 |
{ |
| 359 |
UpdateHitbox(20, 20); |
| 360 |
} |
| 361 |
|
| 362 |
void Bolly::Rocket::OnEmitLights(SmallVectorImpl<LightEmitter>& lights) |
| 363 |
{ |
| 364 |
auto& light = lights.emplace_back(); |
| 365 |
light.Pos = _pos; |
| 366 |
light.Intensity = 0.8f; |
| 367 |
light.Brightness = 0.8f; |
| 368 |
light.RadiusNear = 3.0f; |
| 369 |
light.RadiusFar = 12.0f; |
| 370 |
} |
| 371 |
|
| 372 |
bool Bolly::Rocket::OnHandleCollision(std::shared_ptr<ActorBase> other) |
| 373 |
{ |
| 374 |
if (auto* player = runtime_cast<Player>(other.get())) { |
| 375 |
DecreaseHealth(INT32_MAX); |
| 376 |
} |
| 377 |
|
| 378 |
return ActorBase::OnHandleCollision(std::move(other)); |
| 379 |
} |
| 380 |
|
| 381 |
bool Bolly::Rocket::OnPerish(ActorBase* collider) |
| 382 |
{ |
| 383 |
Explosion::Create(_levelHandler, Vector3i((std::int32_t)_pos.X, (std::int32_t)_pos.Y, _renderer.layer() + 2), Explosion::Type::RF); |
| 384 |
|
| 385 |
return EnemyBase::OnPerish(collider); |
| 386 |
} |
| 387 |
|
| 388 |
void Bolly::Rocket::OnHitFloor(float timeMult) |
| 389 |
{ |
| 390 |
DecreaseHealth(INT32_MAX); |
| 391 |
} |
| 392 |
|
| 393 |
void Bolly::Rocket::OnHitWall(float timeMult) |
| 394 |
{ |
| 395 |
DecreaseHealth(INT32_MAX); |
| 396 |
} |
| 397 |
|
| 398 |
void Bolly::Rocket::OnHitCeiling(float timeMult) |
| 399 |
{ |
| 400 |
DecreaseHealth(INT32_MAX); |
| 401 |
} |
| 402 |
} |