mirror of
https://github.com/BlackMATov/ecs.hpp.git
synced 2025-12-13 10:35:39 +07:00
helpers: entity_filler and registry_filler
This commit is contained in:
39
ecs.hpp
39
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<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
|
||||
|
||||
@@ -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") {
|
||||
|
||||
Reference in New Issue
Block a user