Merge pull request #189 from AnonymusRaccoon/fixs

Bombs fix
This commit is contained in:
Clément Le Bihan
2021-06-17 14:33:06 +02:00
committed by GitHub
5 changed files with 41 additions and 29 deletions
@@ -4,17 +4,19 @@
#include "BasicBombComponent.hpp" #include "BasicBombComponent.hpp"
#include <utility>
namespace BBM namespace BBM
{ {
BasicBombComponent::BasicBombComponent(WAL::Entity &entity, int damage, int explosionRadius, unsigned ownerID) BasicBombComponent::BasicBombComponent(WAL::Entity &entity, int damage, int explosionRadius, std::vector<unsigned> ignored)
: WAL::Component(entity), : WAL::Component(entity),
damage(damage), damage(damage),
explosionRadius(explosionRadius), explosionRadius(explosionRadius),
ownerID(ownerID) ignoredEntities(std::move(ignored))
{} {}
WAL::Component *BasicBombComponent::clone(WAL::Entity &entity) const WAL::Component *BasicBombComponent::clone(WAL::Entity &entity) const
{ {
return new BasicBombComponent(entity, this->damage, this->explosionRadius, this->ownerID); return new BasicBombComponent(entity, this->damage, this->explosionRadius, this->ignoredEntities);
} }
} }
@@ -19,16 +19,14 @@ namespace BBM
const int explosionRadius = 3; const int explosionRadius = 3;
//! @brief The damage made by the explosion on an entity //! @brief The damage made by the explosion on an entity
const int damage = 1; const int damage = 1;
//! @brief The ID of the owner. //! @brief The list of IDs of ignored entities.
unsigned ownerID; std::vector<unsigned> ignoredEntities;
//! @brief Should collisions with the owner be disabled.²
bool ignoreOwner = true;
//! @inherit //! @inherit
WAL::Component *clone(WAL::Entity &entity) const override; WAL::Component *clone(WAL::Entity &entity) const override;
//! @brief A component can't be instantiated, it should be derived. //! @brief A component can't be instantiated, it should be derived.
explicit BasicBombComponent(WAL::Entity &entity, int damage, int explosionRadius, unsigned ownerID); explicit BasicBombComponent(WAL::Entity &entity, int damage, int explosionRadius, std::vector<unsigned> ignored);
//! @brief A component can't be instantiated, it should be derived. //! @brief A component can't be instantiated, it should be derived.
BasicBombComponent(const BasicBombComponent &) = default; BasicBombComponent(const BasicBombComponent &) = default;
+6 -5
View File
@@ -14,15 +14,16 @@ namespace BBM
void BombSystem::onUpdate(WAL::ViewEntity<BasicBombComponent, PositionComponent> &entity, std::chrono::nanoseconds dtime) void BombSystem::onUpdate(WAL::ViewEntity<BasicBombComponent, PositionComponent> &entity, std::chrono::nanoseconds dtime)
{ {
auto &bomb = entity.get<BasicBombComponent>(); auto &bomb = entity.get<BasicBombComponent>();
if (!bomb.ignoreOwner)
if (bomb.ignoredEntities.empty())
return; return;
auto &pos = entity.get<PositionComponent>(); auto &pos = entity.get<PositionComponent>();
for (auto &[owner, ownerPos, _] : this->_wal.getScene()->view<PositionComponent, BombHolderComponent>()) { for (auto &[owner, ownerPos, _] : this->_wal.getScene()->view<PositionComponent, BombHolderComponent>()) {
if (owner.getUid() != bomb.ownerID)
continue;
if (pos.position.distance(ownerPos.position) >= 1.1) { if (pos.position.distance(ownerPos.position) >= 1.1) {
bomb.ignoreOwner = false; bomb.ignoredEntities.erase(
return; std::remove(bomb.ignoredEntities.begin(), bomb.ignoredEntities.end(), owner.getUid()),
bomb.ignoredEntities.end());
} }
} }
} }
+23 -12
View File
@@ -2,7 +2,6 @@
// Created by Zoe Roux on 5/31/21. // Created by Zoe Roux on 5/31/21.
// //
#include <Component/Animation/AnimationsComponent.hpp>
#include <Component/Bomb/BasicBombComponent.hpp> #include <Component/Bomb/BasicBombComponent.hpp>
#include "Component/Timer/TimerComponent.hpp" #include "Component/Timer/TimerComponent.hpp"
#include "System/Event/EventSystem.hpp" #include "System/Event/EventSystem.hpp"
@@ -31,7 +30,8 @@ namespace BBM
CollisionComponent::CollidedAxis collidedAxis) CollisionComponent::CollidedAxis collidedAxis)
{ {
auto &bombInfo = bomb.getComponent<BasicBombComponent>(); auto &bombInfo = bomb.getComponent<BasicBombComponent>();
if (bombInfo.ignoreOwner && bombInfo.ownerID == entity.getUid()) auto found = std::find(bombInfo.ignoredEntities.begin(), bombInfo.ignoredEntities.end(), entity.getUid());
if (found != bombInfo.ignoredEntities.end())
return; return;
return MapGenerator::wallCollided(entity, bomb, collidedAxis); return MapGenerator::wallCollided(entity, bomb, collidedAxis);
} }
@@ -115,10 +115,17 @@ namespace BBM
_dispatchExplosion(position, wal, explosionRadius); _dispatchExplosion(position, wal, explosionRadius);
} }
void BombHolderSystem::_spawnBomb(Vector3f position, BombHolderComponent &holder, unsigned id) void BombHolderSystem::_spawnBomb(Vector3f position, BombHolderComponent &holder)
{ {
std::vector<unsigned> overlapping;
for (auto &[entity, pos, _] : this->_wal.getScene()->view<PositionComponent, BombHolderComponent>()) {
if (position.distance(pos.position) <= 1.1)
overlapping.emplace_back(entity.getUid());
}
this->_wal.getScene()->scheduleNewEntity("Bomb") this->_wal.getScene()->scheduleNewEntity("Bomb")
.addComponent<PositionComponent>(position.round()) .addComponent<PositionComponent>(position)
.addComponent<HealthComponent>(1, [](WAL::Entity &entity, WAL::Wal &wal) { .addComponent<HealthComponent>(1, [](WAL::Entity &entity, WAL::Wal &wal) {
// the bomb explode when hit // the bomb explode when hit
entity.scheduleDeletion(); entity.scheduleDeletion();
@@ -154,7 +161,7 @@ namespace BBM
}) })
.addComponent<WhiteShaderComponent>() .addComponent<WhiteShaderComponent>()
.addComponent<TagComponent<BlowablePass>>() .addComponent<TagComponent<BlowablePass>>()
.addComponent<BasicBombComponent>(holder.damage, holder.explosionRadius, id) .addComponent<BasicBombComponent>(holder.damage, holder.explosionRadius, overlapping)
.addComponent<TimerComponent>(BombHolderSystem::explosionTimer, &BombHolderSystem::_bombExplosion) .addComponent<TimerComponent>(BombHolderSystem::explosionTimer, &BombHolderSystem::_bombExplosion)
.addComponent<CollisionComponent>( .addComponent<CollisionComponent>(
WAL::Callback<WAL::Entity &, const WAL::Entity &, CollisionComponent::CollidedAxis>(), WAL::Callback<WAL::Entity &, const WAL::Entity &, CollisionComponent::CollidedAxis>(),
@@ -166,18 +173,13 @@ namespace BBM
)); ));
} }
void void BombHolderSystem::onUpdate(WAL::ViewEntity<PositionComponent, BombHolderComponent, ControllableComponent> &entity,
BombHolderSystem::onUpdate(WAL::ViewEntity<PositionComponent, BombHolderComponent, ControllableComponent> &entity, std::chrono::nanoseconds dtime)
std::chrono::nanoseconds dtime)
{ {
auto &holder = entity.get<BombHolderComponent>(); auto &holder = entity.get<BombHolderComponent>();
auto &position = entity.get<PositionComponent>(); auto &position = entity.get<PositionComponent>();
auto &controllable = entity.get<ControllableComponent>(); auto &controllable = entity.get<ControllableComponent>();
if (controllable.bomb && holder.bombCount > 0) {
holder.bombCount--;
this->_spawnBomb(position.position, holder, entity->getUid());
}
if (holder.bombCount < holder.maxBombCount) { if (holder.bombCount < holder.maxBombCount) {
holder.nextBombRefill -= dtime; holder.nextBombRefill -= dtime;
if (holder.nextBombRefill <= 0ns) { if (holder.nextBombRefill <= 0ns) {
@@ -185,5 +187,14 @@ namespace BBM
holder.bombCount++; holder.bombCount++;
} }
} }
if (controllable.bomb && holder.bombCount > 0) {
auto spawnPos = position.position.round();
for (auto &[entity, pos, _] : this->_wal.getScene()->view<PositionComponent, BasicBombComponent>()) {
if (pos.position == spawnPos)
return;
}
holder.bombCount--;
this->_spawnBomb(spawnPos, holder);
}
} }
} }
@@ -35,7 +35,7 @@ namespace BBM
{ {
private: private:
//! @brief Spawn a bomb at the specified position. //! @brief Spawn a bomb at the specified position.
void _spawnBomb(Vector3f position, BombHolderComponent &holder, unsigned id); void _spawnBomb(Vector3f position, BombHolderComponent &holder);
//! @brief Spawn a bomb at the specified position. //! @brief Spawn a bomb at the specified position.
static void _dispatchExplosion(const Vector3f &position, static void _dispatchExplosion(const Vector3f &position,