diff --git a/zecsy.hpp b/zecsy.hpp index 4c2279c..325a78f 100644 --- a/zecsy.hpp +++ b/zecsy.hpp @@ -29,12 +29,6 @@ namespace zecsy return true; }(); - /* - * Using std::set for entities_set to maintain sorted order, which can - * improve cache locality during queries and iterations. - */ - using entities_set = std::set; - class system_scheduler final { public: @@ -120,10 +114,10 @@ namespace zecsy void query(std::invocable auto&& system); private: - entities_set alive_entities; - entity_id entity_counter = 0; - using comp_id = size_t; + std::set alive_entities; + std::unordered_map> entity_to_comps; + entity_id entity_counter = 0; struct component_pool { @@ -151,12 +145,24 @@ namespace zecsy { auto id = ++entity_counter; alive_entities.emplace(id); + // entity_to_comps[id] = {}; return id; } inline void world::destroy_entity(entity_id e) { alive_entities.erase(e); + + for(comp_id id: entity_to_comps[e]) + { + auto& pool = pools[id]; + auto index = pool.entity_to_index[e]; + pool.entity_to_index.erase(e); + pool.index_to_entity.erase(index); + pool.free_list.emplace_back(index); + } + + entity_to_comps.erase(e); } inline bool world::is_alive(entity_id e) const @@ -167,10 +173,9 @@ namespace zecsy template inline bool world::has(entity_id e) const { - auto id = get_component_id(); - if(pools.contains(id)) + if(entity_to_comps.contains(e)) { - return pools.at(id).entity_to_index.contains(e); + return entity_to_comps.at(e).contains(get_component_id()); } return false; } @@ -211,6 +216,8 @@ namespace zecsy new(&pool.data[index * sizeof(T)]) T(comp); pool.entity_to_index[e] = index; pool.index_to_entity[index] = e; + + entity_to_comps[e].emplace(id); } template @@ -234,6 +241,7 @@ namespace zecsy pool.free_list.push_back(index); pool.entity_to_index.erase(e); pool.index_to_entity.erase(index); + entity_to_comps[e].erase(id); } template