diff options
author | heavydemon21 <nielsstunnebrink1@gmail.com> | 2024-11-18 14:27:58 +0100 |
---|---|---|
committer | heavydemon21 <nielsstunnebrink1@gmail.com> | 2024-11-18 14:27:58 +0100 |
commit | 03e493012edc106b00bc5676c1830ce2085a7ff9 (patch) | |
tree | ff06d4314552e82f98d08c52d7a15edb3a74436e /src/crepe/api/Script.h | |
parent | 21f5b66d0bcc13d903800adf3bb614e380ff8591 (diff) | |
parent | 121b64b1cb6cfead5814070c8b0185d3d7308095 (diff) |
Merge remote-tracking branch 'origin/master' into niels/sorting_rendering
Diffstat (limited to 'src/crepe/api/Script.h')
-rw-r--r-- | src/crepe/api/Script.h | 76 |
1 files changed, 63 insertions, 13 deletions
diff --git a/src/crepe/api/Script.h b/src/crepe/api/Script.h index 49e625f..2b70379 100644 --- a/src/crepe/api/Script.h +++ b/src/crepe/api/Script.h @@ -2,35 +2,85 @@ #include <vector> -namespace crepe { -class ScriptSystem; -} +#include "../types.h" 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. + * + * \note Additional *events* (like Unity's OnDisable and OnEnable) should be implemented as + * member or lambda methods in derivative user script classes and registered in \c init(). + */ class Script { - 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 - // registered in init(), otherwise this class will balloon in size with each - // added event. + //! ScriptSystem calls \c init() and \c update() + friend class crepe::ScriptSystem; 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; + // TODO: make get_component calls for component types that can have more than 1 instance + // cause compile-time errors + /** + * \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: + // These references are set by BehaviorScript immediately after calling the constructor of + // Script. + game_object_id_t game_object_id = -1; + ComponentManager * component_manager_ref = nullptr; + // TODO: use OptionalRef instead of pointer private: - friend class crepe::BehaviorScript; - BehaviorScript * parent = nullptr; + //! Flag to indicate if \c init() has been called already + bool initialized = false; }; } // namespace crepe |