From: Mikko Rasa Date: Fri, 4 Nov 2022 22:40:55 +0000 (+0200) Subject: Add a way for systems to do things at the end of a tick X-Git-Url: http://git.tdb.fi/?a=commitdiff_plain;h=c7177adc21993307482b2c18dc4db6a03361586d;p=libs%2Fgame.git Add a way for systems to do things at the end of a tick Objects should not be created or destroyed mid-tick. --- diff --git a/source/game/stage.cpp b/source/game/stage.cpp index 1c56c7e..8317610 100644 --- a/source/game/stage.cpp +++ b/source/game/stage.cpp @@ -39,6 +39,8 @@ void Stage::tick(Time::TimeDelta dt) { for(const auto &s: systems) s->tick(dt); + for(const auto &s: systems) + s->deferred_tick(); } } // namespace Msp::Game diff --git a/source/game/system.cpp b/source/game/system.cpp new file mode 100644 index 0000000..87ba3b6 --- /dev/null +++ b/source/game/system.cpp @@ -0,0 +1,12 @@ +#include "system.h" + +namespace Msp::Game { + +void System::deferred_tick() +{ + for(const auto &f: deferred_queue) + f(); + deferred_queue.clear(); +} + +} // namespace Msp::Game diff --git a/source/game/system.h b/source/game/system.h index 9e6ec2a..3b37917 100644 --- a/source/game/system.h +++ b/source/game/system.h @@ -1,6 +1,7 @@ #ifndef MSP_GAME_SYSTEM_H_ #define MSP_GAME_SYSTEM_H_ +#include #include namespace Msp::Game { @@ -11,6 +12,7 @@ class System { protected: Stage &stage; + std::vector> deferred_queue; System(Stage &s): stage(s) { } public: @@ -19,6 +21,11 @@ public: Stage &get_stage() const { return stage; } virtual void tick(Time::TimeDelta) = 0; + virtual void deferred_tick(); + +protected: + template + void defer(F &&f) { deferred_queue.emplace_back(std::forward(f)); } }; } // namespace Msp::Game