From f77becd2690609c0926c9ec18dfb310b475be13a Mon Sep 17 00:00:00 2001 From: BlackMATov Date: Wed, 26 Dec 2018 18:36:41 +0700 Subject: [PATCH] dummy systems --- ecs.hpp | 44 +++++++++++++++++++++++++++++++++++++++----- ecs_tests.cpp | 35 ++++++++++++++++++++++++++++++++++- 2 files changed, 73 insertions(+), 6 deletions(-) diff --git a/ecs.hpp b/ecs.hpp index 85818b3..3ae4513 100644 --- a/ecs.hpp +++ b/ecs.hpp @@ -13,6 +13,7 @@ #include #include #include +#include #include #include #include @@ -31,6 +32,7 @@ namespace ecs_hpp { class entity; + class system; class registry; using family_id = std::uint16_t; @@ -275,6 +277,21 @@ namespace std }; } +// ----------------------------------------------------------------------------- +// +// system +// +// ----------------------------------------------------------------------------- + +namespace ecs_hpp +{ + class system { + public: + virtual ~system() = default; + virtual void process(registry& owner) = 0; + }; +} + // ----------------------------------------------------------------------------- // // registry @@ -285,8 +302,8 @@ namespace ecs_hpp { class registry final { public: - registry(); - ~registry() noexcept; + registry() = default; + ~registry() noexcept = default; entity create_entity(); bool destroy_entity(const entity& ent); @@ -332,6 +349,10 @@ namespace ecs_hpp void for_joined_components(F&& f); template < typename... Ts, typename F > void for_joined_components(F&& f) const; + + template < typename T, typename... Args > + void add_system(Args&&... args); + void process_systems(); private: template < typename T > detail::component_storage* find_storage_() noexcept; @@ -370,6 +391,9 @@ namespace ecs_hpp using storage_uptr = std::unique_ptr; std::unordered_map storages_; + + using system_uptr = std::unique_ptr; + std::vector systems_; }; } @@ -483,9 +507,6 @@ namespace ecs_hpp namespace ecs_hpp { - inline registry::registry() = default; - inline registry::~registry() noexcept = default; - inline entity registry::create_entity() { std::lock_guard guard(mutex_); assert(last_entity_id_ < std::numeric_limits::max()); @@ -628,6 +649,19 @@ namespace ecs_hpp for_joined_components_impl_(std::forward(f)); } + template < typename T, typename... Args > + void registry::add_system(Args&&... args) { + std::lock_guard guard(mutex_); + systems_.emplace_back( + std::make_unique(std::forward(args)...)); + } + + inline void registry::process_systems() { + for ( auto& s : systems_ ) { + s->process(*this); + } + } + template < typename T > detail::component_storage* registry::find_storage_() noexcept { const auto family = detail::type_family::id(); diff --git a/ecs_tests.cpp b/ecs_tests.cpp index ff5f34b..7163c76 100644 --- a/ecs_tests.cpp +++ b/ecs_tests.cpp @@ -318,7 +318,6 @@ TEST_CASE("registry") { } } } - SECTION("for_joined_components") { { ecs::registry w; @@ -365,4 +364,38 @@ TEST_CASE("registry") { } } } + SECTION("systems") { + { + class movement_system : public ecs::system { + public: + void process(ecs::registry& owner) override { + owner.for_joined_components([]( + ecs::entity e, position_c& p, const velocity_c& v) + { + p.x += v.x; + p.y += v.y; + }); + } + }; + + ecs::registry w; + w.add_system(); + + auto e1 = w.create_entity(); + auto e2 = w.create_entity(); + + e1.assign_component(1, 2); + e1.assign_component(3, 4); + e2.assign_component(5, 6); + e2.assign_component(7, 8); + + w.process_systems(); + + REQUIRE(e1.get_component().x == 1 + 3); + REQUIRE(e1.get_component().y == 2 + 4); + + REQUIRE(e2.get_component().x == 5 + 7); + REQUIRE(e2.get_component().y == 6 + 8); + } + } }