From 57f0821b0effb6a41582de0dd9d46b6075fd5c2d Mon Sep 17 00:00:00 2001 From: BlackMATov Date: Tue, 15 Oct 2019 10:04:44 +0700 Subject: [PATCH] update readme example --- README.md | 68 +++++++++++++++++++++++++------------------ untests/ecs_tests.cpp | 64 ++++++++++++++++++++++------------------ 2 files changed, 74 insertions(+), 58 deletions(-) diff --git a/README.md b/README.md index 4b4db71..b198ae1 100644 --- a/README.md +++ b/README.md @@ -41,61 +41,71 @@ target_link_libraries(your_project_target ecs.hpp) ## Basic usage ```cpp -struct position_component { - float x; - float y; - position_component(float nx, float ny) - : x(nx), y(ny) {} + +#include +namespace ecs = ecs_hpp; + +struct movable {}; +struct disabled {}; + +struct position { + float x{}; + float y{}; }; -struct velocity_component { - float dx; - float dy; - velocity_component(float ndx, float ndy) - : dx(ndx), dy(ndy) {} +struct velocity { + float dx{}; + float dy{}; }; -class movement_system : public ecs_hpp::system { +class movement_system : public ecs::system { public: - void process(ecs_hpp::registry& owner) override { + void process(ecs::registry& owner) override { owner.for_joined_components< - position_component, - velocity_component - >([](const ecs_hpp::entity& e, position_component& p, const velocity_component& v) { + position, + velocity + >([](ecs::entity, position& p, const velocity& v) { p.x += v.dx; p.y += v.dy; - }); + }, ecs::require{}, ecs::filter{}); } }; -class gravity_system : public ecs_hpp::system { +class gravity_system : public ecs::system { public: gravity_system(float gravity) : gravity_(gravity) {} - void process(ecs_hpp::registry& owner) override { + void process(ecs::registry& owner) override { owner.for_each_component< - velocity_component - >([this](const ecs_hpp::entity& e, velocity_component& v) { + velocity + >([this](ecs::entity e, velocity& v) { v.dx += gravity_; v.dy += gravity_; - }); + }, ecs::filter{}); } private: - float gravity_; + float gravity_{}; }; -ecs_hpp::registry world; -world.add_system(0); -world.add_system(1, 9.8f); +ecs::registry world; + +ecs::registry_filler(world) + .system(0) + .system(1, 9.8f); auto entity_one = world.create_entity(); -world.assign_component(entity_one, 4.f, 2.f); -world.assign_component(entity_one, 10.f, 20.f); +ecs::entity_filler(entity_one) + .component() + .component(4.f, 2.f) + .component(10.f, 20.f); auto entity_two = world.create_entity(); -entity_two.assign_component(4.f, 2.f); -entity_two.assign_component(10.f, 20.f); +ecs::entity_filler(entity_two) + .component() + .component() + .component(4.f, 2.f) + .component(10.f, 20.f); world.process_all_systems(); ``` diff --git a/untests/ecs_tests.cpp b/untests/ecs_tests.cpp index 7c757fd..2e99802 100644 --- a/untests/ecs_tests.cpp +++ b/untests/ecs_tests.cpp @@ -1571,61 +1571,67 @@ TEST_CASE("registry") { } TEST_CASE("example") { - struct position_component { - float x; - float y; - position_component(float nx, float ny) - : x(nx), y(ny) {} + struct movable {}; + struct disabled {}; + + struct position { + float x{}; + float y{}; }; - struct velocity_component { - float dx; - float dy; - velocity_component(float ndx, float ndy) - : dx(ndx), dy(ndy) {} + struct velocity { + float dx{}; + float dy{}; }; - class movement_system : public ecs_hpp::system { + class movement_system : public ecs::system { public: - void process(ecs_hpp::registry& owner) override { + void process(ecs::registry& owner) override { owner.for_joined_components< - position_component, - velocity_component - >([](const ecs_hpp::entity&, position_component& p, const velocity_component& v) { + position, + velocity + >([](ecs::entity, position& p, const velocity& v) { p.x += v.dx; p.y += v.dy; - }); + }, ecs::require{}, ecs::filter{}); } }; - class gravity_system : public ecs_hpp::system { + class gravity_system : public ecs::system { public: gravity_system(float gravity) : gravity_(gravity) {} - void process(ecs_hpp::registry& owner) override { + void process(ecs::registry& owner) override { owner.for_each_component< - velocity_component - >([this](const ecs_hpp::entity&, velocity_component& v) { + velocity + >([this](ecs::entity, velocity& v) { v.dx += gravity_; v.dy += gravity_; - }); + }, ecs::filter{}); } private: - float gravity_; + float gravity_{}; }; - ecs_hpp::registry world; - world.add_system(0); - world.add_system(1, 9.8f); + ecs::registry world; + + ecs::registry_filler(world) + .system(0) + .system(1, 9.8f); auto entity_one = world.create_entity(); - world.assign_component(entity_one, 4.f, 2.f); - world.assign_component(entity_one, 10.f, 20.f); + ecs::entity_filler(entity_one) + .component() + .component(4.f, 2.f) + .component(10.f, 20.f); auto entity_two = world.create_entity(); - entity_two.assign_component(4.f, 2.f); - entity_two.assign_component(10.f, 20.f); + ecs::entity_filler(entity_two) + .component() + .component() + .component(4.f, 2.f) + .component(10.f, 20.f); world.process_all_systems(); }