| 1 |
#include "Bilsy.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 |
Bilsy::Bilsy() |
| 13 |
: _state(StateTransition), _stateTime(0.0f), _endText(0) |
| 14 |
{ |
| 15 |
} |
| 16 |
|
| 17 |
void Bilsy::Preload(const ActorActivationDetails& details) |
| 18 |
{ |
| 19 |
uint8_t theme = details.Params[0]; |
| 20 |
switch (theme) { |
| 21 |
case 0: |
| 22 |
default: |
| 23 |
PreloadMetadataAsync("Boss/Bilsy"_s); |
| 24 |
break; |
| 25 |
case 1: // Xmas |
| 26 |
PreloadMetadataAsync("Boss/BilsyXmas"_s); |
| 27 |
break; |
| 28 |
} |
| 29 |
} |
| 30 |
|
| 31 |
Task<bool> Bilsy::OnActivatedAsync(const ActorActivationDetails& details) |
| 32 |
{ |
| 33 |
_theme = details.Params[0]; |
| 34 |
_endText = details.Params[1]; |
| 35 |
_originPos = _pos; |
| 36 |
_scoreValue = 3000; |
| 37 |
|
| 38 |
SetState(ActorState::ApplyGravitation, false); |
| 39 |
|
| 40 |
switch (_theme) { |
| 41 |
case 0: |
| 42 |
default: |
| 43 |
async_await RequestMetadataAsync("Boss/Bilsy"_s); |
| 44 |
break; |
| 45 |
case 1: // Xmas |
| 46 |
async_await RequestMetadataAsync("Boss/BilsyXmas"_s); |
| 47 |
break; |
| 48 |
} |
| 49 |
|
| 50 |
SetAnimation(AnimState::Idle); |
| 51 |
|
| 52 |
_renderer.setDrawEnabled(false); |
| 53 |
|
| 54 |
async_return true; |
| 55 |
} |
| 56 |
|
| 57 |
bool Bilsy::OnActivatedBoss() |
| 58 |
{ |
| 59 |
SetHealthByDifficulty(120); |
| 60 |
Teleport(); |
| 61 |
return true; |
| 62 |
} |
| 63 |
|
| 64 |
void Bilsy::OnUpdate(float timeMult) |
| 65 |
{ |
| 66 |
BossBase::OnUpdate(timeMult); |
| 67 |
|
| 68 |
if (_frozenTimeLeft > 0.0f) { |
| 69 |
return; |
| 70 |
} |
| 71 |
|
| 72 |
switch (_state) { |
| 73 |
case StateWaiting: { |
| 74 |
if (_stateTime <= 0.0f) { |
| 75 |
bool isFacingPlayer; |
| 76 |
if (_levelHandler->GetDifficulty() < GameDifficulty::Hard) { |
| 77 |
bool found = false; |
| 78 |
Vector2f targetPos = Vector2f(FLT_MAX, FLT_MAX); |
| 79 |
|
| 80 |
auto players = _levelHandler->GetPlayers(); |
| 81 |
for (auto* player : players) { |
| 82 |
if (player->GetHealth() <= 0) { |
| 83 |
continue; |
| 84 |
} |
| 85 |
|
| 86 |
Vector2f newPos = player->GetPos(); |
| 87 |
if ((_pos - newPos).SqrLength() < (_pos - targetPos).SqrLength()) { |
| 88 |
targetPos = newPos; |
| 89 |
found = true; |
| 90 |
} |
| 91 |
} |
| 92 |
|
| 93 |
isFacingPlayer = (found && (targetPos.X < _pos.X) == IsFacingLeft()); |
| 94 |
} else { |
| 95 |
isFacingPlayer = true; |
| 96 |
} |
| 97 |
|
| 98 |
if (isFacingPlayer) { |
| 99 |
_state = StateTransition; |
| 100 |
SetTransition((AnimState)1073741826, false, [this]() { |
| 101 |
PlaySfx("ThrowFireball"_s); |
| 102 |
|
| 103 |
std::shared_ptr<Fireball> fireball = std::make_shared<Fireball>(); |
| 104 |
uint8_t fireballParams[2] = { _theme, (uint8_t)(IsFacingLeft() ? 1 : 0) }; |
| 105 |
fireball->OnActivated(ActorActivationDetails( |
| 106 |
_levelHandler, |
| 107 |
Vector3i((std::int32_t)_pos.X + (IsFacingLeft() ? -26 : 26), (std::int32_t)_pos.Y - 20, _renderer.layer() + 2), |
| 108 |
fireballParams |
| 109 |
)); |
| 110 |
_levelHandler->AddActor(fireball); |
| 111 |
|
| 112 |
SetTransition((AnimState)1073741827, false, [this]() { |
| 113 |
_state = StateWaiting2; |
| 114 |
_stateTime = 30.0f; |
| 115 |
}); |
| 116 |
}); |
| 117 |
} else { |
| 118 |
_state = StateWaiting2; |
| 119 |
_stateTime = 30.0f; |
| 120 |
} |
| 121 |
} |
| 122 |
break; |
| 123 |
} |
| 124 |
case StateWaiting2: { |
| 125 |
if (_stateTime <= 0.0f) { |
| 126 |
SetState(ActorState::CanBeFrozen, false); |
| 127 |
|
| 128 |
PlaySfx("Disappear"_s, 0.8f); |
| 129 |
|
| 130 |
if (_levelHandler->GetDifficulty() < GameDifficulty::Hard) { |
| 131 |
_canHurtPlayer = false; |
| 132 |
} |
| 133 |
|
| 134 |
_state = StateTransition; |
| 135 |
SetTransition((AnimState)1073741825, false, [this]() { |
| 136 |
Teleport(); |
| 137 |
}); |
| 138 |
} |
| 139 |
break; |
| 140 |
} |
| 141 |
} |
| 142 |
|
| 143 |
_stateTime -= timeMult; |
| 144 |
} |
| 145 |
|
| 146 |
void Bilsy::OnUpdateHitbox() |
| 147 |
{ |
| 148 |
UpdateHitbox(20, 60); |
| 149 |
} |
| 150 |
|
| 151 |
bool Bilsy::OnPerish(ActorBase* collider) |
| 152 |
{ |
| 153 |
CreateParticleDebrisOnPerish(collider); |
| 154 |
_levelHandler->PlayCommonSfx("Splat"_s, Vector3f(_pos.X, _pos.Y, 0.0f)); |
| 155 |
|
| 156 |
StringView text = _levelHandler->GetLevelText(_endText); |
| 157 |
_levelHandler->ShowLevelText(text); |
| 158 |
|
| 159 |
return BossBase::OnPerish(collider); |
| 160 |
} |
| 161 |
|
| 162 |
void Bilsy::Teleport() |
| 163 |
{ |
| 164 |
for (std::int32_t i = 0; i < 20; i++) { |
| 165 |
Vector2f pos = Vector2f(_originPos.X + Random().NextFloat(-320.0f, 320.0f), _originPos.Y + Random().NextFloat(-240.0f, 240.0f)); |
| 166 |
if (MoveInstantly(pos, MoveType::Absolute)) { |
| 167 |
break; |
| 168 |
} |
| 169 |
} |
| 170 |
|
| 171 |
OnUpdateHitbox(); |
| 172 |
|
| 173 |
std::int32_t j = 60; |
| 174 |
while (j-- > 0 && MoveInstantly(Vector2f(0.0f, 4.0f), MoveType::Relative)) { |
| 175 |
// Nothing to do... |
| 176 |
} |
| 177 |
while (j-- > 0 && MoveInstantly(Vector2f(0.0f, 1.0f), MoveType::Relative)) { |
| 178 |
// Nothing to do... |
| 179 |
} |
| 180 |
|
| 181 |
bool found = false; |
| 182 |
Vector2f targetPos = Vector2f(FLT_MAX, FLT_MAX); |
| 183 |
|
| 184 |
auto players = _levelHandler->GetPlayers(); |
| 185 |
for (auto* player : players) { |
| 186 |
if (player->GetHealth() <= 0) { |
| 187 |
continue; |
| 188 |
} |
| 189 |
|
| 190 |
Vector2f newPos = player->GetPos(); |
| 191 |
if ((_pos - newPos).SqrLength() < (_pos - targetPos).SqrLength()) { |
| 192 |
targetPos = newPos; |
| 193 |
found = true; |
| 194 |
} |
| 195 |
} |
| 196 |
|
| 197 |
if (found) { |
| 198 |
SetFacingLeft(targetPos.X < _pos.X); |
| 199 |
} |
| 200 |
|
| 201 |
_renderer.setDrawEnabled(true); |
| 202 |
|
| 203 |
_state = StateTransition; |
| 204 |
SetTransition((AnimState)1073741824, false, [this]() { |
| 205 |
SetState(ActorState::CanBeFrozen, true); |
| 206 |
|
| 207 |
if (_levelHandler->GetDifficulty() < GameDifficulty::Hard) { |
| 208 |
_canHurtPlayer = true; |
| 209 |
} |
| 210 |
|
| 211 |
_state = StateWaiting; |
| 212 |
_stateTime = 30.0f; |
| 213 |
}); |
| 214 |
|
| 215 |
PlaySfx("Appear"_s, 0.8f); |
| 216 |
} |
| 217 |
|
| 218 |
Task<bool> Bilsy::Fireball::OnActivatedAsync(const ActorActivationDetails& details) |
| 219 |
{ |
| 220 |
uint8_t theme = details.Params[0]; |
| 221 |
SetFacingLeft(details.Params[1] != 0); |
| 222 |
_timeLeft = 90.0f; |
| 223 |
|
| 224 |
SetState(ActorState::IsInvulnerable, true); |
| 225 |
SetState(ActorState::CanBeFrozen | ActorState::ApplyGravitation, false); |
| 226 |
CanCollideWithShots = false; |
| 227 |
|
| 228 |
_health = INT32_MAX; |
| 229 |
_speed.X = (IsFacingLeft() ? -4.0f : 4.0f); |
| 230 |
_speed.Y = 2.0f; |
| 231 |
|
| 232 |
switch (theme) { |
| 233 |
case 0: |
| 234 |
default: |
| 235 |
async_await RequestMetadataAsync("Boss/Bilsy"_s); |
| 236 |
break; |
| 237 |
|
| 238 |
case 1: // Xmas |
| 239 |
async_await RequestMetadataAsync("Boss/BilsyXmas"_s); |
| 240 |
break; |
| 241 |
} |
| 242 |
|
| 243 |
SetAnimation((AnimState)1073741828); |
| 244 |
|
| 245 |
PlaySfx("FireStart"_s); |
| 246 |
|
| 247 |
async_return true; |
| 248 |
} |
| 249 |
|
| 250 |
void Bilsy::Fireball::OnUpdate(float timeMult) |
| 251 |
{ |
| 252 |
MoveInstantly(Vector2f(_speed.X * timeMult, _speed.Y * timeMult), MoveType::Relative | MoveType::Force); |
| 253 |
|
| 254 |
if (_timeLeft <= 0.0f) { |
| 255 |
DecreaseHealth(INT32_MAX); |
| 256 |
} else { |
| 257 |
_timeLeft -= timeMult; |
| 258 |
FollowNearestPlayer(); |
| 259 |
} |
| 260 |
|
| 261 |
// TODO: Spawn fire particles |
| 262 |
} |
| 263 |
|
| 264 |
void Bilsy::Fireball::OnUpdateHitbox() |
| 265 |
{ |
| 266 |
UpdateHitbox(18, 18); |
| 267 |
} |
| 268 |
|
| 269 |
void Bilsy::Fireball::OnEmitLights(SmallVectorImpl<LightEmitter>& lights) |
| 270 |
{ |
| 271 |
auto& light = lights.emplace_back(); |
| 272 |
light.Pos = _pos; |
| 273 |
light.Intensity = 0.85f; |
| 274 |
light.Brightness = 0.4f; |
| 275 |
light.RadiusNear = 0.0f; |
| 276 |
light.RadiusFar = 30.0f; |
| 277 |
} |
| 278 |
|
| 279 |
bool Bilsy::Fireball::OnHandleCollision(std::shared_ptr<ActorBase> other) |
| 280 |
{ |
| 281 |
if (auto* player = runtime_cast<Player>(other.get())) { |
| 282 |
DecreaseHealth(INT32_MAX); |
| 283 |
} |
| 284 |
|
| 285 |
return ActorBase::OnHandleCollision(std::move(other)); |
| 286 |
} |
| 287 |
|
| 288 |
bool Bilsy::Fireball::OnPerish(ActorBase* collider) |
| 289 |
{ |
| 290 |
Explosion::Create(_levelHandler, Vector3i((std::int32_t)(_pos.X + _speed.X), (std::int32_t)(_pos.Y + _speed.Y), _renderer.layer() + 2), Explosion::Type::RF); |
| 291 |
|
| 292 |
return EnemyBase::OnPerish(collider); |
| 293 |
} |
| 294 |
|
| 295 |
void Bilsy::Fireball::FollowNearestPlayer() |
| 296 |
{ |
| 297 |
bool found = false; |
| 298 |
Vector2f targetPos = Vector2f(FLT_MAX, FLT_MAX); |
| 299 |
|
| 300 |
auto players = _levelHandler->GetPlayers(); |
| 301 |
for (auto* player : players) { |
| 302 |
if (player->GetHealth() <= 0) { |
| 303 |
continue; |
| 304 |
} |
| 305 |
|
| 306 |
Vector2f newPos = player->GetPos(); |
| 307 |
if ((_pos - newPos).Length() < (_pos - targetPos).Length()) { |
| 308 |
targetPos = newPos; |
| 309 |
found = true; |
| 310 |
} |
| 311 |
} |
| 312 |
|
| 313 |
if (found) { |
| 314 |
Vector2f diff = (targetPos - _pos).Normalized(); |
| 315 |
Vector2f speed = (Vector2f(_speed.X, _speed.Y) + diff * 0.4f).Normalized(); |
| 316 |
_speed.X = speed.X * 4.0f; |
| 317 |
_speed.Y = speed.Y * 4.0f; |
| 318 |
|
| 319 |
if (_speed.X < 0.0f) { |
| 320 |
SetFacingLeft(true); |
| 321 |
_renderer.setRotation(atan2f(-_speed.Y, -_speed.X)); |
| 322 |
} else { |
| 323 |
SetFacingLeft(false); |
| 324 |
_renderer.setRotation(atan2f(_speed.Y, _speed.X)); |
| 325 |
} |
| 326 |
} |
| 327 |
} |
| 328 |
} |