wPlot2D version: 0.1.0
A lightweight C++ library for clear and customizable 2D scientific plots.
Loading...
Searching...
No Matches
wEngine::Entity Class Reference

Represents an entity in the ECS (Entity-Component System) architecture. More...

#include <wEntity.hpp>

Inheritance diagram for wEngine::Entity:
Inheritance graph

Public Member Functions

 Entity ()
virtual ~Entity ()
unsigned int getEntityID () const
 Returns the unique ID associated with this entity.
void clearComponents ()
 Removes all components currently attached to the entity.
template<typename T, typename... Args>
std::shared_ptr< T > addComponent (Args &&... args)
 Adds a new component of type T to the entity.
template<typename T>
void removeComponent ()
 Removes the component of type T from the entity.
template<typename T>
bool hasComponent () const noexcept
 Checks whether the entity has a component of type T.
template<typename T>
std::shared_ptr< T > getComponent () const
 Retrieves the component of type T attached to the entity.
template<typename T>
std::shared_ptr< T > requireComponent (const std::string &context="") const
 Retrieves the component of type T and throws if it's missing.
template<typename Interface>
std::shared_ptr< Interface > getInterfaceComponent () const
 Returns the first component that implements the specified interface.

Static Public Member Functions

static void resetEntityIDCounter ()
 Resets the global entity ID counter to zero.

Detailed Description

Represents an entity in the ECS (Entity-Component System) architecture.

Each entity is uniquely identified and can dynamically manage a collection of components. Components are stored in a type-safe map and accessed by type.

The class provides utility methods to add, remove, retrieve and query components, as well as retrieve components through interfaces.

Note
Entities do not define behavior directly: behavior is defined by the components attached to them.
Author
Wilfried Koch

Constructor & Destructor Documentation

◆ Entity()

wEngine::Entity::Entity ( )

◆ ~Entity()

wEngine::Entity::~Entity ( )
virtual

Member Function Documentation

◆ getEntityID()

unsigned int wEngine::Entity::getEntityID ( ) const
nodiscard

Returns the unique ID associated with this entity.

Returns
Unsigned integer representing the entity's ID.

◆ clearComponents()

void wEngine::Entity::clearComponents ( )

Removes all components currently attached to the entity.

◆ resetEntityIDCounter()

void wEngine::Entity::resetEntityIDCounter ( )
static

Resets the global entity ID counter to zero.

This affects all subsequently created entities. Use with caution, especially in multi-entity systems.

◆ addComponent()

template<typename T, typename... Args>
std::shared_ptr< T > wEngine::Entity::addComponent ( Args &&... args)
inline

Adds a new component of type T to the entity.

Constructs the component using the provided arguments and attaches it to the entity.

Template Parameters
TComponent type, must inherit from wEngine::Component.
ArgsVariadic arguments used to construct the component.
Parameters
argsConstructor arguments forwarded to the component.
Returns
Shared pointer to the newly created component.
Exceptions
std::runtime_errorif a component of the same type already exists in the entity.

◆ removeComponent()

template<typename T>
void wEngine::Entity::removeComponent ( )
inline

Removes the component of type T from the entity.

If no such component exists, this operation does nothing.

Template Parameters
TComponent type to remove.

◆ hasComponent()

template<typename T>
bool wEngine::Entity::hasComponent ( ) const
inlinenodiscardnoexcept

Checks whether the entity has a component of type T.

Template Parameters
TComponent type to check.
Returns
True if the component is present, false otherwise.

◆ getComponent()

template<typename T>
std::shared_ptr< T > wEngine::Entity::getComponent ( ) const
inlinenodiscard

Retrieves the component of type T attached to the entity.

Template Parameters
TComponent type to retrieve.
Returns
Shared pointer to the component if found, or nullptr otherwise.

◆ requireComponent()

template<typename T>
std::shared_ptr< T > wEngine::Entity::requireComponent ( const std::string & context = "") const
inlinenodiscard

Retrieves the component of type T and throws if it's missing.

This method is similar to getComponent( ), but throws a std::runtime_error if the component is not found. Useful for critical systems where components must be present.

Template Parameters
TThe type of the component.
Parameters
contextOptional string to specify the context of the call (e.g., method name).
Returns
A shared pointer to the required component.
Exceptions
std::runtime_errorif the component is not found.

◆ getInterfaceComponent()

template<typename Interface>
std::shared_ptr< Interface > wEngine::Entity::getInterfaceComponent ( ) const
inlinenodiscard

Returns the first component that implements the specified interface.

Checks all components attached to the entity using dynamic casting. If a component matches the given interface type, it is returned.

Template Parameters
InterfaceThe desired interface type.
Returns
A shared pointer to the matching component, or nullptr if none found.

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