Compare commits

...

2 commits

Author SHA1 Message Date
93d6d14e70 Implement filter method 2025-02-22 20:20:48 +03:00
b3ccf43097 zecsy_bits -> archetype_signature 2025-02-22 19:57:23 +03:00
2 changed files with 25 additions and 13 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++; });
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);

View file

@ -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);
@ -86,9 +89,10 @@ namespace zecsy
private:
using comp_id = size_t;
using zecsy_bits = std::bitset<ZECSY_MAX_COMPONENTS>;
using entity_group = std::set<entity_id>;
using archetype_signature = std::bitset<ZECSY_MAX_COMPONENTS>;
std::unordered_map<entity_id, zecsy_bits> entity_to_comps;
std::unordered_map<entity_id, archetype_signature> entity_to_comps;
entity_id entity_counter = 0;
size_t query_archetypes_checked = 0;
@ -102,10 +106,6 @@ namespace zecsy
};
std::unordered_map<comp_id, component_pool> pools;
using archetype_signature = zecsy_bits;
using entity_group = std::set<entity_id>;
std::unordered_map<archetype_signature, entity_group> archetypes;
template<Component T>
@ -349,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>()), ...);
@ -360,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++;
@ -367,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