From 502fb8e8d1dcfe10f55fdef2cdfb71afec806204 Mon Sep 17 00:00:00 2001 From: Loek Le Blansch Date: Thu, 21 Nov 2024 09:43:13 +0100 Subject: pull script/event changes from `loek/collision-system` --- src/test/ScriptTest.cpp | 60 +++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 58 insertions(+), 2 deletions(-) (limited to 'src/test') diff --git a/src/test/ScriptTest.cpp b/src/test/ScriptTest.cpp index 19fef6d..875c422 100644 --- a/src/test/ScriptTest.cpp +++ b/src/test/ScriptTest.cpp @@ -9,26 +9,44 @@ #include #include #include +#include +#include #include using namespace std; using namespace crepe; using namespace testing; +class MyEvent : public Event { }; + class ScriptTest : public Test { public: ComponentManager component_manager{}; ScriptSystem system{component_manager}; + EventManager & evmgr = EventManager::get_instance(); class MyScript : public Script { // NOTE: default (private) visibility of init and update shouldn't cause // issues! - void init() { this->init_count++; } - void update() { this->update_count++; } + void init() { + this->init_count++; + + subscribe([this](const MyEvent &) { + this->event_count++; + return true; + }); + + // init should never be called more than once + EXPECT_LE(this->init_count, 1); + } + void update() { + this->update_count++; + } public: unsigned init_count = 0; unsigned update_count = 0; + unsigned event_count = 0; }; BehaviorScript * behaviorscript_ref = nullptr; @@ -46,21 +64,58 @@ public: this->script_ref = (MyScript *) this->behaviorscript_ref->script.get(); ASSERT_NE(this->script_ref, nullptr); + + // sanity + ASSERT_EQ(script_ref->init_count, 0); + ASSERT_EQ(script_ref->update_count, 0); + ASSERT_EQ(script_ref->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); } 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); +} + +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); +} +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(); + 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(); + EXPECT_EQ(1, this->script_ref->init_count); + EXPECT_EQ(1, this->script_ref->update_count); + EXPECT_EQ(1, this->script_ref->event_count); } TEST_F(ScriptTest, ListScripts) { @@ -70,3 +125,4 @@ TEST_F(ScriptTest, ListScripts) { } ASSERT_EQ(1, script_count); } + -- cgit v1.2.3 From 70b1bf50de703330436f2ae9cb103fe33cbb567e Mon Sep 17 00:00:00 2001 From: Loek Le Blansch Date: Thu, 21 Nov 2024 10:00:35 +0100 Subject: `make format` --- src/crepe/api/Event.h | 4 ++-- src/crepe/api/Script.cpp | 1 - src/crepe/api/Script.h | 4 +++- src/crepe/api/Script.hpp | 15 +++++++++------ src/test/SceneManagerTest.cpp | 4 ++-- src/test/ScriptTest.cpp | 11 ++++------- 6 files changed, 20 insertions(+), 19 deletions(-) (limited to 'src/test') diff --git a/src/crepe/api/Event.h b/src/crepe/api/Event.h index ab4be2b..b267e3e 100644 --- a/src/crepe/api/Event.h +++ b/src/crepe/api/Event.h @@ -93,7 +93,7 @@ public: /** * \brief Event triggered during a collision between objects. */ -class CollisionEvent : public Event { }; +class CollisionEvent : public Event {}; /** * \brief Event triggered when text is submitted, e.g., from a text input. @@ -109,4 +109,4 @@ public: */ class ShutDownEvent : public Event {}; -} +} // namespace crepe diff --git a/src/crepe/api/Script.cpp b/src/crepe/api/Script.cpp index d3fae07..0e73848 100644 --- a/src/crepe/api/Script.cpp +++ b/src/crepe/api/Script.cpp @@ -14,4 +14,3 @@ void Script::subscribe(const EventHandler & callback) { const game_object_id_t & game_object_id = *this->game_object_id_ref; this->subscribe_internal(callback, game_object_id); } - diff --git a/src/crepe/api/Script.h b/src/crepe/api/Script.h index 373b253..8356fd2 100644 --- a/src/crepe/api/Script.h +++ b/src/crepe/api/Script.h @@ -127,6 +127,7 @@ protected: Script() = default; //! Only \c BehaviorScript instantiates Script friend class BehaviorScript; + public: // std::unique_ptr destroys script virtual ~Script(); @@ -180,7 +181,8 @@ private: template <> void Script::subscribe(const EventHandler & callback); template <> -void Script::subscribe(const EventHandler & callback, event_channel_t) = delete; +void Script::subscribe(const EventHandler & callback, event_channel_t) + = delete; } // namespace crepe diff --git a/src/crepe/api/Script.hpp b/src/crepe/api/Script.hpp index 8186bd4..e60be07 100644 --- a/src/crepe/api/Script.hpp +++ b/src/crepe/api/Script.hpp @@ -31,13 +31,16 @@ void Script::logf(Args &&... args) { } template -void Script::subscribe_internal(const EventHandler & callback, event_channel_t channel) { +void Script::subscribe_internal(const EventHandler & callback, + event_channel_t channel) { EventManager & mgr = *this->event_manager_ref; - subscription_t listener = mgr.subscribe([this, callback](const EventType & data) -> bool { - bool & active = *this->active_ref; - if (!active) return false; - return callback(data); - }, channel); + subscription_t listener = mgr.subscribe( + [this, callback](const EventType & data) -> bool { + bool & active = *this->active_ref; + if (!active) return false; + return callback(data); + }, + channel); this->listeners.push_back(listener); } diff --git a/src/test/SceneManagerTest.cpp b/src/test/SceneManagerTest.cpp index dab2ce9..1efcfb2 100644 --- a/src/test/SceneManagerTest.cpp +++ b/src/test/SceneManagerTest.cpp @@ -21,7 +21,7 @@ public: GameObject object3 = mgr.new_object("scene_1", "tag_scene_1", Vector2{2, 0}, 0, 1); } - string get_name() const { return "scene1";} + string get_name() const { return "scene1"; } }; class ConcreteScene2 : public Scene { @@ -36,7 +36,7 @@ public: GameObject object4 = mgr.new_object("scene_2", "tag_scene_2", Vector2{0, 3}, 0, 1); } - string get_name() const { return "scene2";} + string get_name() const { return "scene2"; } }; class SceneManagerTest : public ::testing::Test { diff --git a/src/test/ScriptTest.cpp b/src/test/ScriptTest.cpp index 875c422..c35c3e2 100644 --- a/src/test/ScriptTest.cpp +++ b/src/test/ScriptTest.cpp @@ -6,18 +6,18 @@ #include #include +#include +#include #include #include #include -#include -#include #include using namespace std; using namespace crepe; using namespace testing; -class MyEvent : public Event { }; +class MyEvent : public Event {}; class ScriptTest : public Test { public: @@ -39,9 +39,7 @@ public: // init should never be called more than once EXPECT_LE(this->init_count, 1); } - void update() { - this->update_count++; - } + void update() { this->update_count++; } public: unsigned init_count = 0; @@ -125,4 +123,3 @@ TEST_F(ScriptTest, ListScripts) { } ASSERT_EQ(1, script_count); } - -- cgit v1.2.3 From db66bb4babb19e8a86d5c61281c8b94469729d03 Mon Sep 17 00:00:00 2001 From: Loek Le Blansch Date: Thu, 21 Nov 2024 10:52:22 +0100 Subject: use OptionalRef instead of pointer references --- src/crepe/api/BehaviorScript.hpp | 8 +-- src/crepe/api/Script.cpp | 4 +- src/crepe/api/Script.h | 11 ++-- src/crepe/api/Script.hpp | 8 +-- src/crepe/system/ScriptSystem.cpp | 30 ++++------- src/crepe/system/ScriptSystem.h | 11 ---- src/test/ScriptTest.cpp | 109 ++++++++++++++++++++------------------ 7 files changed, 81 insertions(+), 100 deletions(-) (limited to 'src/test') 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::value); Script * s = new T(std::forward(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