| 1 |
#include "GenericContainer.h" |
| 2 |
#include "../Weapons/TNT.h" |
| 3 |
#include "../../ILevelHandler.h" |
| 4 |
#include "../../Events/EventSpawner.h" |
| 5 |
#include "../../Tiles/TileMap.h" |
| 6 |
#include "../../../nCine/Base/Random.h" |
| 7 |
|
| 8 |
using namespace Jazz2::Tiles; |
| 9 |
|
| 10 |
namespace Jazz2::Actors::Solid |
| 11 |
{ |
| 12 |
GenericContainer::GenericContainer() |
| 13 |
{ |
| 14 |
} |
| 15 |
|
| 16 |
bool GenericContainer::CanCauseDamage(ActorBase* collider) |
| 17 |
{ |
| 18 |
return _levelHandler->IsReforged() || runtime_cast<Weapons::TNT>(collider); |
| 19 |
} |
| 20 |
|
| 21 |
bool GenericContainer::OnPerish(ActorBase* collider) |
| 22 |
{ |
| 23 |
SpawnContent(); |
| 24 |
|
| 25 |
return SolidObjectBase::OnPerish(collider); |
| 26 |
} |
| 27 |
|
| 28 |
void GenericContainer::AddContent(EventType eventType, std::int32_t count, const std::uint8_t* eventParams, std::int32_t eventParamsSize) |
| 29 |
{ |
| 30 |
if (count > 0) { |
| 31 |
_content.emplace_back(eventType, count, eventParams, eventParamsSize); |
| 32 |
} |
| 33 |
} |
| 34 |
|
| 35 |
void GenericContainer::SpawnContent() |
| 36 |
{ |
| 37 |
if (_content.empty()) { |
| 38 |
return; |
| 39 |
} |
| 40 |
|
| 41 |
AABBf aabbExtended = AABB; |
| 42 |
aabbExtended.L -= 20.0f; |
| 43 |
aabbExtended.R += 20.0f; |
| 44 |
aabbExtended.T -= 20.0f; |
| 45 |
TileCollisionParams params = { TileDestructType::None, true }; |
| 46 |
auto prevState = GetState(); |
| 47 |
SetState(ActorState::CollideWithTileset | ActorState::CollideWithSolidObjects | ActorState::SkipPerPixelCollisions, true); |
| 48 |
SetState(ActorState::IsSolidObject | ActorState::CollideWithSolidObjectsBelow, false); |
| 49 |
SetState(prevState); |
| 50 |
|
| 51 |
std::int32_t maxCount = 0; |
| 52 |
for (auto& item : _content) { |
| 53 |
if (maxCount < item.Count) { |
| 54 |
maxCount = item.Count; |
| 55 |
} |
| 56 |
} |
| 57 |
|
| 58 |
bool canThrowFar = (maxCount > 1 && _levelHandler->IsPositionEmpty(this, aabbExtended, params)); |
| 59 |
|
| 60 |
for (auto& item : _content) { |
| 61 |
for (std::int32_t j = 0; j < item.Count; j++) { |
| 62 |
float x, y, fx, fy; |
| 63 |
if (item.Count > 1) { |
| 64 |
if (canThrowFar) { |
| 65 |
fx = Random().NextFloat(-6.0f, 6.0f); |
| 66 |
fy = Random().NextFloat(-0.8f, 0.0f); |
| 67 |
x = _pos.X + fx * (2.0f + _content.size() * 0.1f); |
| 68 |
y = _pos.Y + fy * (12.0f + _content.size() * 0.2f); |
| 69 |
} else { |
| 70 |
fx = Random().NextFloat(-4.0f, 4.0f); |
| 71 |
fy = Random().NextFloat(-0.4f, 0.0f); |
| 72 |
x = _pos.X + fx * (1.0f + _content.size() * 0.1f); |
| 73 |
y = _pos.Y + fy * (4.0f + _content.size() * 0.2f); |
| 74 |
} |
| 75 |
} else { |
| 76 |
fx = 0.0f; |
| 77 |
fy = 0.0f; |
| 78 |
x = _pos.X; |
| 79 |
y = _pos.Y; |
| 80 |
} |
| 81 |
|
| 82 |
std::shared_ptr<ActorBase> actor = _levelHandler->EventSpawner()->SpawnEvent(item.Type, item.EventParams, |
| 83 |
ActorState::None, Vector3i((std::int32_t)x, (std::int32_t)y, _renderer.layer() - 10)); |
| 84 |
if (actor != nullptr) { |
| 85 |
actor->AddExternalForce(fx, fy); |
| 86 |
_levelHandler->AddActor(actor); |
| 87 |
} |
| 88 |
} |
| 89 |
} |
| 90 |
|
| 91 |
_content.clear(); |
| 92 |
} |
| 93 |
|
| 94 |
GenericContainer::ContainerContent::ContainerContent(EventType eventType, std::int32_t count, const std::uint8_t* eventParams, std::int32_t eventParamsSize) |
| 95 |
{ |
| 96 |
Type = eventType; |
| 97 |
Count = count; |
| 98 |
|
| 99 |
std::memcpy(EventParams, eventParams, eventParamsSize); |
| 100 |
if (eventParamsSize < 16) { |
| 101 |
std::memset(EventParams + eventParamsSize, 0, 16 - eventParamsSize); |
| 102 |
} |
| 103 |
} |
| 104 |
} |