Compare commits
2 commits
3ad8fc6d21
...
93d6d14e70
Author | SHA1 | Date | |
---|---|---|---|
93d6d14e70 | |||
b3ccf43097 |
2 changed files with 25 additions and 13 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++; });
|
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);
|
||||||
|
|
||||||
|
|
34
zecsy.hpp
34
zecsy.hpp
|
@ -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);
|
||||||
|
|
||||||
|
@ -86,9 +89,10 @@ namespace zecsy
|
||||||
|
|
||||||
private:
|
private:
|
||||||
using comp_id = size_t;
|
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;
|
entity_id entity_counter = 0;
|
||||||
|
|
||||||
size_t query_archetypes_checked = 0;
|
size_t query_archetypes_checked = 0;
|
||||||
|
@ -102,10 +106,6 @@ namespace zecsy
|
||||||
};
|
};
|
||||||
|
|
||||||
std::unordered_map<comp_id, component_pool> pools;
|
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;
|
std::unordered_map<archetype_signature, entity_group> archetypes;
|
||||||
|
|
||||||
template<Component T>
|
template<Component T>
|
||||||
|
@ -349,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>()), ...);
|
||||||
|
@ -360,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++;
|
||||||
|
@ -367,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
|
||||||
|
|
Loading…
Reference in a new issue