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"
20 using EventSource = Game::EventSource<Events::EntityCreated, Events::EntityDestroyed,
21 Events::ComponentCreated, Events::ComponentDestroyed>;
24 DataFile::Collection &resources;
27 EventSource event_source;
28 /* Use unique_ptr because there's only one root per stage so it's pointless
29 to put it in a pool. */
30 std::unique_ptr<Root> root;
31 std::vector<std::unique_ptr<System>> systems;
34 Stage(DataFile::Collection &);
37 DataFile::Collection &get_resources() const { return resources; }
38 PoolPool &get_pools() { return pools; }
39 EventBus &get_event_bus() { return event_bus; }
40 EventSource &get_event_source() { return event_source; }
41 Handle<Root> get_root() { return Handle<Root>::from_object(root.get()); }
43 template<typename T, typename F>
44 void iterate_objects(const F &);
46 template<typename T, typename... Args>
47 T &add_system(Args &&...);
49 const std::vector<std::unique_ptr<System>> &get_systems() const { return systems; }
52 T *get_system() const;
54 void tick(Time::TimeDelta);
57 template<typename T, typename F>
58 inline void Stage::iterate_objects(const F &func)
60 pools.get_pool<T>().iterate_objects(func);
63 template<typename T, typename... Args>
64 inline T &Stage::add_system(Args &&... args)
66 systems.emplace_back(std::make_unique<T>(*this, std::forward<Args>(args)...));
67 return static_cast<T &>(*systems.back());
71 inline T *Stage::get_system() const
73 for(const auto &s: systems)
74 if(T *ts = dynamic_cast<T *>(s.get()))
79 } // namespace Msp::Game