--- /dev/null
+#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
#define MSP_GAME_OWNED_H_
#include <stdexcept>
+#include "events.h"
#include "handle.h"
#include "stage.h"
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>
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);
}
#include <memory>
#include <msp/time/timedelta.h>
#include "eventbus.h"
+#include "events.h"
+#include "eventsource.h"
#include "handle.h"
namespace Msp::Game {
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;
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>