diff options
author | Loek Le Blansch <loek@pipeframe.xyz> | 2024-10-25 20:46:32 +0200 |
---|---|---|
committer | Loek Le Blansch <loek@pipeframe.xyz> | 2024-10-25 20:46:32 +0200 |
commit | 195f7770c42aa1ed1052725dde26699d2ecc689f (patch) | |
tree | 0c0504f4750947ac158f500d6f4fff4ed83d63d8 /src/crepe | |
parent | e7377daf2fe3b759fbbf7e57f88c412c3b1cf011 (diff) |
implement get_component for scripts
Diffstat (limited to 'src/crepe')
-rw-r--r-- | src/crepe/api/BehaviorScript.h | 1 | ||||
-rw-r--r-- | src/crepe/api/BehaviorScript.hpp | 2 | ||||
-rw-r--r-- | src/crepe/api/CMakeLists.txt | 1 | ||||
-rw-r--r-- | src/crepe/api/Script.cpp | 1 | ||||
-rw-r--r-- | src/crepe/api/Script.h | 18 | ||||
-rw-r--r-- | src/crepe/api/Script.hpp | 26 |
6 files changed, 48 insertions, 1 deletions
diff --git a/src/crepe/api/BehaviorScript.h b/src/crepe/api/BehaviorScript.h index cb37a78..6133cc8 100644 --- a/src/crepe/api/BehaviorScript.h +++ b/src/crepe/api/BehaviorScript.h @@ -3,7 +3,6 @@ #include <memory> #include "../Component.h" -#include "Script.h" namespace crepe { class ScriptSystem; diff --git a/src/crepe/api/BehaviorScript.hpp b/src/crepe/api/BehaviorScript.hpp index 6fdf605..2a3502f 100644 --- a/src/crepe/api/BehaviorScript.hpp +++ b/src/crepe/api/BehaviorScript.hpp @@ -5,6 +5,7 @@ #include "../util/log.h" #include "BehaviorScript.h" +#include "Script.h" namespace crepe::api { @@ -13,6 +14,7 @@ BehaviorScript & BehaviorScript::set_script() { static_assert(std::is_base_of<Script, T>::value); dbg_trace(); Script * s = new T(); + s->parent = this; this->script = std::unique_ptr<Script>(s); return *this; } diff --git a/src/crepe/api/CMakeLists.txt b/src/crepe/api/CMakeLists.txt index f262019..0bb1263 100644 --- a/src/crepe/api/CMakeLists.txt +++ b/src/crepe/api/CMakeLists.txt @@ -18,6 +18,7 @@ target_sources(crepe PUBLIC FILE_SET HEADERS FILES BehaviorScript.h Config.h Script.h + Script.hpp GameObject.h GameObject.hpp Rigidbody.h diff --git a/src/crepe/api/Script.cpp b/src/crepe/api/Script.cpp index 5016ed0..0dfdd6f 100644 --- a/src/crepe/api/Script.cpp +++ b/src/crepe/api/Script.cpp @@ -1,3 +1,4 @@ #include "Script.h" using namespace crepe::api; + diff --git a/src/crepe/api/Script.h b/src/crepe/api/Script.h index 0036d1f..4950b5c 100644 --- a/src/crepe/api/Script.h +++ b/src/crepe/api/Script.h @@ -1,11 +1,15 @@ #pragma once +#include <vector> + namespace crepe { class ScriptSystem; } namespace crepe::api { +class BehaviorScript; + class Script { friend class crepe::ScriptSystem; @@ -16,6 +20,20 @@ protected: // implemented as member methods in derivative user script classes and // registered in init(), otherwise this class will balloon in size with each // added event. + +protected: + template<typename T> + T & get_component(); + + template<typename T> + std::vector<std::reference_wrapper<T>> get_components(); + +private: + friend class crepe::api::BehaviorScript; + BehaviorScript * parent = nullptr; }; } // namespace crepe::api + +#include "Script.hpp" + diff --git a/src/crepe/api/Script.hpp b/src/crepe/api/Script.hpp new file mode 100644 index 0000000..8ddd344 --- /dev/null +++ b/src/crepe/api/Script.hpp @@ -0,0 +1,26 @@ +#pragma once + +#include "../ComponentManager.h" + +#include "Script.h" +#include "BehaviorScript.h" + +namespace crepe::api { + +template<typename T> +T & Script::get_component() { + std::vector<std::reference_wrapper<T>> all_components = this->get_components<T>(); + if (all_components.size() < 1) + throw nullptr; // TODO + + return all_components.back().get(); +} + +template<typename T> +std::vector<std::reference_wrapper<T>> Script::get_components() { + ComponentManager & mgr = ComponentManager::get_instance(); + return mgr.get_components_by_id<T>(this->parent->game_object_id); +} + +} + |