]> git.tdb.fi Git - libs/game.git/commitdiff
Emit events on entity and component creation and destruction
authorMikko Rasa <tdb@tdb.fi>
Thu, 20 Oct 2022 21:29:23 +0000 (00:29 +0300)
committerMikko Rasa <tdb@tdb.fi>
Thu, 20 Oct 2022 21:34:26 +0000 (00:34 +0300)
source/game/events.h [new file with mode: 0644]
source/game/owned.h
source/game/stage.cpp
source/game/stage.h

diff --git a/source/game/events.h b/source/game/events.h
new file mode 100644 (file)
index 0000000..15988e5
--- /dev/null
@@ -0,0 +1,36 @@
+#ifndef MSP_GAME_EVENTS_H_
+#define MSP_GAME_EVENTS_H_
+
+#include "handle.h"
+
+namespace Msp::Game {
+
+class Component;
+class Entity;
+
+namespace Events {
+
+struct EntityCreated
+{
+       Handle<Entity> entity;
+};
+
+struct EntityDestroyed
+{
+       Handle<Entity> entity;
+};
+
+struct ComponentCreated
+{
+       Handle<Component> component;
+};
+
+struct ComponentDestroyed
+{
+       Handle<Component> component;
+};
+
+} // namespace Events
+} // namespace Msp::Game
+
+#endif
index e20ff775b61ed557201f4af40bb9244912d8452e..620c6aab54e2114a7e3c718256fb794a92593d47 100644 (file)
@@ -2,6 +2,7 @@
 #define MSP_GAME_OWNED_H_
 
 #include <stdexcept>
+#include "events.h"
 #include "handle.h"
 #include "stage.h"
 
@@ -41,12 +42,19 @@ Owned<T>::Owned(Handle<P> parent, Args &&... args)
        if(!parent)
                throw std::invalid_argument("Owned::Owned");
 
-       Pool<T> &pool = get_stage(*parent).get_pools().template get_pool<T>();
+       Stage &stage = get_stage(*parent);
+       Pool<T> &pool = stage.get_pools().template get_pool<T>();
        this->ptr = pool.create(parent, std::forward<Args>(args)...);
        if constexpr(std::is_base_of_v<Component, T>)
+       {
                parent->add_component(*this);
+               stage.get_event_source().emit<Events::ComponentCreated>(*this);
+       }
        else
+       {
                parent->add_child(*this);
+               stage.get_event_source().emit<Events::EntityCreated>(*this);
+       }
 }
 
 template<typename T>
@@ -77,13 +85,20 @@ void Owned<T>::destroy()
        if(!obj)
                return;
 
-       Pool<T> &pool = get_stage(*obj).get_pools().template get_pool<T>();
+       Stage &stage = get_stage(*obj);
 
        if constexpr(std::is_base_of_v<Component, T>)
+       {
+               stage.get_event_source().emit<Events::ComponentDestroyed>(*this);
                obj->get_entity()->remove_component(*this);
+       }
        else if(auto parent = obj->get_parent().get())
+       {
+               stage.get_event_source().emit<Events::EntityDestroyed>(*this);
                parent->remove_child(*this);
+       }
 
+       Pool<T> &pool = stage.get_pools().template get_pool<T>();
        pool.destroy(this->ptr);
 }
 
index a616d5a94da775ebd61da5171e9b4baf2435215d..73eb9a55f7299542cb08cf8c95b44083d7ee8fc2 100644 (file)
@@ -5,6 +5,7 @@
 namespace Msp::Game {
 
 Stage::Stage():
+       event_source(event_bus),
        root(std::make_unique<Root>(*this))
 { }
 
index dbffc9960bedb4fd89c55a232dd6dd91f0fb68c5..80e7a4b72c25e51141d43718c6bed196ffdf712c 100644 (file)
@@ -4,6 +4,8 @@
 #include <memory>
 #include <msp/time/timedelta.h>
 #include "eventbus.h"
+#include "events.h"
+#include "eventsource.h"
 #include "handle.h"
 
 namespace Msp::Game {
@@ -13,9 +15,14 @@ class System;
 
 class Stage
 {
+public:
+       using EventSource = Game::EventSource<Events::EntityCreated, Events::EntityDestroyed,
+               Events::ComponentCreated, Events::ComponentDestroyed>;
+
 private:
        PoolPool pools;
        EventBus event_bus;
+       EventSource event_source;
        /* Use unique_ptr because there's only one root per stage so it's pointless
        to put it in a pool. */
        std::unique_ptr<Root> root;
@@ -27,6 +34,7 @@ public:
 
        PoolPool &get_pools() { return pools; }
        EventBus &get_event_bus() { return event_bus; }
+       EventSource &get_event_source() { return event_source; }
        Handle<Root> get_root() { return Handle<Root>::from_object(root.get()); }
 
        template<typename T, typename F>