From: Mikko Rasa Date: Thu, 20 Oct 2022 21:29:23 +0000 (+0300) Subject: Emit events on entity and component creation and destruction X-Git-Url: http://git.tdb.fi/?a=commitdiff_plain;h=f93a0f0afe8aff4d0bbc8ee393918881cfdd9db8;p=libs%2Fgame.git Emit events on entity and component creation and destruction --- 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