diff --git a/CMakeLists.txt b/CMakeLists.txt index 588f5096..0583a352 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -81,6 +81,7 @@ set(SOURCES sources/Component/Animator/AnimatorComponent.hpp sources/System/Animator/AnimatorSystem.cpp sources/System/Animator/AnimatorSystem.hpp + sources/Component/Tag/TagComponent.hpp ) add_executable(bomberman sources/main.cpp diff --git a/sources/Component/Tag/TagComponent.hpp b/sources/Component/Tag/TagComponent.hpp new file mode 100644 index 00000000..00fa27d5 --- /dev/null +++ b/sources/Component/Tag/TagComponent.hpp @@ -0,0 +1,54 @@ +// +// Created by Zoe Roux on 6/9/21. +// + +#pragma once + +#include +#include + +namespace BBM +{ + template + struct StringLiteral + { + public: + char value[I]; + + //! @brief Implicitly convert an array of char to a string literal. + constexpr StringLiteral(const char (&str)[I]) // NOLINT(google-explicit-constructor) + : value() + { + std::copy_n(str, I, value); + } + //! @brief A string literal is copy constructable. + constexpr StringLiteral(const StringLiteral &) = default; + //! @brief A default destructor + constexpr ~StringLiteral() = default; + //! @brief A string literal is assignable. + constexpr StringLiteral &operator=(const StringLiteral &) = default; + }; + + template + class TagComponent : public WAL::Component + { + public: + Component *clone(WAL::Entity &entity) const override + { + return new TagComponent(entity); + } + + //! @brief Create a new empty tag component. + explicit TagComponent(WAL::Entity &entity) + : WAL::Component(entity) + {} + //! @brief A default copy constructor. + TagComponent(const TagComponent &) = default; + //! @brief A default destructor + ~TagComponent() override = default; + //! @brief A tag component is not assignable. + TagComponent &operator=(const TagComponent &) = delete; + }; + + constexpr const char Blowable[] = "Blowable"; +} diff --git a/sources/Map/Map.cpp b/sources/Map/Map.cpp index 754e080f..90e187f8 100644 --- a/sources/Map/Map.cpp +++ b/sources/Map/Map.cpp @@ -6,6 +6,7 @@ #include #include "Map.hpp" #include +#include namespace RAY3D = RAY::Drawables::Drawables3D; @@ -58,6 +59,7 @@ namespace BBM if (!(i % 2) && !(j % 2)) { scene->addEntity("Unbreakable Wall") .addComponent(i, 0, j) + .addComponent>() .addComponent(WAL::Callback(), &MapGenerator::wallCollide, .75) .addComponent(unbreakableObj, std::make_pair(MAP_DIFFUSE, unbreakablePng)); } @@ -72,24 +74,28 @@ namespace BBM scene->addEntity("Bottom Wall") .addComponent(Vector3f((width + 1) / 2, 0, -1)) + .addComponent>() .addComponent(WAL::Callback(), &MapGenerator::wallCollide, .75) .addComponent(unbreakableObj, std::make_pair(MAP_DIFFUSE, unbreakablePnj), RAY::Vector3(width + 3, 1, 1)); scene->addEntity("Upper Wall") .addComponent(Vector3f((width + 1) / 2, 0, height + 1)) + .addComponent>() .addComponent(WAL::Callback(), &MapGenerator::wallCollide, .75) .addComponent(unbreakableObj, std::make_pair(MAP_DIFFUSE, unbreakablePnj), RAY::Vector3(width + 3, 1, 1)); scene->addEntity("Left Wall") .addComponent(Vector3f(width + 1, 0, height / 2)) + .addComponent>() .addComponent(WAL::Callback(), &MapGenerator::wallCollide, .75) .addComponent(unbreakableObj, std::make_pair(MAP_DIFFUSE, unbreakablePnj), RAY::Vector3(1, 1, height + 1)); scene->addEntity("Right Wall") .addComponent(Vector3f(-1, 0, height / 2)) + .addComponent>() .addComponent(WAL::Callback(), &MapGenerator::wallCollide, .75) .addComponent(unbreakableObj, std::make_pair(MAP_DIFFUSE, unbreakablePnj), @@ -141,6 +147,7 @@ namespace BBM .addComponent(coords) .addComponent(1, &MapGenerator::wallDestroyed) .addComponent(WAL::Callback(), &MapGenerator::wallCollide, .75) + .addComponent>() .addComponent(breakableObj, std::make_pair(MAP_DIFFUSE, breakablePng)); } @@ -173,6 +180,7 @@ namespace BBM scene->addEntity("Unbreakable Block") .addComponent(coords) + .addComponent>() .addComponent(WAL::Callback(), &MapGenerator::wallCollide, .75) .addComponent(UnbreakableObj, std::make_pair(MAP_DIFFUSE, UnbreakablePng)); diff --git a/sources/Runner/Runner.cpp b/sources/Runner/Runner.cpp index 57494e5e..c88c9194 100644 --- a/sources/Runner/Runner.cpp +++ b/sources/Runner/Runner.cpp @@ -28,6 +28,7 @@ #include #include #include +#include #include "Component/Animation/AnimationsComponent.hpp" #include "System/Animation/AnimationsSystem.hpp" #include "Map/Map.hpp" @@ -77,6 +78,7 @@ namespace BBM .addComponent() .addComponent() .addComponent() + .addComponent>() .addComponent(RAY::ModelAnimations("assets/player/player.iqm"), 3) .addComponent(1) .addComponent() diff --git a/sources/System/BombHolder/BombHolderSystem.cpp b/sources/System/BombHolder/BombHolderSystem.cpp index 10c21960..e9ff4ab5 100644 --- a/sources/System/BombHolder/BombHolderSystem.cpp +++ b/sources/System/BombHolder/BombHolderSystem.cpp @@ -8,7 +8,8 @@ #include "BombHolderSystem.hpp" #include "Component/Health/HealthComponent.hpp" #include -#include +#include "Component/Collision/CollisionComponent.hpp" +#include "Component/Tag/TagComponent.hpp" using namespace std::chrono_literals; namespace RAY3D = RAY::Drawables::Drawables3D; @@ -25,22 +26,18 @@ namespace BBM { if (count <= 0) return; - std::cout << position << " count: " << count << std::endl; - // TODO use it in a global context with a find and so on. - wal.getSystem().dispatchEvent([position, &wal, count](WAL::Entity &entity) { - auto *health = entity.tryGetComponent(); - auto *pos = entity.tryGetComponent(); - - if (health && pos && pos->position.round() == position) { - std::cout << pos->position << std::endl; - health->takeDmg(1); - } - else { - _dispatchExplosion(position + Vector3f(1, 0, 0), wal, count - 1); - _dispatchExplosion(position + Vector3f(-1, 0, 0), wal, count - 1); - _dispatchExplosion(position + Vector3f(0, 0, 1), wal, count - 1); - _dispatchExplosion(position + Vector3f(0, 0, -1), wal, count - 1); + wal.getSystem().dispatchEvent([position, count](WAL::Wal &wal) { + for (auto &[entity, pos, _] : wal.scene->view>()) { + if (pos.position.round() == position) { + if (auto *health = entity.tryGetComponent()) + health->takeDmg(1); + return; + } } + _dispatchExplosion(position + Vector3f(1, 0, 0), wal, count - 1); + _dispatchExplosion(position + Vector3f(-1, 0, 0), wal, count - 1); + _dispatchExplosion(position + Vector3f(0, 0, 1), wal, count - 1); + _dispatchExplosion(position + Vector3f(0, 0, -1), wal, count - 1); }); } @@ -48,13 +45,13 @@ namespace BBM { bomb.scheduleDeletion(); auto position = bomb.getComponent().position.round(); - _dispatchExplosion(position, wal, 2); + _dispatchExplosion(position, wal, 3); } void BombHolderSystem::_spawnBomb(Vector3f position) { this->_wal.scene->scheduleNewEntity("Bomb") - .addComponent(position) + .addComponent(position.round()) .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/Event/EventSystem.cpp b/sources/System/Event/EventSystem.cpp index af20674f..da683dec 100644 --- a/sources/System/Event/EventSystem.cpp +++ b/sources/System/Event/EventSystem.cpp @@ -15,6 +15,11 @@ namespace BBM this->_events.emplace_back(event); } + void EventSystem::dispatchEvent(const std::function &event) + { + this->_globalEvents.emplace_back(event); + } + void EventSystem::onUpdate(WAL::ViewEntity<> &entity, std::chrono::nanoseconds) { for (auto &event : this->_events) @@ -23,6 +28,9 @@ namespace BBM void EventSystem::onSelfUpdate() { + for (auto &event : this->_globalEvents) + event(this->_wal); this->_events.clear(); + this->_globalEvents.clear(); } } \ No newline at end of file diff --git a/sources/System/Event/EventSystem.hpp b/sources/System/Event/EventSystem.hpp index 0a4c205f..bcdb14c0 100644 --- a/sources/System/Event/EventSystem.hpp +++ b/sources/System/Event/EventSystem.hpp @@ -14,11 +14,16 @@ namespace BBM { private: //! @brief The list of events that occurred in the last update. - std::vector> _events; + std::list> _events; + //! @brief The list of events that occurred in the last update. + std::list> _globalEvents; 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); + //! @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::ViewEntity<> &entity, std::chrono::nanoseconds dtime) override; //! @inherit