Engine²
Open-source game engine written in C++.
Loading...
Searching...
No Matches
Engine::Entity Class Reference

Wrapper class providing a convenient interface for entity manipulation with the Core. Entity acts as a handle to an entity in the registry, combining a Core reference with an EntityId. It provides utility methods to add, remove, and query components, and maintains context relative to the Core it belongs to. More...

#include <Entity.hpp>

Public Member Functions

 Entity (Core &core, EntityId entityId)
 Create an Entity from EntityId and the Core it belongs to.
 ~Entity ()=default
 Default destructor for Entity. It does not perform any special cleanup, as the Core is responsible for managing the entity's lifecycle.
bool IsAlive () const
 Check if the entity is alive.
EntityId Id () const
 Get the EntityId associated with this Entity.
 explicit (false) operator EntityId() const
 Implicit conversion operator to EntityId.
void Kill ()
 Kill the entity, removing all components and making it invalid. After calling this method, the EntityId will no longer correspond to a valid entity in the Core's registry, and the Entity will be considered dead.
template<typename TComponent>
decltype(auto) AddComponent (TComponent &&component)
 Add a component to an entity.
template<typename TComponent, typename... TArgs>
decltype(auto) AddComponent (TArgs &&...args)
 Add a component to an entity.
template<typename TComponent, typename... TArgs>
decltype(auto) AddComponentIfNotExists (TArgs &&...args)
 Add a component to an entity if it does not already exist.
template<typename TTempComponent, typename... TArgs>
decltype(auto) AddTemporaryComponent (TArgs &&...args)
 Add a temporary component to an entity. Temporary component are removed when calling RemoveTemporaryComponents system.
template<typename TComponent>
void RemoveComponent ()
 Remove a component from an entity.
template<typename... TComponent>
bool HasComponents () const
 Check if entity have one or multiple component's type.
template<typename... TComponent>
decltype(auto) GetComponents ()
 Get components of type TComponent from the entity.
template<typename... TComponent>
decltype(auto) GetComponents () const
 Get components of type TComponent from the entity.
template<typename TComponent>
decltype(auto) TryGetComponent ()
 Try to get a component of type TComponent from the entity. It returns a pointer to the component if it exists, or nullptr if it does not exist.
bool operator== (const Entity &rhs) const
 Equality operator for Entity. It compares the underlying EntityId values to determine if two Entity instances refer to the same entity in the registry.
bool operator== (const EntityId &rhs) const
 Equality operator for Entity and EntityId. It compares the underlying EntityId value of the Entity with the EntityId to determine if they refer to the same entity in the registry.

Static Public Member Functions

static void RemoveTemporaryComponents (Core &core)
 Remove all temporary components from the registry.

Private Member Functions

CoreGetCore () const
 Get a reference to the Core instance that this Entity belongs to. This is used internally to access the registry and other resources when manipulating components.

Private Attributes

std::reference_wrapper< Core_core
 A reference to the Core instance that this Entity belongs to.
EntityId _entityId
 The EntityId that identifies the entity in the Core's registry. This is the underlying identifier for the entity that this Entity class wraps around, and is used for all operations that manipulate the entity in the registry.

Detailed Description

Wrapper class providing a convenient interface for entity manipulation with the Core. Entity acts as a handle to an entity in the registry, combining a Core reference with an EntityId. It provides utility methods to add, remove, and query components, and maintains context relative to the Core it belongs to.

See also
Engine::Core
Engine::EntityId

Constructor & Destructor Documentation

◆ Entity()

Engine::Entity::Entity ( Core & core,
EntityId entityId )

Create an Entity from EntityId and the Core it belongs to.

Parameters
coreReference to the Core instance that the Entity belongs to.
entityIdThe EntityId that identifies the entity in the Core's registry.

◆ ~Entity()

Engine::Entity::~Entity ( )
default

Default destructor for Entity. It does not perform any special cleanup, as the Core is responsible for managing the entity's lifecycle.

Member Function Documentation

◆ AddComponent() [1/2]

template<typename TComponent, typename... TArgs>
decltype(auto) Engine::Entity::AddComponent ( TArgs &&... args)

Add a component to an entity.

Template Parameters
TComponentThe type of the component to add to the registry.
TArgsThe types of the arguments to construct the component in-place in the registry.
Parameters
argsThe arguments to construct the component in-place in the registry.
Returns
A reference to the added component.

◆ AddComponent() [2/2]

template<typename TComponent>
decltype(auto) Engine::Entity::AddComponent ( TComponent && component)

Add a component to an entity.

