Implement filter method

This commit is contained in:
NukeBird 2025-02-22 20:20:48 +03:00
parent b3ccf43097
commit 93d6d14e70
2 changed files with 22 additions and 7 deletions

View file

@ -207,6 +207,7 @@ TEST_CASE("Create a simple system that processes entities with a specific "
*/ */
w.query<Component>([](entity_id e, Component& c) { c.value++; }); w.query<Component>([](entity_id e, Component& c) { c.value++; });
REQUIRE(w.filter<Component>().size() == 2);
REQUIRE(w.get<Component>(e0).value == 1); REQUIRE(w.get<Component>(e0).value == 1);
REQUIRE(w.get<Component>(e1).value == 21); REQUIRE(w.get<Component>(e1).value == 21);
} }
@ -247,6 +248,9 @@ TEST_CASE("Test a systems ability to query and process only entities with a "
c1.value++; c1.value++;
}); });
REQUIRE(w.filter<C0, C1>().size() == 1);
REQUIRE(w.filter<C0, C1>()[0] == e0);
REQUIRE(w.get<C0>(e0).value == 1); REQUIRE(w.get<C0>(e0).value == 1);
REQUIRE(w.get<C1>(e0).value == 11); REQUIRE(w.get<C1>(e0).value == 11);

View file

@ -78,6 +78,9 @@ namespace zecsy
template<Component First, Component Second, Component... Rest> template<Component First, Component Second, Component... Rest>
void remove(entity_id e); void remove(entity_id e);
template<Component... T>
std::vector<entity_id> filter();
template<Component... T> template<Component... T>
void query(std::invocable<entity_id, T&...> auto&& system); void query(std::invocable<entity_id, T&...> auto&& system);
@ -346,10 +349,8 @@ namespace zecsy
} }
template<Component... T> template<Component... T>
inline void world::query(std::invocable<entity_id, T&...> auto&& system) inline std::vector<entity_id> world::filter()
{ {
/*std::vector<comp_id> required = {get_component_id<T>()...};*/
archetype_signature required; archetype_signature required;
(required.set(get_component_id<T>()), ...); (required.set(get_component_id<T>()), ...);
@ -357,6 +358,8 @@ namespace zecsy
query_archetypes_checked = 0; query_archetypes_checked = 0;
query_entities_processed = 0; query_entities_processed = 0;
std::vector<entity_id> result;
for(const auto& [archetype_key, entities]: archetypes) for(const auto& [archetype_key, entities]: archetypes)
{ {
query_archetypes_checked++; query_archetypes_checked++;
@ -364,11 +367,19 @@ namespace zecsy
if((archetype_key & required) == required) if((archetype_key & required) == required)
{ {
query_entities_processed += entities.size(); query_entities_processed += entities.size();
for(entity_id e: entities) result.insert(result.end(), entities.begin(), entities.end());
{
system(e, get<T>(e)...);
}
} }
} }
return result;
}
template<Component... T>
inline void world::query(std::invocable<entity_id, T&...> auto&& system)
{
for(auto e: filter<T...>())
{
system(e, get<T>(e)...);
}
} }
}; // namespace zecsy }; // namespace zecsy