From fc5dfd2873279d5003f8a02187a71b05a44892fa Mon Sep 17 00:00:00 2001 From: Loek Le Blansch Date: Sat, 16 Nov 2024 16:46:18 +0100 Subject: final touchups 2 for #26 --- src/crepe/ComponentManager.h | 45 ++++++++++++++++++++++++++++----------- src/crepe/api/BehaviorScript.h | 37 ++++++++++++++++++-------------- src/crepe/api/BehaviorScript.hpp | 2 +- src/crepe/api/LoopManager.cpp | 2 -- src/crepe/api/LoopManager.h | 26 ++++++++++++++++------ src/crepe/api/Scene.h | 8 ++++--- src/crepe/api/Script.h | 44 +++++++++++++++++++++----------------- src/crepe/api/Script.hpp | 3 +-- src/crepe/system/ParticleSystem.h | 2 -- src/crepe/system/ScriptSystem.cpp | 5 ++--- 10 files changed, 106 insertions(+), 68 deletions(-) diff --git a/src/crepe/ComponentManager.h b/src/crepe/ComponentManager.h index 99d45d9..2107453 100644 --- a/src/crepe/ComponentManager.h +++ b/src/crepe/ComponentManager.h @@ -1,6 +1,5 @@ #pragma once -#include #include #include #include @@ -20,11 +19,40 @@ class GameObject; * 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 Vector2 & 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 * @@ -39,11 +67,6 @@ protected: */ template T & add_component(game_object_id_t id, Args &&... args); - //! GameObject is used as an interface to add components instead of the - // component manager directly - friend class GameObject; - -public: /** * \brief Delete all components of a specific type and id * @@ -77,6 +100,8 @@ public: * This method deletes all components. */ void delete_all_components(); + +public: /** * \brief Get all components of a specific type and id * @@ -99,11 +124,6 @@ public: template std::vector> get_components_by_type() const; - // TODO: doxygen - GameObject new_object(const std::string & name, const std::string & tag = "", - const Vector2 & position = {0, 0}, double rotation = 0, - double scale = 0); - private: /** * \brief The components @@ -118,9 +138,8 @@ private: std::unordered_map>>> components; - //! ID of next GameObject + //! ID of next GameObject allocated by \c ComponentManager::new_object game_object_id_t next_id = 0; - std::forward_list> objects; }; } // namespace crepe diff --git a/src/crepe/api/BehaviorScript.h b/src/crepe/api/BehaviorScript.h index f7d484a..9d85d4c 100644 --- a/src/crepe/api/BehaviorScript.h +++ b/src/crepe/api/BehaviorScript.h @@ -15,12 +15,21 @@ class Script; /** * \brief Script component * - * This class acts as a (component) wrapper around an instance of (a class - * derivatived from) \c Script. \c BehaviorScript is the only ECS component - * that stores member function implementations as data. + * This class acts as a (component) wrapper around an instance of (a class derivatived from) \c + * Script. \c BehaviorScript is the only ECS component that stores member function + * implementations as data. */ class BehaviorScript : public Component { protected: + /** + * \param id Parent \c GameObject id + * \param component_manager Reference to component manager (passed through to \c Script + * instance) + * + * \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); //! Only ComponentManager is allowed to instantiate BehaviorScript friend class ComponentManager; @@ -37,27 +46,23 @@ public: BehaviorScript & set_script(); protected: - //! ScriptSystem needs direct access to the script instance - friend class ScriptSystem; - //! Flag to indicate if script->init() has been called already - bool initialized = false; //! Script instance std::unique_ptr