From 9489c1caefda9d95df32c6231bc8d38d88b23580 Mon Sep 17 00:00:00 2001 From: lisyarus Date: Sat, 29 Oct 2022 10:26:16 +0300 Subject: [PATCH] Pass ecs & species handle to behavior context --- libs/util/include/psemek/util/ecs.hpp | 31 ++++++++++++++++++++------- 1 file changed, 23 insertions(+), 8 deletions(-) diff --git a/libs/util/include/psemek/util/ecs.hpp b/libs/util/include/psemek/util/ecs.hpp index 20b23201..93ffbdc3 100644 --- a/libs/util/include/psemek/util/ecs.hpp +++ b/libs/util/include/psemek/util/ecs.hpp @@ -19,6 +19,8 @@ namespace psemek::util { + struct ecs; + namespace ecs_detail { @@ -58,8 +60,9 @@ namespace psemek::util struct species_base { - species_base(std::string name, species_handle id) - : name_(std::move(name)) + species_base(ecs * ecs, std::string name, species_handle id) + : ecs_(ecs) + , name_(std::move(name)) , id_(id) {} @@ -111,6 +114,7 @@ namespace psemek::util virtual entity_version const * get_version_list() const = 0; protected: + ecs * ecs_; std::string name_; species_handle id_; @@ -147,7 +151,8 @@ namespace psemek::util if (!std::apply(all_nonzero, cptrs)) return; - typename Behavior::context ctx; + typename Behavior::context ctx{*ecs_}; + ctx.species.value = id_; ((std::get(ctx.components) = get_species_component()), ...); @@ -228,7 +233,11 @@ namespace psemek::util ((std::get(cptrs) += entity), ...); - typename Behavior::context ctx; + auto version = get_version_list(); + + typename Behavior::context ctx{*ecs_}; + ctx.species.value = id_; + ctx.entity.value = pack(id_, entity, version ? version[entity] : 0); ((std::get(ctx.components) = get_species_component()), ...); @@ -254,8 +263,8 @@ namespace psemek::util struct species_impl_base : species_base { - species_impl_base(std::string name, species_handle id, Components && ... components) - : species_base(std::move(name), id) + species_impl_base(ecs * ecs, std::string name, species_handle id, Components && ... components) + : species_base(ecs, std::move(name), id) , species_components_{std::move(components)...} {} @@ -485,12 +494,18 @@ namespace psemek::util struct context { + struct ecs & ecs; + species_handle species; entity_handle entity; component_ptrs components; mutable bool remove = false; + context(struct ecs & ecs) + : ecs(ecs) + {} + template Component & get() const { @@ -630,9 +645,9 @@ namespace psemek::util { auto result = species_.size(); if (p == policy::sparse) - species_.push_back(std::make_unique>(std::move(name), result, std::move(components)...)); + species_.push_back(std::make_unique>(this, std::move(name), result, std::move(components)...)); else - species_.push_back(std::make_unique>(std::move(name), result, std::move(components)...)); + species_.push_back(std::make_unique>(this, std::move(name), result, std::move(components)...)); return {result}; }