diff options
author | Loek Le Blansch <loek@pipeframe.xyz> | 2024-11-13 16:26:38 +0100 |
---|---|---|
committer | Loek Le Blansch <loek@pipeframe.xyz> | 2024-11-13 16:26:38 +0100 |
commit | b63f4700f7bda696afb14cc3111be0f8b0eed458 (patch) | |
tree | 08c4b00b249a4fa672d97a5bc79927adac0a0257 /src/crepe/api/BehaviorScript.h | |
parent | 2933655dea64f11f200f42fe51e58dacc5f160eb (diff) | |
parent | 9e87a556a5f68c5f9bb04bef9a66880536ccd6e8 (diff) |
merge `loek/tests` into `loek/cleanup` (close #32)
Diffstat (limited to 'src/crepe/api/BehaviorScript.h')
-rw-r--r-- | src/crepe/api/BehaviorScript.h | 37 |
1 files changed, 34 insertions, 3 deletions
diff --git a/src/crepe/api/BehaviorScript.h b/src/crepe/api/BehaviorScript.h index 4160a72..2982358 100644 --- a/src/crepe/api/BehaviorScript.h +++ b/src/crepe/api/BehaviorScript.h @@ -3,6 +3,7 @@ #include <memory> #include "../Component.h" +#include "GameObject.h" namespace crepe { @@ -19,22 +20,52 @@ class Script; */ class BehaviorScript : public Component { protected: - friend class crepe::ComponentManager; - using Component::Component; + BehaviorScript(game_object_id_t id, ComponentManager & component_manager); + //! Only ComponentManager is allowed to instantiate BehaviorScript + friend class ComponentManager; public: ~BehaviorScript() = default; public: + /** + * \brief Set the concrete script of this component + * + * \tparam T Concrete script type (derived from \c crepe::Script) + * + * \returns Reference to BehaviorScript component (`*this`) + */ template <class T> BehaviorScript & set_script(); protected: - friend class crepe::ScriptSystem; + //! 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<Script> script = nullptr; + //! Reference to component manager + ComponentManager & component_manager; + +private: + //! Script accesses the component manager directly via its parent + // (BehaviorScript) reference + friend class Script; }; +/** + * \brief Add a BehaviorScript component to this game object + * + * The \c BehaviorScript class is the only exception to the ECS harmony, and + * requires a reference to the component manager passed to its constructor in + * order to function normally. This is because the \c BehaviorScript (and \c + * Script) classes are the only component-related classes that store + * implemented member functions as data. + */ +template <> +BehaviorScript & GameObject::add_component<BehaviorScript>(); + } // namespace crepe #include "BehaviorScript.hpp" |