aboutsummaryrefslogtreecommitdiff
path: root/src/crepe/api
diff options
context:
space:
mode:
authorLoek Le Blansch <loek@pipeframe.xyz>2024-11-20 22:49:04 +0100
committerLoek Le Blansch <loek@pipeframe.xyz>2024-11-20 22:49:04 +0100
commit9a5967006c4b8cd202142517b402b4c75dc8b90b (patch)
treebafe2656cfd686a7ca6f6e6023f2d437ff437e7e /src/crepe/api
parent48f69cedaf16038d980071d43d32e22caae44c17 (diff)
add Script::subscribe with specialization for CollisionEvent
Diffstat (limited to 'src/crepe/api')
-rw-r--r--src/crepe/api/Script.cpp4
-rw-r--r--src/crepe/api/Script.h15
-rw-r--r--src/crepe/api/Script.hpp12
3 files changed, 27 insertions, 4 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<CollisionEvent> & 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 <typename... Args>
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 <typename EventType>
- void subscribe(const EventHandler<EventType> & callback, event_channel_t channel = EventManager::CHANNEL_ALL);
+ void subscribe(const EventHandler<EventType> & callback, event_channel_t channel);
+ template <typename EventType>
+ void subscribe(const EventHandler<EventType> & callback);
//! \}
+private:
+ template <typename EventType>
+ void subscribe_internal(const EventHandler<EventType> & 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<subscription_t> listeners;
};
+template <>
+void Script::subscribe(const EventHandler<CollisionEvent> & callback);
+template <>
+void Script::subscribe(const EventHandler<CollisionEvent> & 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 <typename EventType>
-void Script::subscribe(const EventHandler<EventType> & callback, event_channel_t channel) {
+void Script::subscribe_internal(const EventHandler<EventType> & callback, event_channel_t channel) {
EventManager & mgr = *this->event_manager_ref;
subscription_t listener = mgr.subscribe<EventType>(callback, channel);
this->listeners.push_back(listener);
}
+template <typename EventType>
+void Script::subscribe(const EventHandler<EventType> & callback, event_channel_t channel) {
+ this->subscribe_internal(callback, channel);
+}
+
+template <typename EventType>
+void Script::subscribe(const EventHandler<EventType> & callback) {
+ this->subscribe_internal(callback, EventManager::CHANNEL_ALL);
+}
+
} // namespace crepe