Easys
A minimalist, header-only C++ ECS library for efficient and fuss-free entity and component management.
Loading...
Searching...
No Matches
Public Member Functions | List of all members
Easys::ECS< AllComponentTypes > Class Template Reference

Manages entities and components in an Entity-Component-System architecture. More...

#include <ecs.hpp>

Public Member Functions

 ECS ()
 Initializes the ECS with a predefined maximum number of entities (MAX_ENTITIES).
 
 ECS (const std::set< Entity > &oldEntities)
 Initializes the ECS with a specific set of entities.
 
Entity addEntity ()
 Adds a new entity to the ECS.
 
void removeEntity (const Entity e)
 Removes an entity and all its associated components from the ECS.
 
bool hasEntity (const Entity e) const
 Checks if an entity exists within the ECS.
 
View getEntities () const
 Returns a reference to the set of all entities.
 
template<typename... Ts>
View getEntities () const
 Returns a vector of entities that have all of the specified component types. Use smaller components first for optimal performance.
 
size_t getEntityCount () const
 Returns the total number of active entities in the ECS.
 
template<typename T >
void addComponent (const Entity e, T component)
 Adds a component of type T to an entity.
 
template<typename T , typename Func >
void modifyComponent (const Entity e, Func &&fn)
 Modifies a component of type T for a given entity.
 
template<typename T >
void modifyComponent (const Entity e, T c)
 Modifies a component of type T for a given entity.
 
template<typename T >
void removeComponent (const Entity e)
 Removes a component of type T from an entity.
 
template<typename... Ts>
void removeComponents (const Entity e)
 Removes all components of types Ts from an entity.
 
void removeComponents (const Entity e)
 Removes all components from an entity.
 
template<typename T >
T & getComponent (const Entity e)
 Retrieves a reference to a component of type T from an entity.
 
template<typename T >
const T & getComponent (const Entity e) const
 Retrieves a reference to a component of type T from an entity.
 
template<typename T >
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 not have a component T, return c.
 
template<typename T >
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 not have a component T, return c.
 
template<typename T >
bool hasComponent (const Entity e) const
 Checks if an entity has a component of type T.
 
template<typename... Ts>
size_t getComponentCount () const
 Returns the total count of components of the specified types within the ECS.
 
size_t getComponentCount () const
 
template<typename T >
void clearComponent ()
 Removes component of specific type from all entities within the ECS.
 
template<typename... Ts>
void clearComponents ()
 Removes components of specific types from all entities within the ECS.
 
void clearComponents ()
 
void clear ()
 Clears all entities and components from the ECS.
 

Detailed Description

template<typename... AllComponentTypes>
class Easys::ECS< AllComponentTypes >

Manages entities and components in an Entity-Component-System architecture.

Template Parameters
AllComponentTypesA list of all possible component types that can be used in this ECS instance.

Constructor & Destructor Documentation

◆ ECS() [1/2]

template<typename... AllComponentTypes>
Easys::ECS< AllComponentTypes >::ECS ( )
inline

Initializes the ECS with a predefined maximum number of entities (MAX_ENTITIES).

All entity IDs are initially available for assignment.

◆ ECS() [2/2]

template<typename... AllComponentTypes>
Easys::ECS< AllComponentTypes >::ECS ( const std::set< Entity > &  oldEntities)
inline

Initializes the ECS with a specific set of entities.

This constructor is useful for creating a new ECS instance based on a subset of entities from another instance or a predefined list.

Parameters
entitiesA set of entities to initialize the ECS with.

Member Function Documentation

◆ addComponent()

template<typename... AllComponentTypes>
template<typename T >
void Easys::ECS< AllComponentTypes >::addComponent ( const Entity  e,
component 
)
inline

Adds a component of type T to an entity.

If the entity already has a component of type T, it will be updated with the new value.

Template Parameters
TThe type of the component to add.
Parameters
eThe entity to which the component will be added.
cThe component data to add.

◆ addEntity()

template<typename... AllComponentTypes>
Entity Easys::ECS< AllComponentTypes >::addEntity ( )
inline

Adds a new entity to the ECS.

Returns
The ID of the newly created entity.
Exceptions
std::runtime_errorif the maximum number of entities (MAX_ENTITIES) is reached.

◆ clear()

template<typename... AllComponentTypes>
void Easys::ECS< AllComponentTypes >::clear ( )
inline

Clears all entities and components from the ECS.

Resets the ECS to its initial state, making all entity IDs available again.

◆ clearComponent()

template<typename... AllComponentTypes>
template<typename T >
void Easys::ECS< AllComponentTypes >::clearComponent ( )
inline

Removes component of specific type from all entities within the ECS.

Template Parameters
TA component type to clear.

◆ clearComponents() [1/2]

template<typename... AllComponentTypes>
template<typename... Ts>
void Easys::ECS< AllComponentTypes >::clearComponents ( )
inline

Removes components of specific types from all entities within the ECS.

Template Parameters
TsA variadic list of component types to clear. If template parameters are omitted, all types of components are cleared from all entities.

◆ clearComponents() [2/2]

template<typename... AllComponentTypes>
void Easys::ECS< AllComponentTypes >::clearComponents ( )
inline

◆ getComponent() [1/2]

template<typename... AllComponentTypes>
template<typename T >
T & Easys::ECS< AllComponentTypes >::getComponent ( const Entity  e)
inline

Retrieves a reference to a component of type T from an entity.

Template Parameters
TThe type of the component to retrieve.
Parameters
eThe entity whose component is to be retrieved.
Returns
A mutable reference to the component.

◆ getComponent() [2/2]

