]> git.tdb.fi Git - libs/game.git/blob - source/game/basicsystem.h
12c272585d47cabaa125ec47928254ff0f00b1ff
[libs/game.git] / source / game / basicsystem.h
1 #ifndef MSP_GAME_BASICSYSTEM_
2 #define MSP_GAME_BASICSYSTEM_
3
4 #include <msp/time/timedelta.h>
5 #include "pool.h"
6 #include "stage.h"
7 #include "system.h"
8
9 namespace Msp::Game {
10
11 template<typename T>
12 concept HasPreTick = requires(T x) { x.pre_tick(); };
13
14 template<typename T>
15 concept HasTick = requires(T x) { x.tick(Time::TimeDelta()); };
16
17 template<typename T>
18 concept HasPostTick = requires(T x) { x.post_tick(); };
19
20 template<typename T>
21 class BasicSystem: public System
22 {
23 public:
24         BasicSystem(Stage &s): System(s) { }
25
26         void pre_tick() override;
27         void tick(Time::TimeDelta) override;
28         void post_tick() override;
29 };
30
31 template<typename T>
32 void BasicSystem<T>::pre_tick()
33 {
34         if constexpr(HasPreTick<T>)
35                 stage.iterate_objects<T>([](T &obj){ obj.pre_tick(); });
36 }
37
38 template<typename T>
39 void BasicSystem<T>::tick(Time::TimeDelta dt)
40 {
41         if constexpr(HasTick<T>)
42                 stage.iterate_objects<T>([dt](T &obj){ obj.tick(dt); });
43 }
44
45 template<typename T>
46 void BasicSystem<T>::post_tick()
47 {
48         if constexpr(HasPostTick<T>)
49                 stage.iterate_objects<T>([](T &obj){ obj.post_tick(); });
50 }
51
52 } // namespace Msp::Game
53
54 #endif