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 "mspgame_api.h"
12 #include "reflection.h"
13 #include "systemscheduler.h"
21 class MSPGAME_API Stage
24 using EventSource = Game::EventSource<Events::EntityCreated, Events::EntityDestroyed,
25 Events::ComponentCreated, Events::ComponentDestroyed, Events::CameraChanged>;
28 Reflection::Reflector &reflector;
29 DataFile::Collection &resources;
32 EventSource event_source;
33 EventObserver event_observer;
34 /* Use unique_ptr because there's only one root per stage so it's pointless
35 to put it in a pool. */
36 std::unique_ptr<Root> root;
37 std::vector<std::unique_ptr<System>> systems;
38 SystemScheduler scheduler;
39 Handle<Camera> active_camera;
40 bool pending_reschedule = false;
43 Stage(Reflection::Reflector &, DataFile::Collection &);
46 Reflection::Reflector &get_reflector() const { return reflector; }
47 DataFile::Collection &get_resources() const { return resources; }
48 PoolPool &get_pools() { return pools; }
49 EventBus &get_event_bus() { return event_bus; }
50 EventSource &get_event_source() { return event_source; }
51 Handle<Root> get_root() { return Handle<Root>::from_object(root.get()); }
53 template<typename T, typename F>
54 void iterate_objects(const F &);
56 template<typename T, typename... Args>
57 T &add_system(Args &&...);
59 void remove_system(System &);
60 const std::vector<std::unique_ptr<System>> &get_systems() const { return systems; }
63 T *get_system() const;
65 void set_active_camera(Handle<Camera>);
66 Handle<Camera> get_active_camera() const { return active_camera; }
68 void synthesize_initial_events(EventObserver &);
70 void synthesize_initial_events(Handle<Entity>, EventObserver &);
73 void tick(Time::TimeDelta);
76 template<typename T, typename F>
77 inline void Stage::iterate_objects(const F &func)
79 pools.get_pool<T>().iterate_objects(func);
82 template<typename T, typename... Args>
83 inline T &Stage::add_system(Args &&... args)
85 // Ensure that a reflected class exists for scheduling
86 reflector.get_or_create_class<T>();
88 auto &sys = systems.emplace_back(std::make_unique<T>(*this, std::forward<Args>(args)...));
89 scheduler.add_system(*sys);
90 pending_reschedule = true;
91 return static_cast<T &>(*sys);
95 inline T *Stage::get_system() const
97 for(const auto &s: systems)
98 if(T *ts = dynamic_cast<T *>(s.get()))
103 } // namespace Msp::Game