template<typename... AllComponentTypes>
template<typename T >
const T & Easys::ECS< AllComponentTypes >::getComponent ( const Entity  e) const
inline

Retrieves a reference to a component of type T from an entity.

Template Parameters
TThe type of the component to retrieve.
Parameters
eThe entity whose component is to be retrieved.
Returns
A immutable reference to the component.

◆ getComponentCount() [1/2]

template<typename... AllComponentTypes>
template<typename... Ts>
size_t Easys::ECS< AllComponentTypes >::getComponentCount ( ) const
inline

Returns the total count of components of the specified types within the ECS.

Template Parameters
TsA variadic list of component types. If template parameters are omitted, it returns the total count of all component types.
Returns
The total number of components of the specified types.

◆ getComponentCount() [2/2]

template<typename... AllComponentTypes>
size_t Easys::ECS< AllComponentTypes >::getComponentCount ( ) const
inline

◆ getComponentOr() [1/2]

template<typename... AllComponentTypes>
template<typename T >
const T & Easys::ECS< AllComponentTypes >::getComponentOr ( const Entity  e,
const T &  c 
) const
inline

Retrieves a reference to a component of type T from an entity. If the entity does not exist or does not have a component T, return c.

Template Parameters
TThe type of the component to retrieve.
Parameters
eThe entity whose component is to be retrieved.
cThe default component value to return, if the entity does not have the component T.
Returns
A mutable reference to the component.

◆ getComponentOr() [2/2]

template<typename... AllComponentTypes>
template<typename T >
T & Easys::ECS< AllComponentTypes >::getComponentOr ( const Entity  e,
T &  c 
)
inline

Retrieves a reference to a component of type T from an entity. If the entity does not exist or does not have a component T, return c.

Template Parameters
TThe type of the component to retrieve.
Parameters
eThe entity whose component is to be retrieved.
cThe default component value to return, if the entity does not have the component T.
Returns
A immutable reference to the component.

◆ getEntities() [1/2]

template<typename... AllComponentTypes>
View Easys::ECS< AllComponentTypes >::getEntities ( ) const
inline

Returns a reference to the set of all entities.

Returns
A constant reference to the set of all entities currently in the ECS.

◆ getEntities() [2/2]

template<typename... AllComponentTypes>
template<typename... Ts>
View Easys::ECS< AllComponentTypes >::getEntities ( ) const
inline

Returns a vector of entities that have all of the specified component types. Use smaller components first for optimal performance.

Template Parameters
TsA variadic list of component types to query for.
Returns
A vector of entities that possess all specified components.

◆ getEntityCount()

template<typename... AllComponentTypes>
size_t Easys::ECS< AllComponentTypes >::getEntityCount ( ) const
inline

Returns the total number of active entities in the ECS.

Returns
The number of entities.

◆ hasComponent()

template<typename... AllComponentTypes>
template<typename T >
bool Easys::ECS< AllComponentTypes >::hasComponent ( const Entity  e) const
inline

Checks if an entity has a component of type T.

Template Parameters
TThe type of the component to check for.
Parameters
eThe entity to check.
Returns
True if the entity has the component, false otherwise.

◆ hasEntity()

template<typename... AllComponentTypes>
bool Easys::ECS< AllComponentTypes >::hasEntity ( const Entity  e) const
inline

Checks if an entity exists within the ECS.

Parameters
eThe entity to check for.
Returns
True if the entity exists, false otherwise.

◆ modifyComponent() [1/2]

template<typename... AllComponentTypes>
template<typename T , typename Func >
void Easys::ECS< AllComponentTypes >::modifyComponent ( const Entity  e,
Func &&  fn 
)
inline

Modifies a component of type T for a given entity.

This function retrieves the component of type T associated with the specified entity and invokes the provided callable function with a reference to that component. The callable can be a lambda, function pointer, or any other callable type. The component can be modified directly through the callable.

Template Parameters
TThe type of the component to modify.
Parameters
eThe entity whose component will be modified.
fnThe callable that will be used to modify the component. It should accept a reference to the component of type T.

◆ modifyComponent() [2/2]

template<typename... AllComponentTypes>
template<typename T >
void Easys::ECS< AllComponentTypes >::modifyComponent ( const Entity  e,
c 
)
inline

Modifies a component of type T for a given entity.

This function retrieves the component of type T associated with the specified entity and replaces it with the new component provided as the argument c.

Template Parameters
TThe type of the component to modify.
Parameters
eThe entity whose component will be modified.
cThe new component of type T that will replace the existing component.

◆ removeComponent()

template<typename... AllComponentTypes>
template<typename T >
void Easys::ECS< AllComponentTypes >::removeComponent ( const Entity  e)
inline

Removes a component of type T from an entity.

Template Parameters
TThe type of the component to remove.
Parameters
eThe entity from which to remove the component.

◆ removeComponents() [1/2]

template<typename... AllComponentTypes>
template<typename... Ts>
void Easys::ECS< AllComponentTypes >::removeComponents ( const Entity  e)
inline

Removes all components of types Ts from an entity.

Template Parameters
TThe types of the components to remove.
Parameters
eThe entity from which to remove the components.

◆ removeComponents() [2/2]

template<typename... AllComponentTypes>
void Easys::ECS< AllComponentTypes >::removeComponents ( const Entity  e)
inline

Removes all components from an entity.

Parameters
eThe entity from which to remove all components.

◆ removeEntity()

template<typename... AllComponentTypes>
void Easys::ECS< AllComponentTypes >::removeEntity ( const Entity  e)
inline

Removes an entity and all its associated components from the ECS.

The removed entity's ID is made available for reuse.

Parameters
eThe entity to remove.

The documentation for this class was generated from the following file: