#pragma once #include #include #include #include #include #include #include #include #include namespace zecsy { using entity_id = uint64_t; template concept Component = [] { static_assert((std::is_default_constructible_v && ...), "Should have a default constructor"); static_assert((std::is_trivially_copyable_v && ...), "Should be trivially copyable"); static_assert((std::is_trivially_destructible_v && ...), "Should be trivially destructible"); static_assert((std::is_standard_layout_v && ...), "Should have standard layout"); return true; }(); class world final { public: entity_id make_entity(); void destroy_entity(entity_id e); bool is_alive(entity_id e) const; template bool has(entity_id e) const; template bool has(entity_id e) const; template T& get(entity_id e); template void set(entity_id e); template void set(entity_id e, const T& comp); template void set(entity_id e); template void set(entity_id e, const First& comp0, const Second& comp1, const Rest&... rest_comps); template void remove(entity_id e); template void remove(entity_id e); template void query(std::invocable auto&& system); private: using comp_id = size_t; std::set alive_entities; std::unordered_map> entity_to_comps; entity_id entity_counter = 0; struct component_pool { std::vector data; std::vector free_list; // Reusable indices std::unordered_map entity_to_index; std::unordered_map index_to_entity; }; std::unordered_map pools; template static comp_id get_component_id() { static comp_id id = next_component_id++; return id; } static comp_id next_component_id; }; inline world::comp_id world::next_component_id = 0; inline entity_id world::make_entity() { 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 { return alive_entities.contains(e); } template inline bool world::has(entity_id e) const { if(entity_to_comps.contains(e)) { return entity_to_comps.at(e).contains(get_component_id()); } return false; } template inline T& world::get(entity_id e) { auto id = get_component_id(); if(!has(e)) { throw std::runtime_error( std::format("Entity #{} doesn't have {}", e, typeid(T).name())); } auto& pool = pools.at(id); auto index = pool.entity_to_index.at(e); return *reinterpret_cast(&pool.data[index * sizeof(T)]); } template inline void world::set(entity_id e, const T& comp) { auto id = get_component_id(); auto& pool = pools[id]; size_t index; if(!pool.free_list.empty()) { index = pool.free_list.back(); pool.free_list.pop_back(); } else { index = pool.data.size() / sizeof(T); pool.data.resize(pool.data.size() + sizeof(T)); } 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 inline void world::set(entity_id e) { set(e, T{}); } template inline void world::remove(entity_id e) { auto id = get_component_id(); if(!has(e)) { return; } auto& pool = pools[id]; auto index = pool.entity_to_index[e]; 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 inline bool world::has(entity_id e) const { return has(e) && has(e) && (has(e) && ...); } template inline void world::set(entity_id e) { set(e, First{}); set(e, Second{}); (set(e, Rest{}), ...); } template inline void world::set(entity_id e, const First& comp0, const Second& comp1, const Rest&... rest_comps) { set(e, comp0); set(e, comp1); (set(e, rest_comps), ...); } template inline void world::remove(entity_id e) { remove(e); remove(e); (remove(e), ...); } template inline void world::query(std::invocable auto&& system) { for(auto e: alive_entities) { if((has(e))) { system(get(e)...); } } } }; // namespace zecsy