From 2be7c36902c9a951efcc2b24061e8882fd911a60 Mon Sep 17 00:00:00 2001 From: Zoe Roux Date: Fri, 21 May 2021 16:02:00 +0200 Subject: [PATCH] Creating a callback type --- lib/wal/CMakeLists.txt | 5 +-- lib/wal/sources/Events/EventManager.cpp | 5 --- lib/wal/sources/Events/EventManager.hpp | 15 ------- lib/wal/sources/Models/Callback.hpp | 54 +++++++++++++++++++++++++ lib/wal/sources/Models/Vector3.hpp | 4 +- lib/wal/sources/Scene/Scene.cpp | 2 +- lib/wal/sources/Scene/SceneManager.cpp | 32 --------------- lib/wal/sources/Scene/SceneManager.hpp | 42 ------------------- lib/wal/sources/Wal.cpp | 9 +---- lib/wal/sources/Wal.hpp | 10 +---- 10 files changed, 62 insertions(+), 116 deletions(-) delete mode 100644 lib/wal/sources/Events/EventManager.cpp delete mode 100644 lib/wal/sources/Events/EventManager.hpp create mode 100644 lib/wal/sources/Models/Callback.hpp delete mode 100644 lib/wal/sources/Scene/SceneManager.cpp delete mode 100644 lib/wal/sources/Scene/SceneManager.hpp diff --git a/lib/wal/CMakeLists.txt b/lib/wal/CMakeLists.txt index 335f26ce..0d308025 100644 --- a/lib/wal/CMakeLists.txt +++ b/lib/wal/CMakeLists.txt @@ -9,12 +9,8 @@ add_library(wal sources/System/System.hpp sources/Wal.cpp sources/Wal.hpp - sources/Scene/SceneManager.cpp - sources/Scene/SceneManager.hpp sources/Scene/Scene.cpp sources/Scene/Scene.hpp - sources/Events/EventManager.cpp - sources/Events/EventManager.hpp sources/Exception/WalError.cpp sources/Exception/WalError.hpp sources/Entity/Entity.cpp @@ -27,6 +23,7 @@ add_library(wal sources/System/Movable/MovableSystem.cpp sources/System/Movable/MovableSystem.hpp sources/System/System.cpp + sources/Models/Callback.hpp ) target_include_directories(wal PUBLIC sources) diff --git a/lib/wal/sources/Events/EventManager.cpp b/lib/wal/sources/Events/EventManager.cpp deleted file mode 100644 index 69b939b7..00000000 --- a/lib/wal/sources/Events/EventManager.cpp +++ /dev/null @@ -1,5 +0,0 @@ -// -// Created by Zoe Roux on 2021-05-14. -// - -#include "EventManager.hpp" diff --git a/lib/wal/sources/Events/EventManager.hpp b/lib/wal/sources/Events/EventManager.hpp deleted file mode 100644 index a1f608e1..00000000 --- a/lib/wal/sources/Events/EventManager.hpp +++ /dev/null @@ -1,15 +0,0 @@ -// -// Created by Zoe Roux on 2021-05-14. -// - - -#pragma once - -namespace WAL -{ - //! @brief A class to handle events. - class EventManager - { - - }; -} diff --git a/lib/wal/sources/Models/Callback.hpp b/lib/wal/sources/Models/Callback.hpp new file mode 100644 index 00000000..6eec8778 --- /dev/null +++ b/lib/wal/sources/Models/Callback.hpp @@ -0,0 +1,54 @@ +// +// Created by Zoe Roux on 5/21/21. +// + + +#pragma once + +#include + +namespace WAL +{ + //! @brief A callback where you can subscribe to and emit it. + template + class Callback + { + private: + int _nextID = 0; + //! @brief The list of functions to call. + std::unordered_map> _functions = {}; + + public: + //! @brief Add a method to be called when this callback is invoked. + //! @param callback The list of arguments of the callback method + //! @return A unique ID for this callback. That can be used to remove the callback later. + int addCallback(std::function callback) + { + int id = this->_nextID++; + this->_functions.emplace_back(id, std::move(callback)); + return id; + } + + //! @brief Remove a function from this callback. + //! @param id The ID of the function. + void removeCallback(int id) + { + this->_functions.erase(id); + } + + void operator()(Types ...args) const + { + for (auto &callback : this->_functions) + callback(args...); + } + + //! @brief A default constructor + Callback() = default; + //! @brief A default copy constructor + Callback(const Callback &) = default; + //! @brief A default destructor + ~Callback() = default; + //! @brief A default assignment operator + Callback &operator=(const Callback &) = default; + }; +} \ No newline at end of file diff --git a/lib/wal/sources/Models/Vector3.hpp b/lib/wal/sources/Models/Vector3.hpp index 05391c25..9aa58d10 100644 --- a/lib/wal/sources/Models/Vector3.hpp +++ b/lib/wal/sources/Models/Vector3.hpp @@ -23,12 +23,12 @@ namespace WAL T z; //! @brief Create a new nil vector3. - Vector3() + Vector3() : x(0), y(0), z(0) {} //! @brief Create a new vector3 representing a specific coordinate. - Vector3(T x, T y, T z) + Vector3(T x, T y, T z) : x(x), y(y), z(z) {} diff --git a/lib/wal/sources/Scene/Scene.cpp b/lib/wal/sources/Scene/Scene.cpp index 2fa115f5..5c0ef26e 100644 --- a/lib/wal/sources/Scene/Scene.cpp +++ b/lib/wal/sources/Scene/Scene.cpp @@ -13,7 +13,7 @@ namespace WAL void Scene::removeAll(std::function &predicate) { - this->_entities.erase(std::remove_if(this->_entities.begin(), this->_entities.end(), predicate), this->_entities.end()); +// this->_entities.erase(std::remove_if(this->_entities.begin(), this->_entities.end(), predicate), this->_entities.end()); } } diff --git a/lib/wal/sources/Scene/SceneManager.cpp b/lib/wal/sources/Scene/SceneManager.cpp deleted file mode 100644 index 6bfda2bb..00000000 --- a/lib/wal/sources/Scene/SceneManager.cpp +++ /dev/null @@ -1,32 +0,0 @@ -// -// Created by Zoe Roux on 2021-05-14. -// - -#include "SceneManager.hpp" - -namespace WAL -{ - Scene &WAL::SceneManager::addScene(WAL::Scene &&scene) - { - this->_scenes.push_front(scene); - return this->getCurrent(); - } - - Scene &SceneManager::addBackScene(Scene &&scene) - { - this->_scenes.insert(++this->_scenes.begin(), scene); - return *(this->_scenes.begin() + 1); - } - - Scene &SceneManager::getCurrent() - { - if (this->_scenes.empty()) - throw NotFoundError("No scene exists."); - return this->_scenes.front(); - } - - void SceneManager::closeCurrent() - { - this->_scenes.pop_front(); - } -} \ No newline at end of file diff --git a/lib/wal/sources/Scene/SceneManager.hpp b/lib/wal/sources/Scene/SceneManager.hpp deleted file mode 100644 index d77b5f3c..00000000 --- a/lib/wal/sources/Scene/SceneManager.hpp +++ /dev/null @@ -1,42 +0,0 @@ -// -// Created by Zoe Roux on 2021-05-14. -// - - -#pragma once - -#include -#include "Scene/Scene.hpp" - -namespace WAL -{ - //! @brief A class to manage scenes - class SceneManager - { - private: - std::deque _scenes = {}; - public: - //! @brief Add a scene to the container and move to it. - //! @return The newly added scene, to chain call. - Scene &addScene(Scene &&scene); - - //! @brief Add a scene before the current scene. This could be useful for lobbies or scene where the next scene can be constructed. - //! @return The newly added scene, to chain call. - Scene &addBackScene(Scene &&scene); - - //! @breif Get the current scene - Scene &getCurrent(); - - //! @brief Remove the current scene and switch to the previous scene on the stack. - void closeCurrent(); - - //! @brief A default constructor - SceneManager() = default; - //! @brief A scene manager is copy constructable - SceneManager(const SceneManager &) = default; - //! @brief A default destructor. - ~SceneManager() = default; - //! @brief A scene manager is assignable - SceneManager &operator=(const SceneManager &) = default; - }; -} diff --git a/lib/wal/sources/Wal.cpp b/lib/wal/sources/Wal.cpp index 569743aa..e1598dd7 100644 --- a/lib/wal/sources/Wal.cpp +++ b/lib/wal/sources/Wal.cpp @@ -11,11 +11,6 @@ namespace WAL { std::chrono::nanoseconds Wal::timestep = 8ms; - SceneManager &Wal::getSceneManager() - { - return this->_sceneManager; - } - void Wal::run() { auto lastTick = std::chrono::steady_clock::now(); @@ -37,7 +32,7 @@ namespace WAL void Wal::_update(std::chrono::nanoseconds dtime) { - auto &entities = this->_sceneManager.getCurrent().getEntities(); + auto &entities = this->_scene.getEntities(); for (auto &system : this->_systems) { for (auto &entity : entities) { @@ -53,7 +48,7 @@ namespace WAL void Wal::_fixedUpdate() { - auto &entities = this->_sceneManager.getCurrent().getEntities(); + auto &entities = this->_scene.getEntities(); for (auto &system : this->_systems) { for (auto &entity : entities) { diff --git a/lib/wal/sources/Wal.hpp b/lib/wal/sources/Wal.hpp index ea72eb05..8b21a4c8 100644 --- a/lib/wal/sources/Wal.hpp +++ b/lib/wal/sources/Wal.hpp @@ -9,8 +9,7 @@ #include #include #include -#include "Events/EventManager.hpp" -#include "Scene/SceneManager.hpp" +#include "Scene/Scene.hpp" #include "Entity/Entity.hpp" #include "System/System.hpp" @@ -21,9 +20,7 @@ namespace WAL { private: //! @brief The scene manager that allow multiple scene to work together. - SceneManager _sceneManager; - //! @brief The event manager - EventManager _eventManager; + Scene _scene; //! @brief The list of registered systems std::vector> _systems = {}; //! @brief True if the engine should close after the end of the current tick. @@ -97,9 +94,6 @@ namespace WAL return *this; } - //! @brief Get the scene manager. - SceneManager &getSceneManager(); - //! @brief Start the game loop void run();