diff options
-rw-r--r-- | src/crepe/api/BehaviorScript.hpp | 1 | ||||
-rw-r--r-- | src/crepe/api/CMakeLists.txt | 1 | ||||
-rw-r--r-- | src/crepe/api/Script.cpp | 11 | ||||
-rw-r--r-- | src/crepe/api/Script.h | 36 | ||||
-rw-r--r-- | src/crepe/api/Script.hpp | 9 |
5 files changed, 54 insertions, 4 deletions
diff --git a/src/crepe/api/BehaviorScript.hpp b/src/crepe/api/BehaviorScript.hpp index d80321d..dd1efd5 100644 --- a/src/crepe/api/BehaviorScript.hpp +++ b/src/crepe/api/BehaviorScript.hpp @@ -16,6 +16,7 @@ BehaviorScript & BehaviorScript::set_script() { Script * s = new T(); s->game_object_id = this->game_object_id; s->component_manager_ref = &this->component_manager; + s->event_manager_ref = &EventManager::get_instance(); this->script = std::unique_ptr<Script>(s); return *this; } diff --git a/src/crepe/api/CMakeLists.txt b/src/crepe/api/CMakeLists.txt index a185ca6..a63c5ff 100644 --- a/src/crepe/api/CMakeLists.txt +++ b/src/crepe/api/CMakeLists.txt @@ -23,6 +23,7 @@ target_sources(crepe PUBLIC LoopManager.cpp LoopTimer.cpp EventHandler.cpp + Script.cpp ) target_sources(crepe PUBLIC FILE_SET HEADERS FILES diff --git a/src/crepe/api/Script.cpp b/src/crepe/api/Script.cpp new file mode 100644 index 0000000..9ebb074 --- /dev/null +++ b/src/crepe/api/Script.cpp @@ -0,0 +1,11 @@ +#include "Script.h" + +using namespace crepe; + +Script::~Script() { + EventManager & evmgr = *this->event_manager_ref; + for (auto id : this->listeners) { + evmgr.unsubscribe(id); + } +} + diff --git a/src/crepe/api/Script.h b/src/crepe/api/Script.h index ddb499c..c9eb211 100644 --- a/src/crepe/api/Script.h +++ b/src/crepe/api/Script.h @@ -4,6 +4,8 @@ #include "../types.h" +#include "EventManager.h" + namespace crepe { class ScriptSystem; @@ -85,6 +87,14 @@ protected: template <typename... Args> void logf(Args &&... args); + /** + * \brief Subscribe to an event + * + * \see EventManager::subscribe + */ + template <typename EventType> + void subscribe(const EventHandler<EventType> & callback, event_channel_t channel = EventManager::CHANNEL_ALL); + //! \} protected: @@ -93,17 +103,37 @@ protected: Script() = default; //! Only \c BehaviorScript instantiates Script friend class BehaviorScript; +public: + // std::unique_ptr destroys script + virtual ~Script(); + + Script(const Script &) = delete; + Script(Script &&) = delete; + Script & operator=(const Script &) = delete; + Script & operator=(Script &&) = delete; private: - // These references are set by BehaviorScript immediately after calling the constructor of - // Script. + /** + * \name Late references + * + * These references are set by BehaviorScript immediately after calling the constructor of + * Script. + * + * \{ + */ + //! Game object ID of game object parent BehaviorScript is attached to game_object_id_t game_object_id = -1; + //! Reference to component manager instance ComponentManager * component_manager_ref = nullptr; - // TODO: use OptionalRef instead of pointer + //! Reference to event manager instance + EventManager * event_manager_ref = nullptr; + //! \} private: //! Flag to indicate if \c init() has been called already bool initialized = false; + //! List of subscribed events + std::vector<subscription_t> listeners; }; } // namespace crepe diff --git a/src/crepe/api/Script.hpp b/src/crepe/api/Script.hpp index 4593d69..42c8f0b 100644 --- a/src/crepe/api/Script.hpp +++ b/src/crepe/api/Script.hpp @@ -20,7 +20,7 @@ T & Script::get_component() const { template <typename T> std::vector<std::reference_wrapper<T>> Script::get_components() const { - auto & mgr = *this->component_manager_ref; + ComponentManager & mgr = *this->component_manager_ref; return mgr.get_components_by_id<T>(this->game_object_id); } @@ -30,4 +30,11 @@ void Script::logf(Args &&... args) { Log::logf(std::forward<Args>(args)...); } +template <typename EventType> +void Script::subscribe(const EventHandler<EventType> & callback, event_channel_t channel) { + EventManager & mgr = *this->event_manager_ref; + subscription_t listener = mgr.subscribe<EventType>(callback, channel); + this->listeners.push_back(listener); +} + } // namespace crepe |