diff options
author | Loek Le Blansch <loek@pipeframe.xyz> | 2024-11-14 11:26:12 +0100 |
---|---|---|
committer | Loek Le Blansch <loek@pipeframe.xyz> | 2024-11-14 11:26:12 +0100 |
commit | 01c09a196c3f3e5cefaa4119a95a1cdeb7b9c263 (patch) | |
tree | 5667369a85bd06b683c67de42bf0311c2647912b /src/crepe/api/Script.h | |
parent | 6e13510f3c6d4155707f748d237bb1fa05243450 (diff) | |
parent | 8600b8a29351aae26ec7b22f84aeeef92d8cb421 (diff) |
merge `loek/cleanup` into `loek/audio`
Diffstat (limited to 'src/crepe/api/Script.h')
-rw-r--r-- | src/crepe/api/Script.h | 61 |
1 files changed, 53 insertions, 8 deletions
diff --git a/src/crepe/api/Script.h b/src/crepe/api/Script.h index 49e625f..68a46d7 100644 --- a/src/crepe/api/Script.h +++ b/src/crepe/api/Script.h @@ -3,18 +3,36 @@ #include <vector> namespace crepe { -class ScriptSystem; -} - -namespace crepe { +class ScriptSystem; class BehaviorScript; +class ComponentManager; +/** + * \brief Script interface + * + * This class is used as a base class for user-defined scripts that can be + * added to game objects using the \c BehaviorScript component. + */ class Script { + //! ScriptSystem calls \c update() friend class crepe::ScriptSystem; protected: + /** + * \brief Script initialization function + * + * This function is called during the ScriptSystem::update() routine *before* + * Script::update() if it (a) has not yet been called and (b) the \c + * BehaviorScript component holding this script instance is active. + */ virtual void init() {} + /** + * \brief Script update function + * + * This function is called during the ScriptSystem::update() routine if the + * \c BehaviorScript component holding this script instance is active. + */ virtual void update() {} // NOTE: additional *events* (like unity's OnDisable and OnEnable) should be // implemented as member methods in derivative user script classes and @@ -22,15 +40,42 @@ protected: // added event. protected: + /** + * \brief Get single component of type \c T on this game object (utility) + * + * \tparam T Type of component + * + * \returns Reference to component + * + * \throws nullptr if this game object does not have a component matching + * type \c T + */ template <typename T> - T & get_component(); + T & get_component() const; + /** + * \brief Get all components of type \c T on this game object (utility) + * + * \tparam T Type of component + * + * \returns List of component references + */ template <typename T> - std::vector<std::reference_wrapper<T>> get_components(); + std::vector<std::reference_wrapper<T>> get_components() const; + +protected: + // NOTE: Script must have a constructor without arguments so the game + // programmer doesn't need to manually add `using Script::Script` to their + // concrete script class. + Script() = default; + //! Only \c BehaviorScript instantiates Script + friend class BehaviorScript; private: - friend class crepe::BehaviorScript; - BehaviorScript * parent = nullptr; + // These references are set by BehaviorScript immediately after calling the + // constructor of Script. + BehaviorScript * parent_ref = nullptr; + ComponentManager * component_manager_ref = nullptr; }; } // namespace crepe |