diff options
Diffstat (limited to 'src/crepe/api')
-rw-r--r-- | src/crepe/api/BehaviorScript.h | 8 | ||||
-rw-r--r-- | src/crepe/api/Config.h | 3 | ||||
-rw-r--r-- | src/crepe/api/Script.h | 40 | ||||
-rw-r--r-- | src/crepe/api/Script.hpp | 8 | ||||
-rw-r--r-- | src/crepe/api/Texture.cpp | 2 |
5 files changed, 53 insertions, 8 deletions
diff --git a/src/crepe/api/BehaviorScript.h b/src/crepe/api/BehaviorScript.h index c20842d..4160a72 100644 --- a/src/crepe/api/BehaviorScript.h +++ b/src/crepe/api/BehaviorScript.h @@ -10,6 +10,13 @@ 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; @@ -24,6 +31,7 @@ public: protected: friend class crepe::ScriptSystem; + //! Script instance std::unique_ptr<Script> script = nullptr; }; diff --git a/src/crepe/api/Config.h b/src/crepe/api/Config.h index 8fd381e..dad7e08 100644 --- a/src/crepe/api/Config.h +++ b/src/crepe/api/Config.h @@ -7,7 +7,6 @@ namespace crepe { class Config { private: Config() = default; - public: ~Config() = default; @@ -26,7 +25,7 @@ public: /** * \brief Log level * - * Only messages with equal or higher priority than this value will be + * Only messages with equal or higher severity than this value will be * logged. */ Log::Level level = Log::Level::INFO; diff --git a/src/crepe/api/Script.h b/src/crepe/api/Script.h index ed247ad..e6fbb5c 100644 --- a/src/crepe/api/Script.h +++ b/src/crepe/api/Script.h @@ -7,11 +7,30 @@ namespace crepe { class ScriptSystem; class BehaviorScript; +/** + * \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 { 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 @@ -19,11 +38,28 @@ 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; private: friend class crepe::BehaviorScript; diff --git a/src/crepe/api/Script.hpp b/src/crepe/api/Script.hpp index d96c0e8..afe653f 100644 --- a/src/crepe/api/Script.hpp +++ b/src/crepe/api/Script.hpp @@ -1,6 +1,7 @@ #pragma once #include "../ComponentManager.h" +#include "../Exception.h" #include "BehaviorScript.h" #include "Script.h" @@ -8,16 +9,17 @@ namespace crepe { template <typename T> -T & Script::get_component() { +T & Script::get_component() const { std::vector<std::reference_wrapper<T>> all_components = this->get_components<T>(); - if (all_components.size() < 1) throw nullptr; // TODO + if (all_components.size() < 1) + throw Exception("Script: no component found with type = %s", typeid(T).name()); return all_components.back().get(); } template <typename T> -std::vector<std::reference_wrapper<T>> Script::get_components() { +std::vector<std::reference_wrapper<T>> Script::get_components() const { ComponentManager & mgr = ComponentManager::get_instance(); return mgr.get_components_by_id<T>(this->parent->game_object_id); } diff --git a/src/crepe/api/Texture.cpp b/src/crepe/api/Texture.cpp index 8ddeac5..de0d0ea 100644 --- a/src/crepe/api/Texture.cpp +++ b/src/crepe/api/Texture.cpp @@ -26,7 +26,7 @@ Texture::~Texture() { void Texture::load(unique_ptr<Asset> res) { SDLContext & ctx = SDLContext::get_instance(); - this->texture = std::move(ctx.texture_from_path(res->canonical())); + this->texture = std::move(ctx.texture_from_path(res->get_canonical())); } int Texture::get_width() const { |