diff options
| author | WBoerenkamps <wrj.boerenkamps@student.avans.nl> | 2024-11-18 15:34:22 +0100 | 
|---|---|---|
| committer | WBoerenkamps <wrj.boerenkamps@student.avans.nl> | 2024-11-18 15:34:22 +0100 | 
| commit | b92da3c729c8fc7c002cb3acbb75c56cb63bd89e (patch) | |
| tree | 454b64d31b7e0c9151da88a0d49ee782565f49e9 /src/crepe/api/BehaviorScript.h | |
| parent | 8b449e764b5c91cd3cb0c4fa404a290ab295a7ef (diff) | |
| parent | 121b64b1cb6cfead5814070c8b0185d3d7308095 (diff) | |
Merge branch 'master' of https://github.com/lonkaars/crepe into wouter/events
Diffstat (limited to 'src/crepe/api/BehaviorScript.h')
| -rw-r--r-- | src/crepe/api/BehaviorScript.h | 52 | 
1 files changed, 46 insertions, 6 deletions
| diff --git a/src/crepe/api/BehaviorScript.h b/src/crepe/api/BehaviorScript.h index 6b1fec7..9d85d4c 100644 --- a/src/crepe/api/BehaviorScript.h +++ b/src/crepe/api/BehaviorScript.h @@ -4,29 +4,69 @@  #include "../Component.h" +#include "GameObject.h" +  namespace crepe {  class ScriptSystem;  class ComponentManager;  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. + */  class BehaviorScript : public Component {  protected: -	friend class crepe::ComponentManager; -	using Component::Component; - -public: -	virtual ~BehaviorScript() = default; +	/** +	 * \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;  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; +	//! Script instance  	std::unique_ptr<Script> script = nullptr; +	//! ScriptSystem needs direct access to the script instance +	friend class ScriptSystem; + +protected: +	//! Reference to component manager (passed to Script) +	ComponentManager & component_manager;  }; +/** + * \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" |