]> git.tdb.fi Git - libs/game.git/blob - source/game/stage.h
Adjust some things to make header dependencies easier to manage
[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/time/timedelta.h>
6 #include "handle.h"
7
8 namespace Msp::Game {
9
10 class Root;
11 class System;
12
13 class Stage
14 {
15 private:
16         PoolPool pools;
17         /* Use unique_ptr because there's only one root per stage so it's pointless
18         to put it in a pool. */
19         std::unique_ptr<Root> root;
20         std::vector<std::unique_ptr<System>> systems;
21
22 public:
23         Stage();
24         ~Stage();
25
26         PoolPool &get_pools() { return pools; }
27         Handle<Root> get_root() { return Handle<Root>::from_object(root.get()); }
28
29         template<typename T, typename F>
30         void iterate_objects(const F &);
31
32         template<typename T, typename... Args>
33         T &add_system(Args &&...);
34
35         const std::vector<std::unique_ptr<System>> &get_systems() const { return systems; }
36
37         void tick(Time::TimeDelta);
38 };
39
40 template<typename T, typename F>
41 void Stage::iterate_objects(const F &func)
42 {
43         pools.get_pool<T>().iterate_objects(func);
44 }
45
46 template<typename T, typename... Args>
47 T &Stage::add_system(Args &&... args)
48 {
49         systems.emplace_back(std::make_unique<T>(*this, std::forward<Args>(args)...));
50         return static_cast<T &>(*systems.back());
51 }
52
53 } // namespace Msp::Game
54
55 #endif