diff options
author | Loek Le Blansch <loek@pipeframe.xyz> | 2024-11-21 10:52:22 +0100 |
---|---|---|
committer | Loek Le Blansch <loek@pipeframe.xyz> | 2024-11-21 10:52:22 +0100 |
commit | db66bb4babb19e8a86d5c61281c8b94469729d03 (patch) | |
tree | 83c25f47797c7bd8e06d15412a9859599788569a | |
parent | 5134bebc19c46e4e07a5ec3af1d3f3d2d17a86dd (diff) |
use OptionalRef instead of pointer referencesloek/scripts
-rw-r--r-- | src/crepe/api/BehaviorScript.hpp | 8 | ||||
-rw-r--r-- | src/crepe/api/Script.cpp | 4 | ||||
-rw-r--r-- | src/crepe/api/Script.h | 11 | ||||
-rw-r--r-- | src/crepe/api/Script.hpp | 8 | ||||
-rw-r--r-- | src/crepe/system/ScriptSystem.cpp | 30 | ||||
-rw-r--r-- | src/crepe/system/ScriptSystem.h | 11 | ||||
-rw-r--r-- | src/test/ScriptTest.cpp | 109 |
7 files changed, 81 insertions, 100 deletions
diff --git a/src/crepe/api/BehaviorScript.hpp b/src/crepe/api/BehaviorScript.hpp index 5b5a418..bd59337 100644 --- a/src/crepe/api/BehaviorScript.hpp +++ b/src/crepe/api/BehaviorScript.hpp @@ -15,10 +15,10 @@ BehaviorScript & BehaviorScript::set_script(Args &&... args) { static_assert(std::is_base_of<Script, T>::value); Script * s = new T(std::forward<Args>(args)...); - s->game_object_id_ref = &this->game_object_id; - s->active_ref = &this->active; - s->component_manager_ref = &this->component_manager; - s->event_manager_ref = &EventManager::get_instance(); + s->game_object_id = this->game_object_id; + s->active = this->active; + s->component_manager = this->component_manager; + s->event_manager = EventManager::get_instance(); this->script = std::unique_ptr<Script>(s); return *this; diff --git a/src/crepe/api/Script.cpp b/src/crepe/api/Script.cpp index 0e73848..8bb3ef6 100644 --- a/src/crepe/api/Script.cpp +++ b/src/crepe/api/Script.cpp @@ -3,7 +3,7 @@ using namespace crepe; Script::~Script() { - EventManager & evmgr = *this->event_manager_ref; + EventManager & evmgr = this->event_manager; for (auto id : this->listeners) { evmgr.unsubscribe(id); } @@ -11,6 +11,6 @@ Script::~Script() { template <> void Script::subscribe(const EventHandler<CollisionEvent> & callback) { - const game_object_id_t & game_object_id = *this->game_object_id_ref; + const game_object_id_t & game_object_id = this->game_object_id; this->subscribe_internal(callback, game_object_id); } diff --git a/src/crepe/api/Script.h b/src/crepe/api/Script.h index 43efd15..6418b04 100644 --- a/src/crepe/api/Script.h +++ b/src/crepe/api/Script.h @@ -2,6 +2,7 @@ #include <vector> +#include "../util/OptionalRef.h" #include "../types.h" #include "EventManager.h" @@ -150,18 +151,16 @@ private: * implement a non-default constructor (e.g. for passing references to their own concrete * Script classes). * - * \todo These should be converted to OptionalRef<> once `loek/util` is merged - * * \{ */ //! Game object ID of game object parent BehaviorScript is attached to - const game_object_id_t * game_object_id_ref = nullptr; + OptionalRef<const game_object_id_t> game_object_id; //! Reference to parent component - bool * active_ref = nullptr; + OptionalRef<bool> active; //! Reference to component manager instance - ComponentManager * component_manager_ref = nullptr; + OptionalRef<ComponentManager> component_manager; //! Reference to event manager instance - EventManager * event_manager_ref = nullptr; + OptionalRef<EventManager> event_manager; //! \} private: diff --git a/src/crepe/api/Script.hpp b/src/crepe/api/Script.hpp index e94278d..a2463bf 100644 --- a/src/crepe/api/Script.hpp +++ b/src/crepe/api/Script.hpp @@ -20,9 +20,9 @@ T & Script::get_component() const { template <typename T> RefVector<T> Script::get_components() const { - ComponentManager & mgr = *this->component_manager_ref; + ComponentManager & mgr = this->component_manager; - return mgr.get_components_by_id<T>(*this->game_object_id_ref); + return mgr.get_components_by_id<T>(this->game_object_id); } template <typename... Args> @@ -33,10 +33,10 @@ void Script::logf(Args &&... args) { template <typename EventType> void Script::subscribe_internal(const EventHandler<EventType> & callback, event_channel_t channel) { - EventManager & mgr = *this->event_manager_ref; + EventManager & mgr = this->event_manager; subscription_t listener = mgr.subscribe<EventType>( [this, callback](const EventType & data) -> bool { - bool & active = *this->active_ref; + bool & active = this->active; if (!active) return false; return callback(data); }, diff --git a/src/crepe/system/ScriptSystem.cpp b/src/crepe/system/ScriptSystem.cpp index c33309c..d54bbc9 100644 --- a/src/crepe/system/ScriptSystem.cpp +++ b/src/crepe/system/ScriptSystem.cpp @@ -1,5 +1,3 @@ -#include <functional> - #include "../ComponentManager.h" #include "../api/BehaviorScript.h" #include "../api/Script.h" @@ -12,30 +10,20 @@ using namespace crepe; void ScriptSystem::update() { dbg_trace(); - RefVector<Script> scripts = this->get_scripts(); - - for (auto & script_ref : scripts) { - Script & script = script_ref.get(); - if (!script.initialized) { - script.init(); - script.initialized = true; - } - script.update(); - } -} - -RefVector<Script> ScriptSystem::get_scripts() const { - RefVector<Script> scripts = {}; ComponentManager & mgr = this->component_manager; RefVector<BehaviorScript> behavior_scripts = mgr.get_components_by_type<BehaviorScript>(); - for (auto behavior_script_ref : behavior_scripts) { - BehaviorScript & behavior_script = behavior_script_ref.get(); + for (BehaviorScript & behavior_script : behavior_scripts) { if (!behavior_script.active) continue; + Script * script = behavior_script.script.get(); if (script == nullptr) continue; - scripts.push_back(*script); - } - return scripts; + if (!script->initialized) { + script->init(); + script->initialized = true; + } + script->update(); + } } + diff --git a/src/crepe/system/ScriptSystem.h b/src/crepe/system/ScriptSystem.h index 32e1fcd..936e9ca 100644 --- a/src/crepe/system/ScriptSystem.h +++ b/src/crepe/system/ScriptSystem.h @@ -2,8 +2,6 @@ #include "System.h" -#include "../types.h" - namespace crepe { class Script; @@ -25,15 +23,6 @@ public: * the \c BehaviorScript instance. */ void update() override; - -private: - /** - * \brief Aggregate all active \c BehaviorScript components and return a list - * of references to their \c Script instances (utility) - * - * \returns List of active \c Script instances - */ - RefVector<Script> get_scripts() const; }; } // namespace crepe diff --git a/src/test/ScriptTest.cpp b/src/test/ScriptTest.cpp index c35c3e2..73009a7 100644 --- a/src/test/ScriptTest.cpp +++ b/src/test/ScriptTest.cpp @@ -23,7 +23,7 @@ class ScriptTest : public Test { public: ComponentManager component_manager{}; ScriptSystem system{component_manager}; - EventManager & evmgr = EventManager::get_instance(); + EventManager & event_manager = EventManager::get_instance(); class MyScript : public Script { // NOTE: default (private) visibility of init and update shouldn't cause @@ -47,79 +47,84 @@ public: unsigned event_count = 0; }; - BehaviorScript * behaviorscript_ref = nullptr; - MyScript * script_ref = nullptr; + OptionalRef<BehaviorScript> behaviorscript; + OptionalRef<MyScript> script; void SetUp() override { auto & mgr = this->component_manager; GameObject entity = mgr.new_object("name"); BehaviorScript & component = entity.add_component<BehaviorScript>(); - this->behaviorscript_ref = &component; - EXPECT_EQ(this->behaviorscript_ref->script.get(), nullptr); + this->behaviorscript = component; + ASSERT_TRUE(this->behaviorscript); + EXPECT_EQ(component.script.get(), nullptr); component.set_script<MyScript>(); - ASSERT_NE(this->behaviorscript_ref->script.get(), nullptr); + ASSERT_NE(component.script.get(), nullptr); - this->script_ref = (MyScript *) this->behaviorscript_ref->script.get(); - ASSERT_NE(this->script_ref, nullptr); + this->script = *(MyScript *) component.script.get(); + ASSERT_TRUE(this->script); // sanity - ASSERT_EQ(script_ref->init_count, 0); - ASSERT_EQ(script_ref->update_count, 0); - ASSERT_EQ(script_ref->event_count, 0); + MyScript & script = this->script; + ASSERT_EQ(script.init_count, 0); + ASSERT_EQ(script.update_count, 0); + ASSERT_EQ(script.event_count, 0); } }; TEST_F(ScriptTest, Default) { - EXPECT_EQ(0, this->script_ref->init_count); - EXPECT_EQ(0, this->script_ref->update_count); - EXPECT_EQ(0, this->script_ref->event_count); + MyScript & script = this->script; + EXPECT_EQ(0, script.init_count); + EXPECT_EQ(0, script.update_count); + EXPECT_EQ(0, script.event_count); } TEST_F(ScriptTest, UpdateOnce) { - this->system.update(); - EXPECT_EQ(1, this->script_ref->init_count); - EXPECT_EQ(1, this->script_ref->update_count); - EXPECT_EQ(0, this->script_ref->event_count); + MyScript & script = this->script; + + system.update(); + EXPECT_EQ(1, script.init_count); + EXPECT_EQ(1, script.update_count); + EXPECT_EQ(0, script.event_count); } TEST_F(ScriptTest, UpdateInactive) { - this->behaviorscript_ref->active = false; - this->system.update(); - EXPECT_EQ(0, this->script_ref->init_count); - EXPECT_EQ(0, this->script_ref->update_count); - EXPECT_EQ(0, this->script_ref->event_count); - - this->behaviorscript_ref->active = true; - this->system.update(); - EXPECT_EQ(1, this->script_ref->init_count); - EXPECT_EQ(1, this->script_ref->update_count); - EXPECT_EQ(0, this->script_ref->event_count); + BehaviorScript & behaviorscript = this->behaviorscript; + MyScript & script = this->script; + + behaviorscript.active = false; + system.update(); + EXPECT_EQ(0, script.init_count); + EXPECT_EQ(0, script.update_count); + EXPECT_EQ(0, script.event_count); + + behaviorscript.active = true; + system.update(); + EXPECT_EQ(1, script.init_count); + EXPECT_EQ(1, script.update_count); + EXPECT_EQ(0, script.event_count); } TEST_F(ScriptTest, EventInactive) { - this->system.update(); - this->behaviorscript_ref->active = false; - EXPECT_EQ(1, this->script_ref->init_count); - EXPECT_EQ(1, this->script_ref->update_count); - EXPECT_EQ(0, this->script_ref->event_count); - - this->evmgr.trigger_event<MyEvent>(); - EXPECT_EQ(1, this->script_ref->init_count); - EXPECT_EQ(1, this->script_ref->update_count); - EXPECT_EQ(0, this->script_ref->event_count); - - this->behaviorscript_ref->active = true; - this->evmgr.trigger_event<MyEvent>(); - EXPECT_EQ(1, this->script_ref->init_count); - EXPECT_EQ(1, this->script_ref->update_count); - EXPECT_EQ(1, this->script_ref->event_count); + BehaviorScript & behaviorscript = this->behaviorscript; + MyScript & script = this->script; + EventManager & evmgr = this->event_manager; + + system.update(); + behaviorscript.active = false; + EXPECT_EQ(1, script.init_count); + EXPECT_EQ(1, script.update_count); + EXPECT_EQ(0, script.event_count); + + evmgr.trigger_event<MyEvent>(); + EXPECT_EQ(1, script.init_count); + EXPECT_EQ(1, script.update_count); + EXPECT_EQ(0, script.event_count); + + behaviorscript.active = true; + evmgr.trigger_event<MyEvent>(); + EXPECT_EQ(1, script.init_count); + EXPECT_EQ(1, script.update_count); + EXPECT_EQ(1, script.event_count); } -TEST_F(ScriptTest, ListScripts) { - size_t script_count = 0; - for (auto & _ : this->system.get_scripts()) { - script_count++; - } - ASSERT_EQ(1, script_count); -} |