7#include <unordered_map>
48 m_entities.erase(entity);
49 for (
auto & component : m_components)
51 component.second.erase(entity);
56 template <
typename T,
typename... Args>
59 const std::type_index type = std::type_index(
typeid(T));
60 m_entities[entity].insert(type);
61 m_components[type][entity] = std::make_shared<T>(T{std::forward<Args>(args)...});
67 const std::type_index type = std::type_index(
typeid(T));
68 auto it = m_components[type].find(entity);
69 if (it == m_components[type].end())
71 throw std::runtime_error(
"Entity does not have component");
73 return static_cast<T *
>(it->second.get());
79 const std::type_index type = std::type_index(
typeid(T));
80 m_entities[entity].erase(type);
81 m_components[type].erase(entity);
90 template <
typename... Components>
91 void forEach(
void (*func)(Components &...))
93 std::vector<std::type_index> types = {std::type_index(
typeid(Components))...};
94 for (
auto & [entity, entity_component_types] : m_entities)
97 std::all_of(types.begin(), types.end(),
98 [&entity_component_types](std::type_index type)
100 return entity_component_types.contains(type);
105 func(*getComponent<Components>(entity)...);
114 std::unordered_map<Entity, std::set<std::type_index>> m_entities;
116 std::unordered_map<std::type_index, std::unordered_map<Entity, std::shared_ptr<void>>> m_components;
Definition: ECS_old.hpp:18
uint64_t Entity
Definition: ECS_old.hpp:22
T * getComponent(Entity entity)
Definition: ECS_old.hpp:65
void removeEntity(Entity entity)
Definition: ECS_old.hpp:46
Entity createEntity()
Definition: ECS_old.hpp:39
ECS & operator=(ECS &&other)=delete
ECS & operator=(ECS &other)=delete
void addComponent(Entity entity, Args &&... args)
Definition: ECS_old.hpp:57
void forEach(void(*func)(Components &...))
This function will call the function func for each entity that has all the components specified.
Definition: ECS_old.hpp:91
~ECS()
Definition: ECS_old.hpp:29
ECS()
Definition: ECS_old.hpp:25
void removeComponent(Entity entity)
Definition: ECS_old.hpp:77