From 4f782e9d75d047373f60ee95a899408ba39ced3e Mon Sep 17 00:00:00 2001 From: Zoe Roux Date: Thu, 17 Jun 2021 12:37:40 +0200 Subject: [PATCH 1/2] Spawning a bomb inside a player dont block him anymore --- sources/Component/Bomb/BasicBombComponent.cpp | 14 +++++++----- sources/Component/Bomb/BasicBombComponent.hpp | 8 +++---- sources/System/Bomb/BombSystem.cpp | 11 +++++----- .../System/BombHolder/BombHolderSystem.cpp | 22 ++++++++++++------- .../System/BombHolder/BombHolderSystem.hpp | 2 +- 5 files changed, 32 insertions(+), 25 deletions(-) diff --git a/sources/Component/Bomb/BasicBombComponent.cpp b/sources/Component/Bomb/BasicBombComponent.cpp index 97cbb70d..9de464d7 100644 --- a/sources/Component/Bomb/BasicBombComponent.cpp +++ b/sources/Component/Bomb/BasicBombComponent.cpp @@ -4,17 +4,19 @@ #include "BasicBombComponent.hpp" +#include + namespace BBM { - BasicBombComponent::BasicBombComponent(WAL::Entity &entity, int damage, int explosionRadius, unsigned ownerID) - : WAL::Component(entity), - damage(damage), - explosionRadius(explosionRadius), - ownerID(ownerID) + BasicBombComponent::BasicBombComponent(WAL::Entity &entity, int damage, int explosionRadius, std::vector ignored) + : WAL::Component(entity), + damage(damage), + explosionRadius(explosionRadius), + ignoredEntities(std::move(ignored)) {} 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); } } \ No newline at end of file diff --git a/sources/Component/Bomb/BasicBombComponent.hpp b/sources/Component/Bomb/BasicBombComponent.hpp index 1b9b46bf..5d3838c8 100644 --- a/sources/Component/Bomb/BasicBombComponent.hpp +++ b/sources/Component/Bomb/BasicBombComponent.hpp @@ -19,16 +19,14 @@ namespace BBM const int explosionRadius = 3; //! @brief The damage made by the explosion on an entity const int damage = 1; - //! @brief The ID of the owner. - unsigned ownerID; - //! @brief Should collisions with the owner be disabled.² - bool ignoreOwner = true; + //! @brief The list of IDs of ignored entities. + std::vector ignoredEntities; //! @inherit WAL::Component *clone(WAL::Entity &entity) const override; //! @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 ignored); //! @brief A component can't be instantiated, it should be derived. BasicBombComponent(const BasicBombComponent &) = default; diff --git a/sources/System/Bomb/BombSystem.cpp b/sources/System/Bomb/BombSystem.cpp index 60ff5a30..39b7698b 100644 --- a/sources/System/Bomb/BombSystem.cpp +++ b/sources/System/Bomb/BombSystem.cpp @@ -14,15 +14,16 @@ namespace BBM void BombSystem::onUpdate(WAL::ViewEntity &entity, std::chrono::nanoseconds dtime) { auto &bomb = entity.get(); - if (!bomb.ignoreOwner) + + if (bomb.ignoredEntities.empty()) return; + auto &pos = entity.get(); for (auto &[owner, ownerPos, _] : this->_wal.getScene()->view()) { - if (owner.getUid() != bomb.ownerID) - continue; if (pos.position.distance(ownerPos.position) >= 1.1) { - bomb.ignoreOwner = false; - return; + bomb.ignoredEntities.erase( + std::remove(bomb.ignoredEntities.begin(), bomb.ignoredEntities.end(), owner.getUid()), + bomb.ignoredEntities.end()); } } } diff --git a/sources/System/BombHolder/BombHolderSystem.cpp b/sources/System/BombHolder/BombHolderSystem.cpp index 0a69c435..8bbf673e 100644 --- a/sources/System/BombHolder/BombHolderSystem.cpp +++ b/sources/System/BombHolder/BombHolderSystem.cpp @@ -2,7 +2,6 @@ // Created by Zoe Roux on 5/31/21. // -#include #include #include "Component/Timer/TimerComponent.hpp" #include "System/Event/EventSystem.hpp" @@ -31,7 +30,8 @@ namespace BBM CollisionComponent::CollidedAxis collidedAxis) { auto &bombInfo = bomb.getComponent(); - 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 MapGenerator::wallCollided(entity, bomb, collidedAxis); } @@ -115,8 +115,15 @@ namespace BBM _dispatchExplosion(position, wal, explosionRadius); } - void BombHolderSystem::_spawnBomb(Vector3f position, BombHolderComponent &holder, unsigned id) + void BombHolderSystem::_spawnBomb(Vector3f position, BombHolderComponent &holder) { + std::vector overlapping; + + for (auto &[entity, pos, _] : this->_wal.getScene()->view()) { + if (position.distance(pos.position) <= 1.1) + overlapping.emplace_back(entity.getUid()); + } + this->_wal.getScene()->scheduleNewEntity("Bomb") .addComponent(position.round()) .addComponent(1, [](WAL::Entity &entity, WAL::Wal &wal) { @@ -154,7 +161,7 @@ namespace BBM }) .addComponent() .addComponent>() - .addComponent(holder.damage, holder.explosionRadius, id) + .addComponent(holder.damage, holder.explosionRadius, overlapping) .addComponent(BombHolderSystem::explosionTimer, &BombHolderSystem::_bombExplosion) .addComponent( WAL::Callback(), @@ -166,9 +173,8 @@ namespace BBM )); } - void - BombHolderSystem::onUpdate(WAL::ViewEntity &entity, - std::chrono::nanoseconds dtime) + void BombHolderSystem::onUpdate(WAL::ViewEntity &entity, + std::chrono::nanoseconds dtime) { auto &holder = entity.get(); auto &position = entity.get(); @@ -176,7 +182,7 @@ namespace BBM if (controllable.bomb && holder.bombCount > 0) { holder.bombCount--; - this->_spawnBomb(position.position, holder, entity->getUid()); + this->_spawnBomb(position.position, holder); } if (holder.bombCount < holder.maxBombCount) { holder.nextBombRefill -= dtime; diff --git a/sources/System/BombHolder/BombHolderSystem.hpp b/sources/System/BombHolder/BombHolderSystem.hpp index c5631190..9ba67f2b 100644 --- a/sources/System/BombHolder/BombHolderSystem.hpp +++ b/sources/System/BombHolder/BombHolderSystem.hpp @@ -35,7 +35,7 @@ namespace BBM { private: //! @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. static void _dispatchExplosion(const Vector3f &position, From fcb1c910449dfdcc9d3585e0743934316fba1bbc Mon Sep 17 00:00:00 2001 From: Zoe Roux Date: Thu, 17 Jun 2021 14:00:30 +0200 Subject: [PATCH 2/2] Fixing multiple bombs at the same pos --- sources/System/BombHolder/BombHolderSystem.cpp | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/sources/System/BombHolder/BombHolderSystem.cpp b/sources/System/BombHolder/BombHolderSystem.cpp index 8bbf673e..02ed40aa 100644 --- a/sources/System/BombHolder/BombHolderSystem.cpp +++ b/sources/System/BombHolder/BombHolderSystem.cpp @@ -125,7 +125,7 @@ namespace BBM } this->_wal.getScene()->scheduleNewEntity("Bomb") - .addComponent(position.round()) + .addComponent(position) .addComponent(1, [](WAL::Entity &entity, WAL::Wal &wal) { // the bomb explode when hit entity.scheduleDeletion(); @@ -180,10 +180,6 @@ namespace BBM auto &position = entity.get(); auto &controllable = entity.get(); - if (controllable.bomb && holder.bombCount > 0) { - holder.bombCount--; - this->_spawnBomb(position.position, holder); - } if (holder.bombCount < holder.maxBombCount) { holder.nextBombRefill -= dtime; if (holder.nextBombRefill <= 0ns) { @@ -191,5 +187,14 @@ namespace BBM holder.bombCount++; } } + if (controllable.bomb && holder.bombCount > 0) { + auto spawnPos = position.position.round(); + for (auto &[entity, pos, _] : this->_wal.getScene()->view()) { + if (pos.position == spawnPos) + return; + } + holder.bombCount--; + this->_spawnBomb(spawnPos, holder); + } } } \ No newline at end of file