From 1f82ffa4d3ee8355215d43bf43edf8cecaca0d1d Mon Sep 17 00:00:00 2001 From: Loek Le Blansch Date: Wed, 16 Oct 2024 17:21:04 +0200 Subject: fix user script implementation --- src/crepe/CMakeLists.txt | 1 - src/crepe/ComponentManager.h | 2 +- src/crepe/ComponentManager.hpp | 5 +++-- src/crepe/GameObject.h | 2 +- src/crepe/GameObject.hpp | 4 ++-- src/crepe/Script.cpp | 7 ------- src/crepe/Script.h | 16 ---------------- src/crepe/ScriptSystem.cpp | 27 ++++++++++++++++++++++----- src/crepe/ScriptSystem.h | 12 +++++++++++- src/crepe/api/BehaviorScript.h | 24 ++++++++++++++++++------ src/crepe/api/BehaviorScript.hpp | 18 ++++++++++++++++++ src/crepe/api/CMakeLists.txt | 2 ++ src/crepe/api/Script.cpp | 5 +++++ src/crepe/api/Script.h | 21 +++++++++++++++++++++ src/example/script.cpp | 7 ++++--- 15 files changed, 108 insertions(+), 45 deletions(-) delete mode 100644 src/crepe/Script.cpp delete mode 100644 src/crepe/Script.h create mode 100644 src/crepe/api/BehaviorScript.hpp create mode 100644 src/crepe/api/Script.cpp create mode 100644 src/crepe/api/Script.h 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 - 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 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 #include "ComponentManager.h" -#include "api/BehaviorScript.h" namespace crepe { template -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::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)...); // store its unique_ptr in the vector<> components[type][id].push_back(unique_ptr(instance)); + + return *instance; } template 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 - 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 -void GameObject::add_component(Args &&... args) { +T & GameObject::add_component(Args &&... args) { auto & mgr = ComponentManager::get_instance(); - mgr.add_component(id, std::forward(args)...); + return mgr.add_component(id, std::forward(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/Script.h b/src/crepe/Script.h deleted file mode 100644 index ba4073a..0000000 --- a/src/crepe/Script.h +++ /dev/null @@ -1,16 +0,0 @@ -#pragma once - -namespace crepe { - -class Script { -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. -}; - -} - 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 +#include #include + +#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