From: Mikko Rasa Date: Sat, 22 Oct 2022 15:44:01 +0000 (+0300) Subject: Adjust component ticking X-Git-Url: http://git.tdb.fi/?a=commitdiff_plain;h=86057ab12bea9aaf40be2f1f2a0a3e64e94f0313;p=libs%2Fgame.git Adjust component ticking Running pre_tick() for all systems before running tick() for any of them is too inflexible. Also remove the virtual tick() function from Component and use a concept for BasicSystem instead. --- diff --git a/source/game/basicsystem.h b/source/game/basicsystem.h index 12c2725..aa5d942 100644 --- a/source/game/basicsystem.h +++ b/source/game/basicsystem.h @@ -1,52 +1,28 @@ #ifndef MSP_GAME_BASICSYSTEM_ #define MSP_GAME_BASICSYSTEM_ -#include -#include "pool.h" #include "stage.h" #include "system.h" namespace Msp::Game { -template -concept HasPreTick = requires(T x) { x.pre_tick(); }; - template concept HasTick = requires(T x) { x.tick(Time::TimeDelta()); }; template -concept HasPostTick = requires(T x) { x.post_tick(); }; - -template + requires std::is_base_of_v && HasTick class BasicSystem: public System { public: BasicSystem(Stage &s): System(s) { } - void pre_tick() override; void tick(Time::TimeDelta) override; - void post_tick() override; }; -template -void BasicSystem::pre_tick() -{ - if constexpr(HasPreTick) - stage.iterate_objects([](T &obj){ obj.pre_tick(); }); -} - template void BasicSystem::tick(Time::TimeDelta dt) { - if constexpr(HasTick) - stage.iterate_objects([dt](T &obj){ obj.tick(dt); }); -} - -template -void BasicSystem::post_tick() -{ - if constexpr(HasPostTick) - stage.iterate_objects([](T &obj){ obj.post_tick(); }); + stage.iterate_objects([dt](T &obj){ obj.tick(dt); }); } } // namespace Msp::Game diff --git a/source/game/component.h b/source/game/component.h index 6914c15..6b49dd1 100644 --- a/source/game/component.h +++ b/source/game/component.h @@ -18,10 +18,6 @@ public: virtual ~Component() = default; Handle get_entity() const { return entity; } - - virtual void pre_tick() { } - virtual void tick(Time::TimeDelta) { } - virtual void post_tick() { } }; } // namespace Msp::Game diff --git a/source/game/stage.cpp b/source/game/stage.cpp index 73eb9a5..8d781f2 100644 --- a/source/game/stage.cpp +++ b/source/game/stage.cpp @@ -15,12 +15,8 @@ Stage::~Stage() void Stage::tick(Time::TimeDelta dt) { - for(const auto &s: systems) - s->pre_tick(); for(const auto &s: systems) s->tick(dt); - for(const auto &s: systems) - s->post_tick(); } } // namespace Msp::Game diff --git a/source/game/system.h b/source/game/system.h index 88ba291..2d73928 100644 --- a/source/game/system.h +++ b/source/game/system.h @@ -16,9 +16,7 @@ protected: public: virtual ~System() = default; - virtual void pre_tick() = 0; virtual void tick(Time::TimeDelta) = 0; - virtual void post_tick() = 0; }; } // namespace Msp::Game