From a4a0ab7560c9f7f50e798465ff863e1e08cd4da3 Mon Sep 17 00:00:00 2001 From: BlackMATov Date: Tue, 12 Mar 2019 02:50:40 +0700 Subject: [PATCH] helpers: entity_filler and registry_filler --- ecs.hpp | 39 +++++++++++++++++++++++++++++++++++++++ ecs_tests.cpp | 39 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 78 insertions(+) diff --git a/ecs.hpp b/ecs.hpp index 313bdb8..c045567 100644 --- a/ecs.hpp +++ b/ecs.hpp @@ -1156,6 +1156,45 @@ namespace ecs_hpp }; } +// ----------------------------------------------------------------------------- +// +// fillers +// +// ----------------------------------------------------------------------------- + +namespace ecs_hpp +{ + class entity_filler final { + public: + entity_filler(entity& entity) noexcept + : entity_(entity) {} + + template < typename T, typename... Args > + entity_filler& component(Args&&... args) { + entity_.assign_component(std::forward(args)...); + return *this; + } + private: + entity& entity_; + }; + + class registry_filler final { + public: + registry_filler(registry& registry) noexcept + : registry_(registry) {} + + template < typename T, typename... Args > + registry_filler& system(priority_t priority, Args&&... args) { + registry_.add_system( + priority, + std::forward(args)...); + return *this; + } + private: + registry& registry_; + }; +} + // ----------------------------------------------------------------------------- // // entity impl diff --git a/ecs_tests.cpp b/ecs_tests.cpp index d4944e6..775c6e5 100644 --- a/ecs_tests.cpp +++ b/ecs_tests.cpp @@ -978,6 +978,45 @@ TEST_CASE("registry") { REQUIRE(i == 25); } } + SECTION("fillers") { + struct component_n { + int i = 0; + component_n(int ni) : i(ni) {} + }; + + class system_n : public ecs::system { + public: + system_n(int n) : n_(n) {} + void process(ecs::registry& owner) override { + owner.for_each_component( + [this](const ecs::const_entity&, component_n& c) noexcept { + c.i += n_; + }); + } + private: + int n_; + }; + { + ecs::registry w; + ecs::registry_filler(w) + .system(0, 1) + .system(0, 2); + + ecs::entity e1 = w.create_entity(); + ecs::entity_filler(e1) + .component(0) + .component(1); + + ecs::entity e2 = w.create_entity(); + ecs::entity_filler(e2) + .component(2); + + w.process_all_systems(); + + REQUIRE(e1.get_component().i == 4); + REQUIRE(e2.get_component().i == 5); + } + } } TEST_CASE("example") {