diff options
-rw-r--r-- | src/crepe/CMakeLists.txt | 1 | ||||
-rw-r--r-- | src/crepe/ComponentManager.h | 2 | ||||
-rw-r--r-- | src/crepe/ComponentManager.hpp | 5 | ||||
-rw-r--r-- | src/crepe/GameObject.h | 2 | ||||
-rw-r--r-- | src/crepe/GameObject.hpp | 4 | ||||
-rw-r--r-- | src/crepe/Script.cpp | 7 | ||||
-rw-r--r-- | src/crepe/ScriptSystem.cpp | 27 | ||||
-rw-r--r-- | src/crepe/ScriptSystem.h | 12 | ||||
-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 (renamed from src/crepe/Script.h) | 9 | ||||
-rw-r--r-- | src/example/script.cpp | 7 |
14 files changed, 94 insertions, 31 deletions
diff --git a/src/crepe/CMakeLists.txt b/src/crepe/CMakeLists.txt index d85aef0..8323490 100644 --- a/src/crepe/CMakeLists.txt +++ b/src/crepe/CMakeLists.txt @@ -9,7 +9,6 @@ target_sources(crepe PUBLIC Rigidbody.cpp Sprite.cpp ScriptSystem.cpp - Script.cpp ) target_sources(crepe PUBLIC FILE_SET HEADERS FILES diff --git a/src/crepe/ComponentManager.h b/src/crepe/ComponentManager.h index eab9b45..38f32e4 100644 --- a/src/crepe/ComponentManager.h +++ b/src/crepe/ComponentManager.h @@ -23,7 +23,7 @@ public: public: //! Add a component of a specific type template <typename T, typename... Args> - void add_component(uint32_t id, Args &&... args); + T & add_component(uint32_t id, Args &&... args); //! Deletes all components of a specific type and id template <typename T> void delete_components_by_id(uint32_t id); diff --git a/src/crepe/ComponentManager.hpp b/src/crepe/ComponentManager.hpp index c872594..e0242a2 100644 --- a/src/crepe/ComponentManager.hpp +++ b/src/crepe/ComponentManager.hpp @@ -3,12 +3,11 @@ #include <type_traits> #include "ComponentManager.h" -#include "api/BehaviorScript.h" namespace crepe { template <class T, typename... Args> -void ComponentManager::add_component(uint32_t id, Args &&... args) { +T & ComponentManager::add_component(uint32_t id, Args &&... args) { using namespace std; static_assert(is_base_of<Component, T>::value, "add_component must recieve a derivative class of Component"); @@ -33,6 +32,8 @@ void ComponentManager::add_component(uint32_t id, Args &&... args) { T * instance = new T(forward<Args>(args)...); // store its unique_ptr in the vector<> components[type][id].push_back(unique_ptr<T>(instance)); + + return *instance; } template <typename T> diff --git a/src/crepe/GameObject.h b/src/crepe/GameObject.h index 3588d9a..b5d6399 100644 --- a/src/crepe/GameObject.h +++ b/src/crepe/GameObject.h @@ -10,7 +10,7 @@ public: GameObject(uint32_t id, std::string name, std::string tag, int layer); template <typename T, typename... Args> - void add_component(Args &&... args); + T & add_component(Args &&... args); uint32_t id; std::string name; diff --git a/src/crepe/GameObject.hpp b/src/crepe/GameObject.hpp index 5966fbf..8cd1abe 100644 --- a/src/crepe/GameObject.hpp +++ b/src/crepe/GameObject.hpp @@ -7,9 +7,9 @@ namespace crepe { template <typename T, typename... Args> -void GameObject::add_component(Args &&... args) { +T & GameObject::add_component(Args &&... args) { auto & mgr = ComponentManager::get_instance(); - mgr.add_component<T>(id, std::forward<Args>(args)...); + return mgr.add_component<T>(id, std::forward<Args>(args)...); } } // namespace crepe diff --git a/src/crepe/Script.cpp b/src/crepe/Script.cpp deleted file mode 100644 index 42e3666..0000000 --- a/src/crepe/Script.cpp +++ /dev/null @@ -1,7 +0,0 @@ -#include "Script.h" - -using namespace crepe; - -void Script::init() { } -void Script::update() { } - diff --git a/src/crepe/ScriptSystem.cpp b/src/crepe/ScriptSystem.cpp index e40909e..0537c16 100644 --- a/src/crepe/ScriptSystem.cpp +++ b/src/crepe/ScriptSystem.cpp @@ -1,12 +1,16 @@ +#include <forward_list> +#include <functional> #include <vector> + +#include "ScriptSystem.h" #include "ComponentManager.h" #include "api/BehaviorScript.h" -#include "util/fmt.h" +#include "api/Script.h" #include "util/log.h" -#include "ScriptSystem.h" - +using namespace std; using namespace crepe; +using namespace crepe::api; ScriptSystem::ScriptSystem() { dbg_trace(); @@ -24,10 +28,23 @@ void ScriptSystem::update() { using namespace std; dbg_trace(); + forward_list<Script *> scripts = this->get_scripts(); + for (Script * script : scripts) + script->update(); +} + +forward_list<Script *> ScriptSystem::get_scripts() { + forward_list<Script *> scripts = {}; ComponentManager & mgr = ComponentManager::get_instance(); - vector<reference_wrapper<api::BehaviorScript>> scripts = mgr.get_components_by_type<api::BehaviorScript>(); + vector<reference_wrapper<BehaviorScript>> behavior_scripts = mgr.get_components_by_type<BehaviorScript>(); - dbg_logf("script count: %lu", scripts.size()); + for (auto behavior_script_ref : behavior_scripts) { + BehaviorScript & behavior_script = behavior_script_ref.get(); + Script * script = behavior_script.script; + if (script == nullptr) continue; + scripts.push_front(script); + } + return scripts; } diff --git a/src/crepe/ScriptSystem.h b/src/crepe/ScriptSystem.h index e1ed290..e0b2a65 100644 --- a/src/crepe/ScriptSystem.h +++ b/src/crepe/ScriptSystem.h @@ -1,17 +1,27 @@ #pragma once +#include <forward_list> + #include "System.h" +namespace crepe::api { +class Script; +} + namespace crepe { class ScriptSystem : public System { public: static ScriptSystem & get_instance(); - virtual void update(); + void update(); private: ScriptSystem(); ~ScriptSystem(); + +private: + std::forward_list<api::Script *> get_scripts(); + }; } 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/Script.h b/src/crepe/api/Script.h index ba4073a..b1e6a95 100644 --- a/src/crepe/Script.h +++ b/src/crepe/api/Script.h @@ -1,11 +1,16 @@ #pragma once namespace crepe { +class ScriptSystem; +} + +namespace crepe::api { class Script { + friend class crepe::ScriptSystem; protected: - virtual void init(); - virtual void update(); + 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 diff --git a/src/example/script.cpp b/src/example/script.cpp index b8b3c8d..95c4950 100644 --- a/src/example/script.cpp +++ b/src/example/script.cpp @@ -8,15 +8,16 @@ #include <crepe/ComponentManager.h> #include <crepe/GameObject.h> +#include <crepe/api/Script.h> #include <crepe/api/BehaviorScript.h> using namespace crepe; using namespace crepe::api; using namespace std; -class MyScript : public BehaviorScript { +class MyScript : public Script { void update() { - dbg_trace(); + dbg_log("MY SCRIPT UPDATE"); } }; @@ -24,7 +25,7 @@ int main() { dbg_trace(); auto obj = GameObject(0, "name", "tag", 0); - obj.add_component<MyScript>(); + obj.add_component<BehaviorScript>().set_script<MyScript>(); auto & sys = ScriptSystem::get_instance(); sys.update(); // -> MyScript::update |