From 2482a8c224bfa455d21b0188eea2884db75cfdae Mon Sep 17 00:00:00 2001 From: lisyarus Date: Tue, 16 Aug 2022 10:14:21 +0300 Subject: [PATCH] Support adding arbitrary arguments to ECS behavior call --- libs/util/include/psemek/util/ecs.hpp | 36 +++++++++++++-------------- 1 file changed, 18 insertions(+), 18 deletions(-) diff --git a/libs/util/include/psemek/util/ecs.hpp b/libs/util/include/psemek/util/ecs.hpp index 1f2a69c8..41270b44 100644 --- a/libs/util/include/psemek/util/ecs.hpp +++ b/libs/util/include/psemek/util/ecs.hpp @@ -85,10 +85,10 @@ namespace psemek::util virtual bool entity_active(entity_handle h) const = 0; - template - void apply(Behavior & behavior) + template + void apply(Behavior & behavior, Args const & ... args) { - apply_impl(behavior, static_cast(nullptr), std::make_index_sequence>{}); + apply_impl(behavior, static_cast(nullptr), std::make_index_sequence>{}, args...); } virtual ~species_base() {} @@ -102,8 +102,8 @@ namespace psemek::util std::string name_; species_handle id_; - template - void apply_impl(Behavior & behavior, std::tuple *, std::index_sequence) + template + void apply_impl(Behavior & behavior, std::tuple *, std::index_sequence, Args const & ... args) { std::tuple cptrs; @@ -123,13 +123,13 @@ namespace psemek::util auto visit = [&](auto * ... ptrs) { - if constexpr (std::is_invocable_v) + if constexpr (std::is_invocable_v) { - behavior(*ptrs..., ctx); + behavior(*ptrs..., ctx, args...); } else { - behavior(*ptrs...); + behavior(*ptrs..., args...); } }; @@ -460,11 +460,11 @@ namespace psemek::util template typename Component::data const * get_if(entity_handle entity) const; - template - void apply(Behavior && behavior); + template + void apply(Behavior && behavior, Args const & ... args); - template - void apply(Behavior && behavior, species_handle species); + template + void apply(Behavior && behavior, species_handle species, Args const & ... args); private: std::vector> species_; @@ -560,17 +560,17 @@ namespace psemek::util return const_cast(const_cast(this)->get_if(entity)); } - template - void ecs::apply(Behavior && behavior) + template + void ecs::apply(Behavior && behavior, Args const & ... args) { for (auto & s : species_) - s->apply(behavior); + s->apply(behavior, args...); } - template - void ecs::apply(Behavior && behavior, species_handle species) + template + void ecs::apply(Behavior && behavior, species_handle species, Args const & ... args) { - species_[species.value]->apply(behavior); + species_[species.value]->apply(behavior, args...); } }