diff --git a/tests/zecsy.cpp b/tests/zecsy.cpp index abaffd4..01f2b7f 100644 --- a/tests/zecsy.cpp +++ b/tests/zecsy.cpp @@ -207,6 +207,7 @@ TEST_CASE("Create a simple system that processes entities with a specific " */ w.query([](entity_id e, Component& c) { c.value++; }); + REQUIRE(w.filter().size() == 2); REQUIRE(w.get(e0).value == 1); REQUIRE(w.get(e1).value == 21); } @@ -247,6 +248,9 @@ TEST_CASE("Test a systems ability to query and process only entities with a " c1.value++; }); + REQUIRE(w.filter().size() == 1); + REQUIRE(w.filter()[0] == e0); + REQUIRE(w.get(e0).value == 1); REQUIRE(w.get(e0).value == 11); diff --git a/zecsy.hpp b/zecsy.hpp index 6765741..1d87661 100644 --- a/zecsy.hpp +++ b/zecsy.hpp @@ -78,6 +78,9 @@ namespace zecsy template void remove(entity_id e); + template + std::vector filter(); + template void query(std::invocable auto&& system); @@ -346,10 +349,8 @@ namespace zecsy } template - inline void world::query(std::invocable auto&& system) + inline std::vector world::filter() { - /*std::vector required = {get_component_id()...};*/ - archetype_signature required; (required.set(get_component_id()), ...); @@ -357,6 +358,8 @@ namespace zecsy query_archetypes_checked = 0; query_entities_processed = 0; + std::vector result; + for(const auto& [archetype_key, entities]: archetypes) { query_archetypes_checked++; @@ -364,11 +367,19 @@ namespace zecsy if((archetype_key & required) == required) { query_entities_processed += entities.size(); - for(entity_id e: entities) - { - system(e, get(e)...); - } + result.insert(result.end(), entities.begin(), entities.end()); } } + + return result; + } + + template + inline void world::query(std::invocable auto&& system) + { + for(auto e: filter()) + { + system(e, get(e)...); + } } }; // namespace zecsy