20template <
typename... AllComponentTypes>
33 availableEntityIds_.push(entity);
43 ECS(
const std::set<Entity>& oldEntities)
52 if (oldEntities.contains(entity))
54 entities_.insert(entity);
57 availableEntityIds_.push(entity);
73 Entity e = availableEntityIds_.front();
74 availableEntityIds_.pop();
83 throw std::runtime_error(
"MAX NUMBER OF ENTITIES REACHED!");
100 availableEntityIds_.push(e);
114 return entities_.contains(e);
132 std::vector<Entity> vec;
133 vec.reserve(entities_.size());
134 vec.assign(entities_.begin(), entities_.end());
136 return View(std::move(vec));
154 template <
typename... Ts>
159 return View(std::move(registry_.template getEntities<Ts...>()));
170 return entities_.size();
180 template <
typename T>
203 template <
typename T,
typename Func>
209 T& c = getComponent<T>(e);
224 template <
typename T>
230 getComponent<T>(e) = c;
241 template <
typename T>
246 registry_.template removeComponent<T>(e);
256 template <
typename... Ts>
261 (removeComponent<Ts>(e), ...);
282 template <
typename T>
287 return registry_.template getComponent<T>(e);
296 template <
typename T>
301 return registry_.template getComponent<T>(e);
312 template <
typename T>
317 if (hasComponent<T>(e))
319 return registry_.template getComponent<T>(e);
332 template <
typename T>
337 if (hasComponent<T>(e))
339 return registry_.template getComponent<T>(e);
350 template <
typename T>
355 return registry_.template hasComponent<T>(e);
364 template <
typename... Ts>
369 return registry_.template size<Ts...>();
384 template <
typename T>
389 registry_.template clear<T>();
391 EASYS_LOG_INFO(std::format(
"Clearing all components of type {}",
typeid(T).name()));
399 template <
typename... Ts>
405 (clearComponent<Ts>(), ...);
412 (clearComponent<AllComponentTypes>(), ...);
429 std::queue<Entity> availableEntityIds_;
430 std::set<Entity> entities_;
431 Registry<AllComponentTypes...> registry_;
439 std::queue<Entity> empty;
440 std::swap(availableEntityIds_, empty);
444 availableEntityIds_.push(entity);
Manages entities and components in an Entity-Component-System architecture.
Definition ecs.hpp:21
View getEntities() const
Returns a vector of entities that have all of the specified component types. Use smaller components f...
Definition ecs.hpp:155
void addComponent(const Entity e, T component)
Adds a component of type T to an entity.
Definition ecs.hpp:181
Entity addEntity()
Adds a new entity to the ECS.
Definition ecs.hpp:67
void modifyComponent(const Entity e, Func &&fn)
Modifies a component of type T for a given entity.
Definition ecs.hpp:204
void clearComponent()
Removes component of specific type from all entities within the ECS.
Definition ecs.hpp:385
void clearComponents()
Definition ecs.hpp:408
const T & getComponent(const Entity e) const
Retrieves a reference to a component of type T from an entity.
Definition ecs.hpp:297
size_t getEntityCount() const
Returns the total number of active entities in the ECS.
Definition ecs.hpp:166
size_t getComponentCount() const
Returns the total count of components of the specified types within the ECS.
Definition ecs.hpp:365
void removeEntity(const Entity e)
Removes an entity and all its associated components from the ECS.
Definition ecs.hpp:91
void clear()
Clears all entities and components from the ECS.
Definition ecs.hpp:420
T & getComponent(const Entity e)
Retrieves a reference to a component of type T from an entity.
Definition ecs.hpp:283
size_t getComponentCount() const
Definition ecs.hpp:372
ECS(const std::set< Entity > &oldEntities)
Initializes the ECS with a specific set of entities.
Definition ecs.hpp:43
bool hasEntity(const Entity e) const
Checks if an entity exists within the ECS.
Definition ecs.hpp:110
View getEntities() const
Returns a reference to the set of all entities.
Definition ecs.hpp:128
const T & getComponentOr(const Entity e, const T &c) const
Retrieves a reference to a component of type T from an entity. If the entity does not exist or does n...
Definition ecs.hpp:333
void clearComponents()
Removes components of specific types from all entities within the ECS.
Definition ecs.hpp:400
ECS()
Initializes the ECS with a predefined maximum number of entities (MAX_ENTITIES).
Definition ecs.hpp:27
void removeComponents(const Entity e)
Removes all components from an entity.
Definition ecs.hpp:268
void removeComponent(const Entity e)
Removes a component of type T from an entity.
Definition ecs.hpp:242
T & getComponentOr(const Entity e, T &c)
Retrieves a reference to a component of type T from an entity. If the entity does not exist or does n...
Definition ecs.hpp:313
bool hasComponent(const Entity e) const
Checks if an entity has a component of type T.
Definition ecs.hpp:351
void removeComponents(const Entity e)
Removes all components of types Ts from an entity.
Definition ecs.hpp:257
void modifyComponent(const Entity e, T c)
Modifies a component of type T for a given entity.
Definition ecs.hpp:225
Definition registry.hpp:15
void removeComponents(const Entity entity)
Definition registry.hpp:48
void clear()
Definition registry.hpp:135
void addComponent(const Entity entity, const ComponentType &component)
Definition registry.hpp:21
A lightweight, read-only snapshot of a set of entities.
Definition view.hpp:30
#define EASYS_LOG_ENTRY_EXIT
Definition log.hpp:184
#define EASYS_LOG_ERROR(msg)
Definition log.hpp:180
#define EASYS_E_STR(e)
Definition log.hpp:193
#define EASYS_LOG_INFO(msg)
Definition log.hpp:181
#define EASYS_EC_STR(e)
Definition log.hpp:194
const Entity MAX_ENTITIES
Definition entity.hpp:12
EASYS_ENTITY_TYPE Entity
Definition entity.hpp:11