Template Parameters
TComponentThe type of the component to add to the registry.
Parameters
componentThe rvalue of the component to add to the registry.
Returns
A reference to the added component.
Examples
Creating a 2-unit jelly cube with 5x5x5 grid:, Creating a cloth for soft body simulation:, Creating a cube entity:, Creating a cylinder entity:, Creating a plane entity:, Creating a rope for soft body simulation:, and Creating a sphere entity:.

◆ AddComponentIfNotExists()

template<typename TComponent, typename... TArgs>
decltype(auto) Engine::Entity::AddComponentIfNotExists ( TArgs &&... args)

Add a component to an entity if it does not already exist.

Template Parameters
TComponenttype to add to registry.
TArgstype used to create the component.
Parameters
argsparameters used to instanciate component directly in registry memory.
Returns
reference of the added component.

◆ AddTemporaryComponent()

template<typename TTempComponent, typename... TArgs>
decltype(auto) Engine::Entity::AddTemporaryComponent ( TArgs &&... args)

Add a temporary component to an entity. Temporary component are removed when calling RemoveTemporaryComponents system.

Template Parameters
TTempComponenttype to add to registry
TArgstype used to create the component
Parameters
argsparameters used to instanciate component directly in registry memory
Returns
reference of the added component
See also
Engine::EntityId::RemoveTemporaryComponents

◆ explicit()

Engine::Entity::explicit ( false ) const

Implicit conversion operator to EntityId.

Returns
The EntityId of this Entity.

◆ GetComponents() [1/2]

template<typename... TComponent>
decltype(auto) Engine::Entity::GetComponents ( )

Get components of type TComponent from the entity.

Template Parameters
TComponentcomponents to get.
Returns
components of type TComponent from the entity.

◆ GetComponents() [2/2]

template<typename... TComponent>
decltype(auto) Engine::Entity::GetComponents ( ) const

Get components of type TComponent from the entity.

Template Parameters
TComponentcomponents to get.
Returns
components of type TComponent from the entity.

◆ GetCore()

Core & Engine::Entity::GetCore ( ) const
private

Get a reference to the Core instance that this Entity belongs to. This is used internally to access the registry and other resources when manipulating components.

Returns
A reference to the Core instance.

◆ HasComponents()

template<typename... TComponent>
bool Engine::Entity::HasComponents ( ) const

Check if entity have one or multiple component's type.

Template Parameters
TComponentcomponents to check.
Returns
true if entity have all requested component, false otherwise.

◆ Id()

EntityId Engine::Entity::Id ( ) const

Get the EntityId associated with this Entity.

Returns
The EntityId of this Entity.

◆ IsAlive()

bool Engine::Entity::IsAlive ( ) const

Check if the entity is alive.

Returns
true if the entity is alive, false otherwise.

◆ Kill()

void Engine::Entity::Kill ( )

Kill the entity, removing all components and making it invalid. After calling this method, the EntityId will no longer correspond to a valid entity in the Core's registry, and the Entity will be considered dead.

◆ operator==() [1/2]

bool Engine::Entity::operator== ( const Entity & rhs) const

Equality operator for Entity. It compares the underlying EntityId values to determine if two Entity instances refer to the same entity in the registry.

Parameters
rhsThe other Entity to compare with.
Returns
true if the entities are equal, false otherwise.

◆ operator==() [2/2]

bool Engine::Entity::operator== ( const EntityId & rhs) const

Equality operator for Entity and EntityId. It compares the underlying EntityId value of the Entity with the EntityId to determine if they refer to the same entity in the registry.

Parameters
rhsThe EntityId to compare with.
Returns
true if the Entity and EntityId are equal, false otherwise.

◆ RemoveComponent()

template<typename TComponent>
void Engine::Entity::RemoveComponent ( )

Remove a component from an entity.

Template Parameters
TComponentThe type of the component to remove from the registry.

◆ RemoveTemporaryComponents()

void Engine::Entity::RemoveTemporaryComponents ( Core & core)
static

Remove all temporary components from the registry.

Parameters
coreThe Core instance whose registry is used to store the component.
See also
Engine::EntityId::AddTemporaryComponent

◆ TryGetComponent()

template<typename TComponent>
decltype(auto) Engine::Entity::TryGetComponent ( )

Try to get a component of type TComponent from the entity. It returns a pointer to the component if it exists, or nullptr if it does not exist.

Template Parameters
TComponentThe type of the component to get from the entity.
Returns
The component of type TComponent from the entity if it exists, or nullptr if it does not exist.

Member Data Documentation

◆ _core

std::reference_wrapper<Core> Engine::Entity::_core
private

A reference to the Core instance that this Entity belongs to.

◆ _entityId

EntityId Engine::Entity::_entityId
private

The EntityId that identifies the entity in the Core's registry. This is the underlying identifier for the entity that this Entity class wraps around, and is used for all operations that manipulate the entity in the registry.


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