| 1 |
#include "CrateContainer.h" |
| 2 |
#include "../../ILevelHandler.h" |
| 3 |
#include "../../Tiles/TileMap.h" |
| 4 |
#include "../Player.h" |
| 5 |
#include "../Weapons/ShotBase.h" |
| 6 |
#include "../Weapons/TNT.h" |
| 7 |
|
| 8 |
namespace Jazz2::Actors::Solid |
| 9 |
{ |
| 10 |
CrateContainer::CrateContainer() |
| 11 |
{ |
| 12 |
} |
| 13 |
|
| 14 |
void CrateContainer::Preload(const ActorActivationDetails& details) |
| 15 |
{ |
| 16 |
PreloadMetadataAsync("Object/Crate/Generic"_s); |
| 17 |
} |
| 18 |
|
| 19 |
Task<bool> CrateContainer::OnActivatedAsync(const ActorActivationDetails& details) |
| 20 |
{ |
| 21 |
Movable = true; |
| 22 |
|
| 23 |
EventType eventType = (EventType)*(uint16_t*)&details.Params[0]; |
| 24 |
std::int32_t count = details.Params[2]; |
| 25 |
if (eventType != EventType::Empty && count > 0) { |
| 26 |
AddContent(eventType, count, &details.Params[3], 16 - 3); |
| 27 |
} |
| 28 |
|
| 29 |
async_await RequestMetadataAsync("Object/Crate/Generic"_s); |
| 30 |
|
| 31 |
SetAnimation(AnimState::Idle); |
| 32 |
|
| 33 |
async_return true; |
| 34 |
} |
| 35 |
|
| 36 |
bool CrateContainer::OnHandleCollision(std::shared_ptr<ActorBase> other) |
| 37 |
{ |
| 38 |
if (_health == 0) { |
| 39 |
return GenericContainer::OnHandleCollision(other); |
| 40 |
} |
| 41 |
|
| 42 |
if (auto* shotBase = runtime_cast<Weapons::ShotBase>(other.get())) { |
| 43 |
if (shotBase->GetStrength() > 0) { |
| 44 |
DecreaseHealth(shotBase->GetStrength(), shotBase); |
| 45 |
shotBase->DecreaseHealth(1); |
| 46 |
return true; |
| 47 |
} |
| 48 |
} else if (auto* tnt = runtime_cast<Weapons::TNT>(other.get())) { |
| 49 |
DecreaseHealth(INT32_MAX, tnt); |
| 50 |
return true; |
| 51 |
} else if (auto* player = runtime_cast<Player>(other.get())) { |
| 52 |
if (player->CanBreakSolidObjects()) { |
| 53 |
DecreaseHealth(INT32_MAX, player); |
| 54 |
return true; |
| 55 |
} |
| 56 |
} |
| 57 |
|
| 58 |
return GenericContainer::OnHandleCollision(std::move(other)); |
| 59 |
} |
| 60 |
|
| 61 |
bool CrateContainer::OnPerish(ActorBase* collider) |
| 62 |
{ |
| 63 |
SetState(ActorState::CollideWithTileset | ActorState::CollideWithOtherActors | ActorState::ApplyGravitation, false); |
| 64 |
|
| 65 |
CreateParticleDebrisOnPerish(ParticleDebrisEffect::Standard, Vector2f::Zero); |
| 66 |
|
| 67 |
PlaySfx("Break"_s); |
| 68 |
|
| 69 |
CreateSpriteDebris((AnimState)1, 3); |
| 70 |
CreateSpriteDebris((AnimState)2, 2); |
| 71 |
|
| 72 |
_frozenTimeLeft = std::min(1.0f, _frozenTimeLeft); |
| 73 |
SetTransition(AnimState::TransitionDeath, false, [this, collider]() { |
| 74 |
GenericContainer::OnPerish(collider); |
| 75 |
}); |
| 76 |
SpawnContent(); |
| 77 |
return true; |
| 78 |
} |
| 79 |
} |