Implement filter method
This commit is contained in:
parent
b3ccf43097
commit
93d6d14e70
2 changed files with 22 additions and 7 deletions
|
@ -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++; });
|
||||
|
||||
REQUIRE(w.filter<Component>().size() == 2);
|
||||
REQUIRE(w.get<Component>(e0).value == 1);
|
||||
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++;
|
||||
});
|
||||
|
||||
REQUIRE(w.filter<C0, C1>().size() == 1);
|
||||
REQUIRE(w.filter<C0, C1>()[0] == e0);
|
||||
|
||||
REQUIRE(w.get<C0>(e0).value == 1);
|
||||
REQUIRE(w.get<C1>(e0).value == 11);
|
||||
|
||||
|
|
25
zecsy.hpp
25
zecsy.hpp
|
@ -78,6 +78,9 @@ namespace zecsy
|
|||
template<Component First, Component Second, Component... Rest>
|
||||
void remove(entity_id e);
|
||||
|
||||
template<Component... T>
|
||||
std::vector<entity_id> filter();
|
||||
|
||||
template<Component... T>
|
||||
void query(std::invocable<entity_id, T&...> auto&& system);
|
||||
|
||||
|
@ -346,10 +349,8 @@ namespace zecsy
|
|||
}
|
||||
|
||||
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;
|
||||
|
||||
(required.set(get_component_id<T>()), ...);
|
||||
|
@ -357,6 +358,8 @@ namespace zecsy
|
|||
query_archetypes_checked = 0;
|
||||
query_entities_processed = 0;
|
||||
|
||||
std::vector<entity_id> 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<T>(e)...);
|
||||
}
|
||||
result.insert(result.end(), entities.begin(), entities.end());
|
||||
}
|
||||
}
|
||||
|
||||
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
|
||||
|
|
Loading…
Reference in a new issue