6#include <unordered_map>
14template <
typename... AllComponentTypes>
17 mutable std::tuple<SparseSet<Entity, AllComponentTypes>...> componentSets;
20 template <
typename ComponentType>
23 auto& componentSet = getComponentSet<ComponentType>();
24 componentSet.set(entity, std::move(component));
27 template <
typename ComponentType>
30 auto& componentSet = getComponentSet<ComponentType>();
31 componentSet.remove(entity);
36 forEachComponentType<AllComponentTypes...>(
37 [&]<
typename Component>()
39 removeComponent<Component>(entity);
43 template <
typename... ComponentTypes>
46 forEachComponentType<ComponentTypes...>(
47 [&]<
typename Component>()
49 removeComponent<Component>(entity);
53 template <
typename ComponentType>
56 auto& componentSet = getComponentSet<ComponentType>();
58 return componentSet.get(entity);
61 template <
typename ComponentType>
64 const auto& componentSet = getComponentSet<ComponentType>();
66 return componentSet.get(entity);
69 template <
typename ComponentType>
72 return getComponentSet<ComponentType>().contains(entity);
75 template <
typename ComponentType>
78 return getComponentSet<ComponentType>().getKeys();
81 template <
typename... ComponentTypes>
84 std::vector<Entity> entities;
85 bool isFirstComponentType =
true;
88 auto intersect = [](
const std::vector<Entity>& v1,
const std::vector<Entity>& v2)
90 std::vector<Entity> v_intersection;
91 std::set_intersection(v1.begin(), v1.end(), v2.begin(), v2.end(), std::back_inserter(v_intersection));
92 return v_intersection;
96 forEachComponentType<ComponentTypes...>(
97 [
this, &entities, &isFirstComponentType, &intersect]<
typename T>()
101 auto componentEntities = getEntitiesByComponent<T>();
102 std::sort(componentEntities.begin(), componentEntities.end());
104 if (isFirstComponentType)
106 entities = componentEntities;
107 isFirstComponentType =
false;
110 entities = intersect(entities, componentEntities);
119 size_t totalSize = 0;
121 forEachComponentType<AllComponentTypes...>(
122 [
this, &totalSize]<
typename T>()
124 totalSize += getComponentSet<T>().size();
130 template <
typename... ComponentTypes>
133 size_t totalSize = 0;
135 forEachComponentType<ComponentTypes...>(
136 [
this, &totalSize]<
typename T>()
138 totalSize += getComponentSet<T>().size();
146 forEachComponentType<AllComponentTypes...>(
149 getComponentSet<T>().clear();
153 template <
typename... ComponentTypes>
156 forEachComponentType<ComponentTypes...>(
159 getComponentSet<T>().clear();
164 template <
typename T>
165 static constexpr bool isRegisteredComponent = (std::is_same_v<T, AllComponentTypes> || ...);
167 template <
typename... ComponentTypes,
typename Func>
168 inline void forEachComponentType(Func&& f)
const
171 auto staticAssertAndCall = [&f]<
typename T>()
173 static_assert(isRegisteredComponent<T>,
"Tried to access an unregistered component type in ECS.");
174 f.template operator()<T>();
177 (staticAssertAndCall.template operator()<ComponentTypes>(), ...);
180 template <
typename ComponentType>
181 inline SparseSet<Entity, ComponentType>& getComponentSet()
183 static_assert(isRegisteredComponent<ComponentType>,
"Tried to access an unregistered component type.");
184 return std::get<SparseSet<Entity, ComponentType>>(componentSets);
187 template <
typename ComponentType>
188 inline const SparseSet<Entity, ComponentType>& getComponentSet()
const
190 static_assert(isRegisteredComponent<ComponentType>,
"Tried to access an unregistered component type.");
191 return std::get<SparseSet<Entity, ComponentType>>(componentSets);
Definition registry.hpp:15
ComponentType & getComponent(const Entity entity)
Definition registry.hpp:54
size_t size() const
Definition registry.hpp:117
const ComponentType & getComponent(const Entity entity) const
Definition registry.hpp:62
std::vector< Entity > getEntitiesByComponents() const
Definition registry.hpp:82
void removeComponents(const Entity entity)
Definition registry.hpp:44
void clear()
Definition registry.hpp:154
void addComponent(const Entity entity, const ComponentType &component)
Definition registry.hpp:21
void clear()
Definition registry.hpp:144
bool hasComponent(const Entity entity) const
Definition registry.hpp:70
const std::vector< Entity > & getEntitiesByComponent() const
Definition registry.hpp:76
void removeComponents(const Entity entity)
Definition registry.hpp:34
void removeComponent(const Entity entity)
Definition registry.hpp:28
size_t size() const
Definition registry.hpp:131
EASYS_ENTITY_TYPE Entity
Definition entity.hpp:11