| 1 |
#include "Bird.h" |
| 2 |
#include "../../ILevelHandler.h" |
| 3 |
#include "../Player.h" |
| 4 |
#include "../Enemies/EnemyBase.h" |
| 5 |
#include "../Weapons/BlasterShot.h" |
| 6 |
|
| 7 |
using namespace Jazz2::Tiles; |
| 8 |
|
| 9 |
namespace Jazz2::Actors::Environment |
| 10 |
{ |
| 11 |
Bird::Bird() |
| 12 |
: _owner(nullptr), _fireCooldown(60.0f), _attackTime(0.0f) |
| 13 |
{ |
| 14 |
} |
| 15 |
|
| 16 |
void Bird::Preload(const ActorActivationDetails& details) |
| 17 |
{ |
| 18 |
std::uint8_t type = details.Params[0]; |
| 19 |
switch (type) { |
| 20 |
case 0: // Chuck (red) |
| 21 |
PreloadMetadataAsync("Object/BirdChuck"_s); |
| 22 |
break; |
| 23 |
case 1: // Birdy (yellow) |
| 24 |
PreloadMetadataAsync("Object/BirdBirdy"_s); |
| 25 |
break; |
| 26 |
} |
| 27 |
} |
| 28 |
|
| 29 |
Task<bool> Bird::OnActivatedAsync(const ActorActivationDetails& details) |
| 30 |
{ |
| 31 |
SetState(ActorState::SkipPerPixelCollisions, true); |
| 32 |
SetState(ActorState::CollideWithTileset | ActorState::CollideWithSolidObjects | ActorState::CollideWithOtherActors | ActorState::ApplyGravitation, false); |
| 33 |
|
| 34 |
_type = details.Params[0]; |
| 35 |
|
| 36 |
std::uint8_t playerIndex = details.Params[1]; |
| 37 |
for (auto* player : _levelHandler->GetPlayers()) { |
| 38 |
if (player->GetPlayerIndex() == playerIndex) { |
| 39 |
_owner = player; |
| 40 |
break; |
| 41 |
} |
| 42 |
} |
| 43 |
|
| 44 |
switch (_type) { |
| 45 |
case 0: // Chuck (red) |
| 46 |
async_await RequestMetadataAsync("Object/BirdChuck"_s); |
| 47 |
break; |
| 48 |
case 1: // Birdy (yellow) |
| 49 |
async_await RequestMetadataAsync("Object/BirdBirdy"_s); |
| 50 |
break; |
| 51 |
} |
| 52 |
|
| 53 |
SetAnimation(AnimState::Idle); |
| 54 |
|
| 55 |
async_return true; |
| 56 |
} |
| 57 |
|
| 58 |
void Bird::OnUpdate(float timeMult) |
| 59 |
{ |
| 60 |
if (_owner == nullptr) { |
| 61 |
// Fly away |
| 62 |
if (_fireCooldown <= 0.0f) { |
| 63 |
DecreaseHealth(INT32_MAX); |
| 64 |
} else { |
| 65 |
_fireCooldown -= timeMult; |
| 66 |
MoveInstantly(Vector2f((IsFacingLeft() ? -8.0f : 8.0f) * timeMult, -1.0f * timeMult), MoveType::Relative | MoveType::Force); |
| 67 |
_renderer.setScale(_renderer.scale() + 0.014f * timeMult); |
| 68 |
} |
| 69 |
return; |
| 70 |
} |
| 71 |
|
| 72 |
if (_type == 1 && _attackTime > 0.0f) { |
| 73 |
// Attack |
| 74 |
MoveInstantly(Vector2f(_speed.X * timeMult, _speed.Y * timeMult), MoveType::Relative | MoveType::Force); |
| 75 |
|
| 76 |
_attackTime -= timeMult; |
| 77 |
|
| 78 |
if (_attackTime <= 0.0f) { |
| 79 |
SetAnimation(AnimState::Idle); |
| 80 |
SetState(ActorState::CollideWithOtherActors, false); |
| 81 |
_renderer.setRotation(0.0f); |
| 82 |
} |
| 83 |
} else { |
| 84 |
// Follow player |
| 85 |
Vector2f targetPos = _owner->GetPos(); |
| 86 |
bool playerFacingLeft = _owner->IsFacingLeft(); |
| 87 |
|
| 88 |
if (playerFacingLeft) { |
| 89 |
targetPos.X += 55.0f; |
| 90 |
} else { |
| 91 |
targetPos.X -= 55.0f; |
| 92 |
} |
| 93 |
targetPos.Y -= 50.0f; |
| 94 |
|
| 95 |
if (IsFacingLeft() != playerFacingLeft) { |
| 96 |
SetFacingLeft(playerFacingLeft); |
| 97 |
_fireCooldown = 40.0f; |
| 98 |
} |
| 99 |
|
| 100 |
targetPos.X = lerp(_pos.X, targetPos.X, 0.02f * timeMult); |
| 101 |
targetPos.Y = lerp(_pos.Y, targetPos.Y, 0.02f * timeMult); |
| 102 |
MoveInstantly(targetPos, MoveType::Absolute | MoveType::Force); |
| 103 |
} |
| 104 |
|
| 105 |
// Fire |
| 106 |
if (_fireCooldown <= 0.0f) { |
| 107 |
TryFire(); |
| 108 |
} else { |
| 109 |
_fireCooldown -= timeMult; |
| 110 |
} |
| 111 |
} |
| 112 |
|
| 113 |
void Bird::OnAnimationFinished() |
| 114 |
{ |
| 115 |
ActorBase::OnAnimationFinished(); |
| 116 |
|
| 117 |
PlaySfx("Fly"_s, 0.3f); |
| 118 |
} |
| 119 |
|
| 120 |
bool Bird::OnHandleCollision(std::shared_ptr<ActorBase> other) |
| 121 |
{ |
| 122 |
if (_attackTime > 0.0f && !other->IsInvulnerable()) { |
| 123 |
if (auto* enemy = runtime_cast<Enemies::EnemyBase>(other.get())) { |
| 124 |
enemy->DecreaseHealth(1, this); |
| 125 |
|
| 126 |
SetAnimation(AnimState::Idle); |
| 127 |
_attackTime = 0.0f; |
| 128 |
SetState(ActorState::CollideWithOtherActors, false); |
| 129 |
_renderer.setRotation(0.0f); |
| 130 |
return true; |
| 131 |
} |
| 132 |
} |
| 133 |
|
| 134 |
return ActorBase::OnHandleCollision(std::move(other)); |
| 135 |
} |
| 136 |
|
| 137 |
void Bird::FlyAway() |
| 138 |
{ |
| 139 |
_owner = nullptr; |
| 140 |
_fireCooldown = 300.0f; |
| 141 |
|
| 142 |
if (_attackTime > 0.0f) { |
| 143 |
SetAnimation(AnimState::Idle); |
| 144 |
_attackTime = 0.0f; |
| 145 |
SetState(ActorState::CollideWithOtherActors, false); |
| 146 |
_renderer.setRotation(0.0f); |
| 147 |
} |
| 148 |
} |
| 149 |
|
| 150 |
void Bird::TryFire() |
| 151 |
{ |
| 152 |
_levelHandler->FindCollisionActorsByRadius(_pos.X, _pos.Y, 260.0f, [this](ActorBase* actor) { |
| 153 |
if (auto* enemy = runtime_cast<Enemies::EnemyBase>(actor)) { |
| 154 |
Vector2f newPos = enemy->GetPos(); |
| 155 |
if (IsFacingLeft() ? (newPos.X > _pos.X) : (newPos.X < _pos.X)) { |
| 156 |
return true; |
| 157 |
} |
| 158 |
|
| 159 |
switch (_type) { |
| 160 |
case 0: { // Chuck (red) |
| 161 |
SetState(ActorState::CollideWithTileset, true); |
| 162 |
TileCollisionParams params = { TileDestructType::None, true }; |
| 163 |
if (_levelHandler->IsPositionEmpty(this, AABBf(_pos.X - 2.0f, _pos.Y - 2.0f, _pos.X + 2.0f, _pos.Y + 2.0f), params)) { |
| 164 |
uint8_t shotParams[1] = { 0 }; |
| 165 |
std::shared_ptr<ActorBase> sharedOwner = _owner->shared_from_this(); |
| 166 |
|
| 167 |
std::shared_ptr<Weapons::BlasterShot> shot1 = std::make_shared<Weapons::BlasterShot>(); |
| 168 |
shot1->OnActivated(ActorActivationDetails( |
| 169 |
_levelHandler, |
| 170 |
Vector3i((std::int32_t)_pos.X, (std::int32_t)_pos.Y, _renderer.layer() - 2), |
| 171 |
shotParams |
| 172 |
)); |
| 173 |
shot1->OnFire(sharedOwner, _pos, _speed, 0.0f, IsFacingLeft()); |
| 174 |
_levelHandler->AddActor(shot1); |
| 175 |
|
| 176 |
std::shared_ptr<Weapons::BlasterShot> shot2 = std::make_shared<Weapons::BlasterShot>(); |
| 177 |
shot2->OnActivated(ActorActivationDetails( |
| 178 |
_levelHandler, |
| 179 |
Vector3i((std::int32_t)_pos.X, (std::int32_t)_pos.Y, _renderer.layer() - 2), |
| 180 |
shotParams |
| 181 |
)); |
| 182 |
shot2->OnFire(sharedOwner, _pos, _speed, IsFacingLeft() ? -0.18f : 0.18f, IsFacingLeft()); |
| 183 |
_levelHandler->AddActor(shot2); |
| 184 |
|
| 185 |
PlaySfx("Fire"_s, 0.5f); |
| 186 |
_fireCooldown = 32.0f; |
| 187 |
} |
| 188 |
SetState(ActorState::CollideWithTileset, false); |
| 189 |
break; |
| 190 |
} |
| 191 |
|
| 192 |
case 1: { // Birdy (yellow) |
| 193 |
SetAnimation(AnimState::Shoot); |
| 194 |
|
| 195 |
Vector2f attackSpeed = (newPos - _pos).Normalize(); |
| 196 |
_speed.X = attackSpeed.X * 6.0f; |
| 197 |
_speed.Y = attackSpeed.Y * 6.0f; |
| 198 |
|
| 199 |
float angle = atan2f(_speed.Y, _speed.X); |
| 200 |
if (IsFacingLeft()) { |
| 201 |
angle += fPi; |
| 202 |
} |
| 203 |
_renderer.setRotation(angle); |
| 204 |
|
| 205 |
float distance = (newPos - _pos).Length(); |
| 206 |
_attackTime = distance * 0.2f; |
| 207 |
_fireCooldown = 140.0f; |
| 208 |
|
| 209 |
SetState(ActorState::CollideWithOtherActors, true); |
| 210 |
break; |
| 211 |
} |
| 212 |
} |
| 213 |
} |
| 214 |
return true; |
| 215 |
}); |
| 216 |
|
| 217 |
} |
| 218 |
} |