diff options
-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 | ||||
-rw-r--r-- | src/example/script.cpp | 11 |
7 files changed, 58 insertions, 2 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); +} + +} + diff --git a/src/example/script.cpp b/src/example/script.cpp index d0cc121..43f1c22 100644 --- a/src/example/script.cpp +++ b/src/example/script.cpp @@ -11,13 +11,17 @@ #include <crepe/api/Config.h> #include <crepe/api/GameObject.h> #include <crepe/api/Script.h> +#include <crepe/api/Transform.h> using namespace crepe; using namespace crepe::api; using namespace std; class MyScript : public Script { - void update() { dbg_log("MY SCRIPT UPDATE"); } + void update() { + Transform & test = get_component<Transform>(); + dbg_logf("Transform(%.2f, %.2f)", test.position.x, test.position.y); + } }; int main() { @@ -25,6 +29,11 @@ int main() { cfg.log.level = util::LogLevel::TRACE; auto obj = GameObject(0, "name", "tag", 0); + Point point = { + .x = 1.2, + .y = 3.4, + }; + obj.add_component<Transform>(point, 0, 0); obj.add_component<BehaviorScript>().set_script<MyScript>(); auto & sys = ScriptSystem::get_instance(); |