1 #ifndef MSP_GAME_STAGE_H_
2 #define MSP_GAME_STAGE_H_
5 #include <msp/datafile/collection.h>
6 #include <msp/time/timedelta.h>
9 #include "eventsource.h"
11 #include "reflection.h"
22 using EventSource = Game::EventSource<Events::EntityCreated, Events::EntityDestroyed,
23 Events::ComponentCreated, Events::ComponentDestroyed, Events::CameraChanged>;
26 Reflection::Reflector &reflector;
27 DataFile::Collection &resources;
30 EventSource event_source;
31 EventObserver event_observer;
32 /* Use unique_ptr because there's only one root per stage so it's pointless
33 to put it in a pool. */
34 std::unique_ptr<Root> root;
35 std::vector<std::unique_ptr<System>> systems;
36 Handle<Camera> active_camera;
39 Stage(Reflection::Reflector &, DataFile::Collection &);
42 Reflection::Reflector &get_reflector() const { return reflector; }
43 DataFile::Collection &get_resources() const { return resources; }
44 PoolPool &get_pools() { return pools; }
45 EventBus &get_event_bus() { return event_bus; }
46 EventSource &get_event_source() { return event_source; }
47 Handle<Root> get_root() { return Handle<Root>::from_object(root.get()); }
49 template<typename T, typename F>
50 void iterate_objects(const F &);
52 template<typename T, typename... Args>
53 T &add_system(Args &&...);
55 void remove_system(System &);
56 const std::vector<std::unique_ptr<System>> &get_systems() const { return systems; }
59 T *get_system() const;
61 void set_active_camera(Handle<Camera>);
62 Handle<Camera> get_active_camera() const { return active_camera; }
64 void synthesize_initial_events(EventObserver &);
66 void synthesize_initial_events(Handle<Entity>, EventObserver &);
69 void tick(Time::TimeDelta);
72 template<typename T, typename F>
73 inline void Stage::iterate_objects(const F &func)
75 pools.get_pool<T>().iterate_objects(func);
78 template<typename T, typename... Args>
79 inline T &Stage::add_system(Args &&... args)
81 systems.emplace_back(std::make_unique<T>(*this, std::forward<Args>(args)...));
82 return static_cast<T &>(*systems.back());
86 inline T *Stage::get_system() const
88 for(const auto &s: systems)
89 if(T *ts = dynamic_cast<T *>(s.get()))
94 } // namespace Msp::Game