|
|
|
@ -19,7 +19,6 @@ namespace DinkyECS |
|
|
|
|
template <typename T> |
|
|
|
|
struct ComponentStorage { |
|
|
|
|
std::vector<T> data; |
|
|
|
|
std::queue<size_t> free_indices; |
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
struct Event { |
|
|
|
@ -36,12 +35,23 @@ namespace DinkyECS |
|
|
|
|
std::unordered_map<std::type_index, std::any> $facts; |
|
|
|
|
std::unordered_map<std::type_index, EventQueue> $events; |
|
|
|
|
std::unordered_map<std::type_index, std::any> $component_storages; |
|
|
|
|
std::unordered_map<std::type_index, std::queue<size_t>> $free_indices; |
|
|
|
|
std::unordered_map<Entity, bool> $constants; |
|
|
|
|
|
|
|
|
|
Entity entity() { return ++entity_count; } |
|
|
|
|
|
|
|
|
|
void destroy(DinkyECS::Entity entity) { |
|
|
|
|
(void)entity; |
|
|
|
|
dbc::check(!$constants.contains(entity), "trying to destroy an entity in constants"); |
|
|
|
|
|
|
|
|
|
for(auto& [tid, map] : $components) { |
|
|
|
|
|
|
|
|
|
if(map.contains(entity)) { |
|
|
|
|
size_t index = map.at(entity); |
|
|
|
|
auto& free_queue = $free_indices.at(tid); |
|
|
|
|
free_queue.push(index); |
|
|
|
|
map.erase(entity); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
void clone_into(DinkyECS::World &to_world) { |
|
|
|
@ -76,11 +86,12 @@ namespace DinkyECS |
|
|
|
|
template <typename Comp> |
|
|
|
|
size_t make_component() { |
|
|
|
|
auto &storage = component_storage_for<Comp>(); |
|
|
|
|
auto &free_queue = $free_indices.at(std::type_index(typeid(Comp))); |
|
|
|
|
size_t index; |
|
|
|
|
|
|
|
|
|
if(!storage.free_indices.empty()) { |
|
|
|
|
index = storage.free_indices.front(); |
|
|
|
|
storage.free_indices.pop(); |
|
|
|
|
if(!free_queue.empty()) { |
|
|
|
|
index = free_queue.front(); |
|
|
|
|
free_queue.pop(); |
|
|
|
|
} else { |
|
|
|
|
storage.data.emplace_back(); |
|
|
|
|
index = storage.data.size() - 1; |
|
|
|
@ -93,6 +104,7 @@ namespace DinkyECS |
|
|
|
|
ComponentStorage<Comp> &component_storage_for() { |
|
|
|
|
auto type_index = std::type_index(typeid(Comp)); |
|
|
|
|
$component_storages.try_emplace(type_index, ComponentStorage<Comp>{}); |
|
|
|
|
$free_indices.try_emplace(type_index, std::queue<size_t>{}); |
|
|
|
|
return std::any_cast<ComponentStorage<Comp> &>( |
|
|
|
|
$component_storages.at(type_index)); |
|
|
|
|
} |
|
|
|
@ -113,10 +125,10 @@ namespace DinkyECS |
|
|
|
|
|
|
|
|
|
if(map.contains(ent)) { |
|
|
|
|
size_t index = map.at(ent); |
|
|
|
|
component_storage_for<Comp>().free_indices.push(index); |
|
|
|
|
auto& free_queue = $free_indices.at(std::type_index(typeid(Comp))); |
|
|
|
|
free_queue.push(index); |
|
|
|
|
map.erase(ent); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
map.erase(ent); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
template <typename Comp> |
|
|
|
|