From 9a5967006c4b8cd202142517b402b4c75dc8b90b Mon Sep 17 00:00:00 2001 From: Loek Le Blansch Date: Wed, 20 Nov 2024 22:49:04 +0100 Subject: add Script::subscribe with specialization for CollisionEvent --- src/crepe/api/Script.cpp | 4 ++++ src/crepe/api/Script.h | 15 ++++++++++++--- src/crepe/api/Script.hpp | 12 +++++++++++- src/test/CollisionTest.cpp | 30 +++--------------------------- 4 files changed, 30 insertions(+), 31 deletions(-) diff --git a/src/crepe/api/Script.cpp b/src/crepe/api/Script.cpp index 9ebb074..bac5ab2 100644 --- a/src/crepe/api/Script.cpp +++ b/src/crepe/api/Script.cpp @@ -9,3 +9,7 @@ Script::~Script() { } } +template <> +void Script::subscribe(const EventHandler & callback) { + this->subscribe_internal(callback, this->game_object_id); +} diff --git a/src/crepe/api/Script.h b/src/crepe/api/Script.h index 4d98e4d..2d5e7a6 100644 --- a/src/crepe/api/Script.h +++ b/src/crepe/api/Script.h @@ -87,18 +87,22 @@ protected: template void logf(Args &&... args); - game_object_id_t get_game_object_id() const { return this->game_object_id; }; - /** * \brief Subscribe to an event * * \see EventManager::subscribe */ template - void subscribe(const EventHandler & callback, event_channel_t channel = EventManager::CHANNEL_ALL); + void subscribe(const EventHandler & callback, event_channel_t channel); + template + void subscribe(const EventHandler & callback); //! \} +private: + template + void subscribe_internal(const EventHandler & callback, event_channel_t channel); + protected: // NOTE: Script must have a constructor without arguments so the game programmer doesn't need // to manually add `using Script::Script` to their concrete script class. @@ -138,6 +142,11 @@ private: std::vector listeners; }; +template <> +void Script::subscribe(const EventHandler & callback); +template <> +void Script::subscribe(const EventHandler & callback, event_channel_t) = delete; + } // namespace crepe #include "Script.hpp" diff --git a/src/crepe/api/Script.hpp b/src/crepe/api/Script.hpp index 42c8f0b..96e4a36 100644 --- a/src/crepe/api/Script.hpp +++ b/src/crepe/api/Script.hpp @@ -31,10 +31,20 @@ void Script::logf(Args &&... args) { } template -void Script::subscribe(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(callback, channel); this->listeners.push_back(listener); } +template +void Script::subscribe(const EventHandler & callback, event_channel_t channel) { + this->subscribe_internal(callback, channel); +} + +template +void Script::subscribe(const EventHandler & callback) { + this->subscribe_internal(callback, EventManager::CHANNEL_ALL); +} + } // namespace crepe diff --git a/src/test/CollisionTest.cpp b/src/test/CollisionTest.cpp index a68db46..a3c8527 100644 --- a/src/test/CollisionTest.cpp +++ b/src/test/CollisionTest.cpp @@ -24,24 +24,22 @@ class CollisionHandler : public Script { public: int box_id; EventManager & evmgr = EventManager::get_instance(); - function test_fn = [](const CollisionEvent & ev) { }; CollisionHandler(int box_id) { this->box_id = box_id; } bool on_collision(const CollisionEvent& ev) { - test_fn(ev); + Log::logf("Box {} event x={} y={}", box_id, ev.info.move_back_value.x, ev.info.move_back_value.y); return true; } void init() { Log::logf("Box {} script init()", box_id); - // TODO: this should be built into script - evmgr.subscribe([this](const CollisionEvent & ev) { + subscribe([this](const CollisionEvent & ev) { return this->on_collision(ev); - }, this->get_game_object_id()); + }); } }; @@ -109,31 +107,9 @@ public: }; TEST_F(CollisionTest, collision_example) { - script_object1_ref->test_fn = [](const CollisionEvent & ev) { - Log::logf("event x={} y={}", ev.info.move_back_value.x, ev.info.move_back_value.y); - EXPECT_TRUE(true); - }; collision_sys.update(); - - // should be nullptr after update with no collision - //ASSERT_EQ(MyScriptCollider1::last_collision_info_1, nullptr); - //ASSERT_EQ(MyScriptCollider2::last_collision_info_2, nullptr); - // check if values are correct (filled in data) - // EXPECT_EQ(MyScriptCollider1::last_collision_info->first.collider.game_object_id, 1); - // EXPECT_EQ(MyScriptCollider2::last_collision_info->second.collider.game_object_id, 2); - // check test data } TEST_F(CollisionTest, collision_box_box_dynamic) { - script_object1_ref->test_fn = [](const CollisionEvent & ev) { - EXPECT_TRUE(false); - }; collision_sys.update(); - // should be nullptr after update with no collision - // ASSERT_NE(MyScriptCollider1::last_collision_info_1, nullptr); - // ASSERT_NE(MyScriptCollider2::last_collision_info_2, nullptr); - // // check if values are correct (filled in data) - // EXPECT_EQ(MyScriptCollider1::last_collision_info_1->first.collider.game_object_id, 1); - // EXPECT_EQ(MyScriptCollider2::last_collision_info_2->second.collider.game_object_id, 2); - // check test data } -- cgit v1.2.3