From f93a0f0afe8aff4d0bbc8ee393918881cfdd9db8 Mon Sep 17 00:00:00 2001 From: Mikko Rasa Date: Fri, 21 Oct 2022 00:29:23 +0300 Subject: [PATCH] Emit events on entity and component creation and destruction --- source/game/events.h | 36 ++++++++++++++++++++++++++++++++++++ source/game/owned.h | 19 +++++++++++++++++-- source/game/stage.cpp | 1 + source/game/stage.h | 8 ++++++++ 4 files changed, 62 insertions(+), 2 deletions(-) create mode 100644 source/game/events.h diff --git a/source/game/events.h b/source/game/events.h new file mode 100644 index 0000000..15988e5 --- /dev/null +++ b/source/game/events.h @@ -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; +}; + +struct EntityDestroyed +{ + Handle entity; +}; + +struct ComponentCreated +{ + Handle component; +}; + +struct ComponentDestroyed +{ + Handle component; +}; + +} // namespace Events +} // namespace Msp::Game + +#endif diff --git a/source/game/owned.h b/source/game/owned.h index e20ff77..620c6aa 100644 --- a/source/game/owned.h +++ b/source/game/owned.h @@ -2,6 +2,7 @@ #define MSP_GAME_OWNED_H_ #include +#include "events.h" #include "handle.h" #include "stage.h" @@ -41,12 +42,19 @@ Owned::Owned(Handle

parent, Args &&... args) if(!parent) throw std::invalid_argument("Owned::Owned"); - Pool &pool = get_stage(*parent).get_pools().template get_pool(); + Stage &stage = get_stage(*parent); + Pool &pool = stage.get_pools().template get_pool(); this->ptr = pool.create(parent, std::forward(args)...); if constexpr(std::is_base_of_v) + { parent->add_component(*this); + stage.get_event_source().emit(*this); + } else + { parent->add_child(*this); + stage.get_event_source().emit(*this); + } } template @@ -77,13 +85,20 @@ void Owned::destroy() if(!obj) return; - Pool &pool = get_stage(*obj).get_pools().template get_pool(); + Stage &stage = get_stage(*obj); if constexpr(std::is_base_of_v) + { + stage.get_event_source().emit(*this); obj->get_entity()->remove_component(*this); + } else if(auto parent = obj->get_parent().get()) + { + stage.get_event_source().emit(*this); parent->remove_child(*this); + } + Pool &pool = stage.get_pools().template get_pool(); pool.destroy(this->ptr); } diff --git a/source/game/stage.cpp b/source/game/stage.cpp index a616d5a..73eb9a5 100644 --- a/source/game/stage.cpp +++ b/source/game/stage.cpp @@ -5,6 +5,7 @@ namespace Msp::Game { Stage::Stage(): + event_source(event_bus), root(std::make_unique(*this)) { } diff --git a/source/game/stage.h b/source/game/stage.h index dbffc99..80e7a4b 100644 --- a/source/game/stage.h +++ b/source/game/stage.h @@ -4,6 +4,8 @@ #include #include #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; + 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; @@ -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 get_root() { return Handle::from_object(root.get()); } template -- 2.43.0