]> git.tdb.fi Git - libs/game.git/blob - source/game/stage.h
Decorate things which constitute the public API of the library
[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 "mspgame_api.h"
12 #include "reflection.h"
13 #include "systemscheduler.h"
14
15 namespace Msp::Game {
16
17 class Camera;
18 class Root;
19 class System;
20
21 class MSPGAME_API Stage
22 {
23 public:
24         using EventSource = Game::EventSource<Events::EntityCreated, Events::EntityDestroyed,
25                 Events::ComponentCreated, Events::ComponentDestroyed, Events::CameraChanged>;
26
27 private:
28         Reflection::Reflector &reflector;
29         DataFile::Collection &resources;
30         PoolPool pools;
31         EventBus event_bus;
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;
41
42 public:
43         Stage(Reflection::Reflector &, DataFile::Collection &);
44         ~Stage();
45
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()); }
52
53         template<typename T, typename F>
54         void iterate_objects(const F &);
55
56         template<typename T, typename... Args>
57         T &add_system(Args &&...);
58
59         void remove_system(System &);
60         const std::vector<std::unique_ptr<System>> &get_systems() const { return systems; }
61
62         template<typename T>
63         T *get_system() const;
64
65         void set_active_camera(Handle<Camera>);
66         Handle<Camera> get_active_camera() const { return active_camera; }
67
68         void synthesize_initial_events(EventObserver &);
69 private:
70         void synthesize_initial_events(Handle<Entity>, EventObserver &);
71
72 public:
73         void tick(Time::TimeDelta);
74 };
75
76 template<typename T, typename F>
77 inline void Stage::iterate_objects(const F &func)
78 {
79         pools.get_pool<T>().iterate_objects(func);
80 }
81
82 template<typename T, typename... Args>
83 inline T &Stage::add_system(Args &&... args)
84 {
85         // Ensure that a reflected class exists for scheduling
86         reflector.get_or_create_class<T>();
87
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);
92 }
93
94 template<typename T>
95 inline T *Stage::get_system() const
96 {
97         for(const auto &s: systems)
98                 if(T *ts = dynamic_cast<T *>(s.get()))
99                         return ts;
100         return nullptr;
101 }
102
103 } // namespace Msp::Game
104
105 #endif