]> git.tdb.fi Git - libs/game.git/blob - source/game/stage.h
Add resource container references to Director and Stage
[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
12 namespace Msp::Game {
13
14 class Root;
15 class System;
16
17 class Stage
18 {
19 public:
20         using EventSource = Game::EventSource<Events::EntityCreated, Events::EntityDestroyed,
21                 Events::ComponentCreated, Events::ComponentDestroyed>;
22
23 private:
24         DataFile::Collection &resources;
25         PoolPool pools;
26         EventBus event_bus;
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;
32
33 public:
34         Stage(DataFile::Collection &);
35         ~Stage();
36
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()); }
42
43         template<typename T, typename F>
44         void iterate_objects(const F &);
45
46         template<typename T, typename... Args>
47         T &add_system(Args &&...);
48
49         const std::vector<std::unique_ptr<System>> &get_systems() const { return systems; }
50
51         template<typename T>
52         T *get_system() const;
53
54         void tick(Time::TimeDelta);
55 };
56
57 template<typename T, typename F>
58 inline void Stage::iterate_objects(const F &func)
59 {
60         pools.get_pool<T>().iterate_objects(func);
61 }
62
63 template<typename T, typename... Args>
64 inline T &Stage::add_system(Args &&... args)
65 {
66         systems.emplace_back(std::make_unique<T>(*this, std::forward<Args>(args)...));
67         return static_cast<T &>(*systems.back());
68 }
69
70 template<typename T>
71 inline T *Stage::get_system() const
72 {
73         for(const auto &s: systems)
74                 if(T *ts = dynamic_cast<T *>(s.get()))
75                         return ts;
76         return nullptr;
77 }
78
79 } // namespace Msp::Game
80
81 #endif