diff --git a/CMakeLists.txt b/CMakeLists.txt index 14240035..86be286d 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -62,7 +62,7 @@ set(SOURCES sources/Component/Timer/TimerComponent.hpp sources/System/Timer/TimerSystem.cpp sources/System/Timer/TimerSystem.hpp -) + sources/System/Event/EventSystem.cpp sources/System/Event/EventSystem.hpp) add_executable(bomberman sources/main.cpp diff --git a/sources/Component/Timer/TimerComponent.hpp b/sources/Component/Timer/TimerComponent.hpp index f239ceb4..513ccb71 100644 --- a/sources/Component/Timer/TimerComponent.hpp +++ b/sources/Component/Timer/TimerComponent.hpp @@ -16,7 +16,7 @@ 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; diff --git a/sources/Runner/Runner.cpp b/sources/Runner/Runner.cpp index 9c5652b8..eb531502 100644 --- a/sources/Runner/Runner.cpp +++ b/sources/Runner/Runner.cpp @@ -11,6 +11,9 @@ #include #include #include +#include +#include "System/Event/EventSystem.hpp" +#include "System/Health/HealthSystem.hpp" #include "System/Timer/TimerSystem.hpp" #include "Component/BombHolder/BombHolderComponent.hpp" #include "System/BombHolder/BombHolderSystem.hpp" @@ -48,6 +51,8 @@ namespace BBM .addSystem() .addSystem() .addSystem(wal) + .addSystem() + .addSystem() .addSystem(); } @@ -78,7 +83,8 @@ namespace BBM .addComponent() .addComponent() .addComponent() - .addComponent(); + .addComponent() + .addComponent(1); scene->addEntity("camera") .addComponent(0, 20, -5) .addComponent(); diff --git a/sources/System/BombHolder/BombHolderSystem.cpp b/sources/System/BombHolder/BombHolderSystem.cpp index c7243acd..2c289ce4 100644 --- a/sources/System/BombHolder/BombHolderSystem.cpp +++ b/sources/System/BombHolder/BombHolderSystem.cpp @@ -2,12 +2,14 @@ // Created by Zoe Roux on 5/31/21. // -#include +#include "Component/Timer/TimerComponent.hpp" +#include "System/Event/EventSystem.hpp" #include "Component/Renderer/Drawable3DComponent.hpp" #include "Component/Controllable/ControllableComponent.hpp" #include "BombHolderSystem.hpp" #include "Component/Position/PositionComponent.hpp" #include "Component/BombHolder/BombHolderComponent.hpp" +#include "Component/Health/HealthComponent.hpp" using namespace std::chrono_literals; namespace RAY3D = RAY::Drawables::Drawables3D; @@ -15,6 +17,7 @@ namespace RAY3D = RAY::Drawables::Drawables3D; namespace BBM { std::chrono::nanoseconds BombHolderSystem::explosionTimer = 3s; + float BombHolderSystem::explosionRadius = 3; BombHolderSystem::BombHolderSystem(WAL::Wal &wal) : WAL::System({ @@ -25,10 +28,20 @@ namespace BBM _wal(wal) {} - void BombHolderSystem::_bombExplosion(WAL::Entity &bomb, const WAL::Wal &wal) + void BombHolderSystem::_bombExplosion(WAL::Entity &bomb, WAL::Wal &wal) { std::cout << "Boom" << std::endl; bomb.scheduleDeletion(); + auto &bombPosition = bomb.getComponent(); + wal.getSystem().dispatchEvent([&bombPosition](WAL::Entity &entity){ + if (!entity.hasComponent() || + !entity.hasComponent()) + return; + auto &position = entity.getComponent(); + + if (position.position.distance(bombPosition.position) <= BombHolderSystem::explosionRadius) + entity.getComponent().takeDmg(1); + }); } void BombHolderSystem::_spawnBomb(Vector3f position) diff --git a/sources/System/BombHolder/BombHolderSystem.hpp b/sources/System/BombHolder/BombHolderSystem.hpp index 1dc13c72..83f80711 100644 --- a/sources/System/BombHolder/BombHolderSystem.hpp +++ b/sources/System/BombHolder/BombHolderSystem.hpp @@ -20,10 +20,12 @@ namespace BBM void _spawnBomb(Vector3f position); //! @brief The method triggered when the bomb explode. - static void _bombExplosion(WAL::Entity &bomb, const WAL::Wal &); + static void _bombExplosion(WAL::Entity &bomb, WAL::Wal &); public: //! @brief The explosion time of new bombs. static std::chrono::nanoseconds explosionTimer; + //! @brief The radius of the explosion. + static float explosionRadius; //! @inherit void onUpdate(WAL::Entity &entity, std::chrono::nanoseconds dtime) override; diff --git a/sources/System/Event/EventSystem.cpp b/sources/System/Event/EventSystem.cpp new file mode 100644 index 00000000..e37e9f53 --- /dev/null +++ b/sources/System/Event/EventSystem.cpp @@ -0,0 +1,28 @@ +// +// Created by Zoe Roux on 6/1/21. +// + +#include "EventSystem.hpp" + +namespace BBM +{ + EventSystem::EventSystem() + : WAL::System({}) + {} + + void EventSystem::dispatchEvent(const std::function &event) + { + this->_events.emplace_back(event); + } + + void EventSystem::onUpdate(WAL::Entity &entity, std::chrono::nanoseconds) + { + for (auto &event : this->_events) + event(entity); + } + + void EventSystem::onSelfUpdate() + { + this->_events.clear(); + } +} \ No newline at end of file diff --git a/sources/System/Event/EventSystem.hpp b/sources/System/Event/EventSystem.hpp new file mode 100644 index 00000000..0defda29 --- /dev/null +++ b/sources/System/Event/EventSystem.hpp @@ -0,0 +1,36 @@ +// +// Created by Zoe Roux on 6/1/21. +// + +#pragma once + +#include +#include +#include + +namespace BBM +{ + class EventSystem : public WAL::System + { + private: + //! @brief The list of events that occurred in the last update. + std::vector> _events; + public: + //! @brief Inform the system that a new event has occurred and it should run the given method on every entities. + void dispatchEvent(const std::function& event); + + //! @inherit + void onUpdate(WAL::Entity &entity, std::chrono::nanoseconds dtime) override; + //! @inherit + void onSelfUpdate() override; + + //! @brief A default constructor + EventSystem(); + //! @brief An event system is copy constructable. + EventSystem(const EventSystem &) = default; + //! @brief A default destructor + ~EventSystem() override = default; + //! @brief An event system is not assignable. + EventSystem &operator=(const EventSystem &) = delete; + }; +}