helpers: entity_filler and registry_filler

This commit is contained in:
2019-03-12 02:50:40 +07:00
parent 8d9e112e7a
commit a4a0ab7560
2 changed files with 78 additions and 0 deletions

39
ecs.hpp
View File

@@ -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<T>(std::forward<Args>(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<T>(
priority,
std::forward<Args>(args)...);
return *this;
}
private:
registry& registry_;
};
}
// -----------------------------------------------------------------------------
//
// entity impl

View File

@@ -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<component_n>(
[this](const ecs::const_entity&, component_n& c) noexcept {
c.i += n_;
});
}
private:
int n_;
};
{
ecs::registry w;
ecs::registry_filler(w)
.system<system_n>(0, 1)
.system<system_n>(0, 2);
ecs::entity e1 = w.create_entity();
ecs::entity_filler(e1)
.component<component_n>(0)
.component<component_n>(1);
ecs::entity e2 = w.create_entity();
ecs::entity_filler(e2)
.component<component_n>(2);
w.process_all_systems();
REQUIRE(e1.get_component<component_n>().i == 4);
REQUIRE(e2.get_component<component_n>().i == 5);
}
}
}
TEST_CASE("example") {