diff options
author | Loek Le Blansch <loek@pipeframe.xyz> | 2024-10-16 17:21:04 +0200 |
---|---|---|
committer | Loek Le Blansch <loek@pipeframe.xyz> | 2024-10-16 17:21:04 +0200 |
commit | 1f82ffa4d3ee8355215d43bf43edf8cecaca0d1d (patch) | |
tree | 218b7f87df6edd5a5e2a13ca5bbcca43b7a717a3 /src/crepe/api | |
parent | f9aa198eef7b85eeba3bac4c4fe2d4578b836bbf (diff) |
fix user script implementation
Diffstat (limited to 'src/crepe/api')
-rw-r--r-- | src/crepe/api/BehaviorScript.h | 24 | ||||
-rw-r--r-- | src/crepe/api/BehaviorScript.hpp | 18 | ||||
-rw-r--r-- | src/crepe/api/CMakeLists.txt | 2 | ||||
-rw-r--r-- | src/crepe/api/Script.cpp | 5 | ||||
-rw-r--r-- | src/crepe/api/Script.h | 21 |
5 files changed, 64 insertions, 6 deletions
diff --git a/src/crepe/api/BehaviorScript.h b/src/crepe/api/BehaviorScript.h index dd04dc0..052d764 100644 --- a/src/crepe/api/BehaviorScript.h +++ b/src/crepe/api/BehaviorScript.h @@ -1,21 +1,33 @@ #pragma once -#include "../Script.h" #include "../Component.h" +namespace crepe { +class ScriptSystem; +class ComponentManager; +} + namespace crepe::api { -class BehaviorScript : public Script, public Component { +class Script; + +class BehaviorScript : public Component { protected: - // only allow ComponentManager to instantiate scripts - friend class ComponentManager; + friend class crepe::ComponentManager; BehaviorScript(); public: - // but allow uniqe_ptr to call the destructor (THIS IS VERY IMPORTANT) virtual ~BehaviorScript() = default; - static BehaviorScript * component; +public: + template<class T> + BehaviorScript & set_script(); + +protected: + friend class crepe::ScriptSystem; + Script * script = nullptr; }; } +#include "BehaviorScript.hpp" + diff --git a/src/crepe/api/BehaviorScript.hpp b/src/crepe/api/BehaviorScript.hpp new file mode 100644 index 0000000..0401c0d --- /dev/null +++ b/src/crepe/api/BehaviorScript.hpp @@ -0,0 +1,18 @@ +#pragma once + +#include <type_traits> + +#include "../util/log.h" +#include "BehaviorScript.h" + +namespace crepe::api { + +template<class T> +BehaviorScript & BehaviorScript::set_script() { + static_assert(std::is_base_of<Script, T>::value); + dbg_trace(); + this->script = new T(); + return *this; +} + +} diff --git a/src/crepe/api/CMakeLists.txt b/src/crepe/api/CMakeLists.txt index 86623de..6b337be 100644 --- a/src/crepe/api/CMakeLists.txt +++ b/src/crepe/api/CMakeLists.txt @@ -1,10 +1,12 @@ target_sources(crepe PUBLIC # AudioSource.cpp BehaviorScript.cpp + Script.cpp ) target_sources(crepe PUBLIC FILE_SET HEADERS FILES # AudioSource.h BehaviorScript.h + Script.h ) diff --git a/src/crepe/api/Script.cpp b/src/crepe/api/Script.cpp new file mode 100644 index 0000000..6259574 --- /dev/null +++ b/src/crepe/api/Script.cpp @@ -0,0 +1,5 @@ +#include "Script.h" + +using namespace crepe::api; + + diff --git a/src/crepe/api/Script.h b/src/crepe/api/Script.h new file mode 100644 index 0000000..b1e6a95 --- /dev/null +++ b/src/crepe/api/Script.h @@ -0,0 +1,21 @@ +#pragma once + +namespace crepe { +class ScriptSystem; +} + +namespace crepe::api { + +class Script { + friend class crepe::ScriptSystem; +protected: + virtual void init() {} + 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. +}; + +} + |