]> git.tdb.fi Git - libs/game.git/blob - source/game/stage.h
Add reflection infrastructure
[libs/game.git] / source / game / stage.h
1 #ifndef MSP_GAME_STAGE_H_
2 #define MSP_GAME_STAGE_H_
3
4 #include <memory>
5 #include <msp/datafile/collection.h>
6 #include <msp/time/timedelta.h>
7 #include "eventbus.h"
8 #include "events.h"
9 #include "eventsource.h"
10 #include "handle.h"
11 #include "reflection.h"
12
13 namespace Msp::Game {
14
15 class Camera;
16 class Root;
17 class System;
18
19 class Stage
20 {
21 public:
22         using EventSource = Game::EventSource<Events::EntityCreated, Events::EntityDestroyed,
23                 Events::ComponentCreated, Events::ComponentDestroyed, Events::CameraChanged>;
24
25 private:
26         Reflection::Reflector &reflector;
27         DataFile::Collection &resources;
28         PoolPool pools;
29         EventBus event_bus;
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;
37
38 public:
39         Stage(Reflection::Reflector &, DataFile::Collection &);
40         ~Stage();
41
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()); }
48
49         template<typename T, typename F>
50         void iterate_objects(const F &);
51
52         template<typename T, typename... Args>
53         T &add_system(Args &&...);
54
55         void remove_system(System &);
56         const std::vector<std::unique_ptr<System>> &get_systems() const { return systems; }
57
58         template<typename T>
59         T *get_system() const;
60
61         void set_active_camera(Handle<Camera>);
62         Handle<Camera> get_active_camera() const { return active_camera; }
63
64         void synthesize_initial_events(EventObserver &);
65 private:
66         void synthesize_initial_events(Handle<Entity>, EventObserver &);
67
68 public:
69         void tick(Time::TimeDelta);
70 };
71
72 template<typename T, typename F>
73 inline void Stage::iterate_objects(const F &func)
74 {
75         pools.get_pool<T>().iterate_objects(func);
76 }
77
78 template<typename T, typename... Args>
79 inline T &Stage::add_system(Args &&... args)
80 {
81         systems.emplace_back(std::make_unique<T>(*this, std::forward<Args>(args)...));
82         return static_cast<T &>(*systems.back());
83 }
84
85 template<typename T>
86 inline T *Stage::get_system() const
87 {
88         for(const auto &s: systems)
89                 if(T *ts = dynamic_cast<T *>(s.get()))
90                         return ts;
91         return nullptr;
92 }
93
94 } // namespace Msp::Game
95
96 #endif