Destroyed entity should free its components
This commit is contained in:
parent
52d6b7c4c3
commit
5705238d57
1 changed files with 20 additions and 12 deletions
32
zecsy.hpp
32
zecsy.hpp
|
@ -29,12 +29,6 @@ namespace zecsy
|
||||||
return true;
|
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<entity_id>;
|
|
||||||
|
|
||||||
class system_scheduler final
|
class system_scheduler final
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
|
@ -120,10 +114,10 @@ namespace zecsy
|
||||||
void query(std::invocable<T&...> auto&& system);
|
void query(std::invocable<T&...> auto&& system);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
entities_set alive_entities;
|
|
||||||
entity_id entity_counter = 0;
|
|
||||||
|
|
||||||
using comp_id = size_t;
|
using comp_id = size_t;
|
||||||
|
std::set<entity_id> alive_entities;
|
||||||
|
std::unordered_map<entity_id, std::set<comp_id>> entity_to_comps;
|
||||||
|
entity_id entity_counter = 0;
|
||||||
|
|
||||||
struct component_pool
|
struct component_pool
|
||||||
{
|
{
|
||||||
|
@ -151,12 +145,24 @@ namespace zecsy
|
||||||
{
|
{
|
||||||
auto id = ++entity_counter;
|
auto id = ++entity_counter;
|
||||||
alive_entities.emplace(id);
|
alive_entities.emplace(id);
|
||||||
|
// entity_to_comps[id] = {};
|
||||||
return id;
|
return id;
|
||||||
}
|
}
|
||||||
|
|
||||||
inline void world::destroy_entity(entity_id e)
|
inline void world::destroy_entity(entity_id e)
|
||||||
{
|
{
|
||||||
alive_entities.erase(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
|
inline bool world::is_alive(entity_id e) const
|
||||||
|
@ -167,10 +173,9 @@ namespace zecsy
|
||||||
template<Component T>
|
template<Component T>
|
||||||
inline bool world::has(entity_id e) const
|
inline bool world::has(entity_id e) const
|
||||||
{
|
{
|
||||||
auto id = get_component_id<T>();
|
if(entity_to_comps.contains(e))
|
||||||
if(pools.contains(id))
|
|
||||||
{
|
{
|
||||||
return pools.at(id).entity_to_index.contains(e);
|
return entity_to_comps.at(e).contains(get_component_id<T>());
|
||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
@ -211,6 +216,8 @@ namespace zecsy
|
||||||
new(&pool.data[index * sizeof(T)]) T(comp);
|
new(&pool.data[index * sizeof(T)]) T(comp);
|
||||||
pool.entity_to_index[e] = index;
|
pool.entity_to_index[e] = index;
|
||||||
pool.index_to_entity[index] = e;
|
pool.index_to_entity[index] = e;
|
||||||
|
|
||||||
|
entity_to_comps[e].emplace(id);
|
||||||
}
|
}
|
||||||
|
|
||||||
template<Component T>
|
template<Component T>
|
||||||
|
@ -234,6 +241,7 @@ namespace zecsy
|
||||||
pool.free_list.push_back(index);
|
pool.free_list.push_back(index);
|
||||||
pool.entity_to_index.erase(e);
|
pool.entity_to_index.erase(e);
|
||||||
pool.index_to_entity.erase(index);
|
pool.index_to_entity.erase(index);
|
||||||
|
entity_to_comps[e].erase(id);
|
||||||
}
|
}
|
||||||
|
|
||||||
template<Component First, Component Second, Component... Rest>
|
template<Component First, Component Second, Component... Rest>
|
||||||
|
|
Loading…
Reference in a new issue