From 6fd7cec7d4bbf5aeb361b3f1337671bb0f9af61b Mon Sep 17 00:00:00 2001 From: Loek Le Blansch Date: Thu, 28 Nov 2024 10:13:11 +0100 Subject: manager mediator refactor --- src/crepe/CMakeLists.txt | 4 +- src/crepe/ComponentManager.cpp | 60 ------------ src/crepe/ComponentManager.h | 159 ------------------------------ src/crepe/ComponentManager.hpp | 161 ------------------------------ src/crepe/api/BehaviorScript.cpp | 6 +- src/crepe/api/BehaviorScript.h | 9 +- src/crepe/api/BehaviorScript.hpp | 4 +- src/crepe/api/CMakeLists.txt | 8 -- src/crepe/api/EventManager.cpp | 46 --------- src/crepe/api/EventManager.h | 161 ------------------------------ src/crepe/api/EventManager.hpp | 36 ------- src/crepe/api/GameObject.hpp | 2 +- src/crepe/api/IKeyListener.h | 3 +- src/crepe/api/IMouseListener.h | 3 +- src/crepe/api/LoopManager.cpp | 25 ++--- src/crepe/api/LoopManager.h | 19 +++- src/crepe/api/LoopManager.hpp | 7 +- src/crepe/api/SaveManager.cpp | 173 --------------------------------- src/crepe/api/SaveManager.h | 114 ---------------------- src/crepe/api/Scene.h | 7 +- src/crepe/api/SceneManager.cpp | 34 ------- src/crepe/api/SceneManager.h | 53 ---------- src/crepe/api/SceneManager.hpp | 25 ----- src/crepe/api/Script.cpp | 17 +++- src/crepe/api/Script.h | 16 +-- src/crepe/api/Script.hpp | 8 +- src/crepe/manager/CMakeLists.txt | 20 ++++ src/crepe/manager/ComponentManager.cpp | 63 ++++++++++++ src/crepe/manager/ComponentManager.h | 161 ++++++++++++++++++++++++++++++ src/crepe/manager/ComponentManager.hpp | 161 ++++++++++++++++++++++++++++++ src/crepe/manager/EventManager.cpp | 46 +++++++++ src/crepe/manager/EventManager.h | 161 ++++++++++++++++++++++++++++++ src/crepe/manager/EventManager.hpp | 36 +++++++ src/crepe/manager/Manager.cpp | 6 ++ src/crepe/manager/Manager.h | 17 ++++ src/crepe/manager/Mediator.h | 29 ++++++ src/crepe/manager/SaveManager.cpp | 173 +++++++++++++++++++++++++++++++++ src/crepe/manager/SaveManager.h | 114 ++++++++++++++++++++++ src/crepe/manager/SceneManager.cpp | 35 +++++++ src/crepe/manager/SceneManager.h | 52 ++++++++++ src/crepe/manager/SceneManager.hpp | 25 +++++ src/crepe/system/AnimatorSystem.cpp | 6 +- src/crepe/system/ParticleSystem.cpp | 6 +- src/crepe/system/PhysicsSystem.cpp | 2 +- src/crepe/system/RenderSystem.cpp | 3 +- src/crepe/system/ScriptSystem.cpp | 2 +- src/crepe/system/System.cpp | 2 +- src/crepe/system/System.h | 4 +- 48 files changed, 1196 insertions(+), 1088 deletions(-) delete mode 100644 src/crepe/ComponentManager.cpp delete mode 100644 src/crepe/ComponentManager.h delete mode 100644 src/crepe/ComponentManager.hpp delete mode 100644 src/crepe/api/EventManager.cpp delete mode 100644 src/crepe/api/EventManager.h delete mode 100644 src/crepe/api/EventManager.hpp delete mode 100644 src/crepe/api/SaveManager.cpp delete mode 100644 src/crepe/api/SaveManager.h delete mode 100644 src/crepe/api/SceneManager.cpp delete mode 100644 src/crepe/api/SceneManager.h delete mode 100644 src/crepe/api/SceneManager.hpp create mode 100644 src/crepe/manager/CMakeLists.txt create mode 100644 src/crepe/manager/ComponentManager.cpp create mode 100644 src/crepe/manager/ComponentManager.h create mode 100644 src/crepe/manager/ComponentManager.hpp create mode 100644 src/crepe/manager/EventManager.cpp create mode 100644 src/crepe/manager/EventManager.h create mode 100644 src/crepe/manager/EventManager.hpp create mode 100644 src/crepe/manager/Manager.cpp create mode 100644 src/crepe/manager/Manager.h create mode 100644 src/crepe/manager/Mediator.h create mode 100644 src/crepe/manager/SaveManager.cpp create mode 100644 src/crepe/manager/SaveManager.h create mode 100644 src/crepe/manager/SceneManager.cpp create mode 100644 src/crepe/manager/SceneManager.h create mode 100644 src/crepe/manager/SceneManager.hpp (limited to 'src/crepe') diff --git a/src/crepe/CMakeLists.txt b/src/crepe/CMakeLists.txt index 7e176e7..da9d492 100644 --- a/src/crepe/CMakeLists.txt +++ b/src/crepe/CMakeLists.txt @@ -1,13 +1,10 @@ target_sources(crepe PUBLIC Particle.cpp - ComponentManager.cpp Component.cpp Collider.cpp ) target_sources(crepe PUBLIC FILE_SET HEADERS FILES - ComponentManager.h - ComponentManager.hpp Component.h Collider.h ValueBroker.h @@ -16,6 +13,7 @@ target_sources(crepe PUBLIC FILE_SET HEADERS FILES add_subdirectory(api) add_subdirectory(facade) +add_subdirectory(manager) add_subdirectory(system) add_subdirectory(util) diff --git a/src/crepe/ComponentManager.cpp b/src/crepe/ComponentManager.cpp deleted file mode 100644 index 5b73009..0000000 --- a/src/crepe/ComponentManager.cpp +++ /dev/null @@ -1,60 +0,0 @@ -#include "api/GameObject.h" -#include "util/Log.h" - -#include "ComponentManager.h" -#include "types.h" - -using namespace crepe; -using namespace std; - -ComponentManager::ComponentManager() { dbg_trace(); } -ComponentManager::~ComponentManager() { dbg_trace(); } - -void ComponentManager::delete_all_components_of_id(game_object_id_t id) { - // Do not delete persistent objects - if (this->persistent[id]) { - return; - } - - // Loop through all the types (in the unordered_map<>) - for (auto & [type, component_array] : this->components) { - // Make sure that the id (that we are looking for) is within the boundaries of the vector<> - if (id < component_array.size()) { - // Clear the components at this specific id - component_array[id].clear(); - } - } -} - -void ComponentManager::delete_all_components() { - // Loop through all the types (in the unordered_map<>) - for (auto & [type, component_array] : this->components) { - // Loop through all the ids (in the vector<>) - for (game_object_id_t id = 0; id < component_array.size(); id++) { - // Do not delete persistent objects - if (!this->persistent[id]) { - // Clear the components at this specific id - component_array[id].clear(); - } - } - } - - this->next_id = 0; -} - -GameObject ComponentManager::new_object(const string & name, const string & tag, - const vec2 & position, double rotation, double scale) { - // Find the first available id (taking persistent objects into account) - while (this->persistent[this->next_id]) { - this->next_id++; - } - - GameObject object{*this, this->next_id, name, tag, position, rotation, scale}; - this->next_id++; - - return object; -} - -void ComponentManager::set_persistent(game_object_id_t id, bool persistent) { - this->persistent[id] = persistent; -} diff --git a/src/crepe/ComponentManager.h b/src/crepe/ComponentManager.h deleted file mode 100644 index 480124f..0000000 --- a/src/crepe/ComponentManager.h +++ /dev/null @@ -1,159 +0,0 @@ -#pragma once - -#include -#include -#include -#include - -#include "Component.h" -#include "types.h" - -namespace crepe { - -class GameObject; - -/** - * \brief Manages all components - * - * This class manages all components. It provides methods to add, delete and get components. - */ -class ComponentManager { - // TODO: This relation should be removed! I (loek) believe that the scene manager should - // create/destroy components because the GameObject's are stored in concrete Scene classes, - // which will in turn call GameObject's destructor, which will in turn call - // ComponentManager::delete_components_by_id or something. This is a pretty major change, so - // here is a comment and temporary fix instead :tada: - friend class SceneManager; - -public: - ComponentManager(); // dbg_trace - ~ComponentManager(); // dbg_trace - - /** - * \brief Create a new game object using the component manager - * - * \param name Metadata::name (required) - * \param tag Metadata::tag (optional, empty by default) - * \param position Transform::position (optional, origin by default) - * \param rotation Transform::rotation (optional, 0 by default) - * \param scale Transform::scale (optional, 1 by default) - * - * \returns GameObject interface - * - * \note This method automatically assigns a new entity ID - */ - GameObject new_object(const std::string & name, const std::string & tag = "", - const vec2 & position = {0, 0}, double rotation = 0, - double scale = 1); - -protected: - /** - * GameObject is used as an interface to add/remove components, and the game programmer is - * supposed to use it instead of interfacing with the component manager directly. - */ - friend class GameObject; - /** - * \brief Add a component to the ComponentManager - * - * This method adds a component to the ComponentManager. The component is created with the - * given arguments and added to the ComponentManager. - * - * \tparam T The type of the component - * \tparam Args The types of the arguments - * \param id The id of the GameObject this component belongs to - * \param args The arguments to create the component - * \return The created component - */ - template - T & add_component(game_object_id_t id, Args &&... args); - /** - * \brief Delete all components of a specific type and id - * - * This method deletes all components of a specific type and id. - * - * \tparam T The type of the component - * \param id The id of the GameObject this component belongs to - */ - template - void delete_components_by_id(game_object_id_t id); - /** - * \brief Delete all components of a specific type - * - * This method deletes all components of a specific type. - * - * \tparam T The type of the component - */ - template - void delete_components(); - /** - * \brief Delete all components of a specific id - * - * This method deletes all components of a specific id. - * - * \param id The id of the GameObject this component belongs to - */ - void delete_all_components_of_id(game_object_id_t id); - /** - * \brief Delete all components - * - * This method deletes all components. - */ - void delete_all_components(); - /** - * \brief Set a GameObject as persistent - * - * This method sets a GameObject as persistent. If a GameObject is persistent, its - * components will not be deleted. - * - * \param id The id of the GameObject to set as persistent - * \param persistent The persistent flag - */ - void set_persistent(game_object_id_t id, bool persistent); - -public: - /** - * \brief Get all components of a specific type and id - * - * This method gets all components of a specific type and id. - * - * \tparam T The type of the component - * \param id The id of the GameObject this component belongs to - * \return A vector of all components of the specific type and id - */ - template - RefVector get_components_by_id(game_object_id_t id) const; - /** - * \brief Get all components of a specific type - * - * This method gets all components of a specific type. - * - * \tparam T The type of the component - * \return A vector of all components of the specific type - */ - template - RefVector get_components_by_type() const; - -private: - /** - * \brief The components - * - * This unordered_map stores all components. The key is the type of the component and the - * value is a vector of vectors of unique pointers to the components. - * - * Every component type has its own vector of vectors of unique pointers to the components. - * The first vector is for the ids of the GameObjects and the second vector is for the - * components (because a GameObject might have multiple components). - */ - std::unordered_map>>> - components; - - //! Persistent flag for each GameObject - std::unordered_map persistent; - - //! ID of next GameObject allocated by \c ComponentManager::new_object - game_object_id_t next_id = 0; -}; - -} // namespace crepe - -#include "ComponentManager.hpp" diff --git a/src/crepe/ComponentManager.hpp b/src/crepe/ComponentManager.hpp deleted file mode 100644 index ffb38ec..0000000 --- a/src/crepe/ComponentManager.hpp +++ /dev/null @@ -1,161 +0,0 @@ -#pragma once - -#include - -#include "ComponentManager.h" -#include "types.h" - -namespace crepe { - -template -T & ComponentManager::add_component(game_object_id_t id, Args &&... args) { - using namespace std; - - static_assert(is_base_of::value, - "add_component must recieve a derivative class of Component"); - - // Determine the type of T (this is used as the key of the unordered_map<>) - type_index type = typeid(T); - - // Check if this component type is already in the unordered_map<> - if (this->components.find(type) == this->components.end()) { - //If not, create a new (empty) vector<> of vector> - this->components[type] = vector>>(); - } - - // Resize the vector<> if the id is greater than the current size - if (id >= this->components[type].size()) { - // Initialize new slots to nullptr (resize does automatically init to nullptr) - this->components[type].resize(id + 1); - } - - // Create a new component of type T (arguments directly forwarded). The - // constructor must be called by ComponentManager. - T * instance_ptr = new T(id, forward(args)...); - if (instance_ptr == nullptr) throw std::bad_alloc(); - - T & instance_ref = *instance_ptr; - unique_ptr instance = unique_ptr(instance_ptr); - - // Check if the vector size is not greater than get_instances_max - int max_instances = instance->get_instances_max(); - if (max_instances != -1 && components[type][id].size() >= max_instances) { - throw std::runtime_error( - "Exceeded maximum number of instances for this component type"); - } - - // store its unique_ptr in the vector<> - this->components[type][id].push_back(std::move(instance)); - - return instance_ref; -} - -template -void ComponentManager::delete_components_by_id(game_object_id_t id) { - using namespace std; - - // Do not delete persistent objects - if (this->persistent[id]) { - return; - } - - // Determine the type of T (this is used as the key of the unordered_map<>) - type_index type = typeid(T); - - // Find the type (in the unordered_map<>) - if (this->components.find(type) != this->components.end()) { - // Get the correct vector<> - vector>> & component_array = this->components[type]; - - // Make sure that the id (that we are looking for) is within the boundaries of the vector<> - if (id < component_array.size()) { - // Clear the whole vector<> of this specific type and id - component_array[id].clear(); - } - } -} - -template -void ComponentManager::delete_components() { - // Determine the type of T (this is used as the key of the unordered_map<>) - std::type_index type = typeid(T); - - if (this->components.find(type) == this->components.end()) return; - - // Loop through the whole vector<> of this specific type - for (game_object_id_t i = 0; i < this->components[type].size(); ++i) { - // Do not delete persistent objects - if (!this->persistent[i]) { - this->components[type][i].clear(); - } - } -} - -template -RefVector ComponentManager::get_components_by_id(game_object_id_t id) const { - using namespace std; - - // Determine the type of T (this is used as the key of the unordered_map<>) - type_index type = typeid(T); - - // Create an empty vector<> - RefVector component_vector; - - if (this->components.find(type) == this->components.end()) return component_vector; - - // Get the correct vector<> - const vector>> & component_array = this->components.at(type); - - // Make sure that the id (that we are looking for) is within the boundaries of the vector<> - if (id >= component_array.size()) return component_vector; - - // Loop trough the whole vector<> - for (const unique_ptr & component_ptr : component_array[id]) { - // Cast the unique_ptr to a raw pointer - T * casted_component = static_cast(component_ptr.get()); - - if (casted_component == nullptr) continue; - - // Add the dereferenced raw pointer to the vector<> - component_vector.push_back(*casted_component); - } - - return component_vector; -} - -template -RefVector ComponentManager::get_components_by_type() const { - using namespace std; - - // Determine the type of T (this is used as the key of the unordered_map<>) - type_index type = typeid(T); - - // Create an empty vector<> - RefVector component_vector; - - // Find the type (in the unordered_map<>) - if (this->components.find(type) == this->components.end()) return component_vector; - - // Get the correct vector<> - const vector>> & component_array = this->components.at(type); - - // Loop through the whole vector<> - for (const vector> & component : component_array) { - // Loop trough the whole vector<> - for (const unique_ptr & component_ptr : component) { - // Cast the unique_ptr to a raw pointer - T * casted_component = static_cast(component_ptr.get()); - - // Ensure that the cast was successful - if (casted_component == nullptr) continue; - - // Add the dereferenced raw pointer to the vector<> - component_vector.emplace_back(ref(*casted_component)); - } - } - - // Return the vector<> - return component_vector; -} - -} // namespace crepe diff --git a/src/crepe/api/BehaviorScript.cpp b/src/crepe/api/BehaviorScript.cpp index 7bbace0..d22afdf 100644 --- a/src/crepe/api/BehaviorScript.cpp +++ b/src/crepe/api/BehaviorScript.cpp @@ -4,12 +4,12 @@ using namespace crepe; -BehaviorScript::BehaviorScript(game_object_id_t id, ComponentManager & mgr) +BehaviorScript::BehaviorScript(game_object_id_t id, Mediator & mediator) : Component(id), - component_manager(mgr) {} + mediator(mediator) {} template <> BehaviorScript & GameObject::add_component() { ComponentManager & mgr = this->component_manager; - return mgr.add_component(this->id, mgr); + return mgr.add_component(this->id, mgr.mediator); } diff --git a/src/crepe/api/BehaviorScript.h b/src/crepe/api/BehaviorScript.h index d556fe5..3909b96 100644 --- a/src/crepe/api/BehaviorScript.h +++ b/src/crepe/api/BehaviorScript.h @@ -23,14 +23,13 @@ class BehaviorScript : public Component { protected: /** * \param id Parent \c GameObject id - * \param component_manager Reference to component manager (passed through to \c Script - * instance) + * \param mediator Mediator reference * * \note Calls to this constructor (should) always pass through \c GameObject::add_component, * which has an exception for this specific component type. This was done so the user does * not have to pass references used within \c Script to each \c BehaviorScript instance. */ - BehaviorScript(game_object_id_t id, ComponentManager & component_manager); + BehaviorScript(game_object_id_t id, Mediator & mediator); //! Only ComponentManager is allowed to instantiate BehaviorScript friend class ComponentManager; @@ -55,8 +54,8 @@ protected: friend class ScriptSystem; protected: - //! Reference to component manager (passed to Script) - ComponentManager & component_manager; + //! Reference mediator + Mediator & mediator; }; /** diff --git a/src/crepe/api/BehaviorScript.hpp b/src/crepe/api/BehaviorScript.hpp index bd59337..6de0157 100644 --- a/src/crepe/api/BehaviorScript.hpp +++ b/src/crepe/api/BehaviorScript.hpp @@ -14,11 +14,11 @@ BehaviorScript & BehaviorScript::set_script(Args &&... args) { dbg_trace(); static_assert(std::is_base_of::value); Script * s = new T(std::forward(args)...); + Mediator & mediator = this->mediator; s->game_object_id = this->game_object_id; s->active = this->active; - s->component_manager = this->component_manager; - s->event_manager = EventManager::get_instance(); + s->mediator = mediator; this->script = std::unique_ptr