From 9dca04e3850f209ad0034952bed9133dc671e4dd Mon Sep 17 00:00:00 2001 From: Zoe Roux Date: Tue, 1 Jun 2021 15:33:32 +0200 Subject: [PATCH] Making bomb explose --- sources/Component/Timer/TimerComponent.cpp | 4 ++-- sources/Component/Timer/TimerComponent.hpp | 9 +++++---- sources/Runner/Runner.cpp | 2 +- sources/System/BombHolder/BombHolderSystem.cpp | 11 +++++++++++ sources/System/BombHolder/BombHolderSystem.hpp | 6 ++++++ sources/System/Timer/TimerSystem.cpp | 7 ++++--- sources/System/Timer/TimerSystem.hpp | 6 +++++- 7 files changed, 34 insertions(+), 11 deletions(-) diff --git a/sources/Component/Timer/TimerComponent.cpp b/sources/Component/Timer/TimerComponent.cpp index 09ad1f5b..7845df9d 100644 --- a/sources/Component/Timer/TimerComponent.cpp +++ b/sources/Component/Timer/TimerComponent.cpp @@ -13,13 +13,13 @@ namespace BBM ringIn(delay) {} - TimerComponent::TimerComponent(WAL::Entity &entity, std::chrono::nanoseconds delay, const WAL::Callback &callback) + TimerComponent::TimerComponent(WAL::Entity &entity, std::chrono::nanoseconds delay, const WAL::Callback &callback) : WAL::Component(entity), ringIn(delay), callback(callback) {} - TimerComponent::TimerComponent(WAL::Entity &entity, std::chrono::nanoseconds delay, std::function callback) + TimerComponent::TimerComponent(WAL::Entity &entity, std::chrono::nanoseconds delay, std::function callback) : WAL::Component(entity), ringIn(delay), callback(std::move(callback)) diff --git a/sources/Component/Timer/TimerComponent.hpp b/sources/Component/Timer/TimerComponent.hpp index e6c005db..f239ceb4 100644 --- a/sources/Component/Timer/TimerComponent.hpp +++ b/sources/Component/Timer/TimerComponent.hpp @@ -6,6 +6,7 @@ #include #include +#include #include "Models/Callback.hpp" namespace BBM @@ -15,18 +16,18 @@ namespace BBM { public: //! @brief The callback to call when the timer ring. - WAL::Callback callback; + WAL::Callback callback; //! @brief The ring delay of this timer component. std::chrono::nanoseconds ringIn; - Component * clone(WAL::Entity &entity) const override; + Component *clone(WAL::Entity &entity) const override; //! @brief A default constructor TimerComponent(WAL::Entity &entity, std::chrono::nanoseconds delay); //! @brief Create a timer with a callback. - TimerComponent(WAL::Entity &entity, std::chrono::nanoseconds delay, const WAL::Callback &callback); + TimerComponent(WAL::Entity &entity, std::chrono::nanoseconds delay, const WAL::Callback &callback); //! @brief Create a timer with a function to call on ring. - TimerComponent(WAL::Entity &entity, std::chrono::nanoseconds delay, std::function callback); + TimerComponent(WAL::Entity &entity, std::chrono::nanoseconds delay, std::function callback); //! @brief A timer component is copy constructable TimerComponent(const TimerComponent &) = default; //! @brief A default destructor diff --git a/sources/Runner/Runner.cpp b/sources/Runner/Runner.cpp index 5f416812..9c5652b8 100644 --- a/sources/Runner/Runner.cpp +++ b/sources/Runner/Runner.cpp @@ -43,7 +43,7 @@ namespace BBM void addSystems(WAL::Wal &wal) { - wal.addSystem() + wal.addSystem(wal) .addSystem() .addSystem() .addSystem() diff --git a/sources/System/BombHolder/BombHolderSystem.cpp b/sources/System/BombHolder/BombHolderSystem.cpp index dc56b418..c7243acd 100644 --- a/sources/System/BombHolder/BombHolderSystem.cpp +++ b/sources/System/BombHolder/BombHolderSystem.cpp @@ -2,6 +2,7 @@ // Created by Zoe Roux on 5/31/21. // +#include #include "Component/Renderer/Drawable3DComponent.hpp" #include "Component/Controllable/ControllableComponent.hpp" #include "BombHolderSystem.hpp" @@ -13,6 +14,8 @@ namespace RAY3D = RAY::Drawables::Drawables3D; namespace BBM { + std::chrono::nanoseconds BombHolderSystem::explosionTimer = 3s; + BombHolderSystem::BombHolderSystem(WAL::Wal &wal) : WAL::System({ typeid(PositionComponent), @@ -22,10 +25,18 @@ namespace BBM _wal(wal) {} + void BombHolderSystem::_bombExplosion(WAL::Entity &bomb, const WAL::Wal &wal) + { + std::cout << "Boom" << std::endl; + bomb.scheduleDeletion(); + } + void BombHolderSystem::_spawnBomb(Vector3f position) { + std::cout << "Spawnned" << std::endl; this->_wal.scene->addEntity("Bomb") .addComponent(position) + .addComponent(BombHolderSystem::explosionTimer, &BombHolderSystem::_bombExplosion) .addComponent>("assets/bombs/bomb.obj", std::make_pair(MAP_DIFFUSE, "assets/bombs/bomb_normal.png")); } diff --git a/sources/System/BombHolder/BombHolderSystem.hpp b/sources/System/BombHolder/BombHolderSystem.hpp index 1241c9d9..1dc13c72 100644 --- a/sources/System/BombHolder/BombHolderSystem.hpp +++ b/sources/System/BombHolder/BombHolderSystem.hpp @@ -18,7 +18,13 @@ namespace BBM WAL::Wal &_wal; //! @brief Spawn a bomb at the specified position. void _spawnBomb(Vector3f position); + + //! @brief The method triggered when the bomb explode. + static void _bombExplosion(WAL::Entity &bomb, const WAL::Wal &); public: + //! @brief The explosion time of new bombs. + static std::chrono::nanoseconds explosionTimer; + //! @inherit void onUpdate(WAL::Entity &entity, std::chrono::nanoseconds dtime) override; diff --git a/sources/System/Timer/TimerSystem.cpp b/sources/System/Timer/TimerSystem.cpp index e299549a..e1765a23 100644 --- a/sources/System/Timer/TimerSystem.cpp +++ b/sources/System/Timer/TimerSystem.cpp @@ -9,10 +9,11 @@ using namespace std::chrono_literals; namespace BBM { - TimerSystem::TimerSystem() + TimerSystem::TimerSystem(WAL::Wal &wal) : WAL::System({ typeid(TimerComponent) - }) + }), + _wal(wal) {} void TimerSystem::onUpdate(WAL::Entity &entity, std::chrono::nanoseconds dtime) @@ -21,7 +22,7 @@ namespace BBM timer.ringIn -= dtime; if (timer.ringIn <= 0ns) { timer.setDisable(true); - timer.callback(entity); + timer.callback(entity, this->_wal); } } } \ No newline at end of file diff --git a/sources/System/Timer/TimerSystem.hpp b/sources/System/Timer/TimerSystem.hpp index a3cf8a2b..f925dd82 100644 --- a/sources/System/Timer/TimerSystem.hpp +++ b/sources/System/Timer/TimerSystem.hpp @@ -5,17 +5,21 @@ #pragma once #include +#include namespace BBM { class TimerSystem : public WAL::System { + private: + //! @brief The wal engine + WAL::Wal &_wal; public: //! @inherit void onUpdate(WAL::Entity &entity, std::chrono::nanoseconds dtime) override; //! @brief A default constructor - TimerSystem(); + TimerSystem(WAL::Wal &); //! @brief A timer system is copy constructable. TimerSystem(const TimerSystem &) = default; //! @brief A default destructor