CeresEngine 0.2.0
A game development framework
Loading...
Searching...
No Matches
CeresEngine::EntityObject< Components > Class Template Reference

A type-safe entity type. More...

#include <CeresEngine/Entity/Entity.hpp>

Inheritance diagram for CeresEngine::EntityObject< Components >:
CeresEngine::Entity

Classes

struct  _MaskObject
 A trait that contains the minimum necessary component mask for an entity to be an entity object of this type. More...
 

Public Types

using ComponentSet = CeresEngine::ComponentSet< Components... >
 The component set for the entity object.
 
using Type = EntityObject< Components... >
 Underlying EntityObject.
 

Public Member Functions

 EntityObject ()=default
 Creates a new entity object.
 
 EntityObject (std::nullptr_t) noexcept
 Creates a new empty entity.
 
 EntityObject (EntityManager &entityManager)
 Creates a new entity object attached to a entity manager.
 
 EntityObject (EntityManager &entityManager, const Entity &parent)
 Creates a new entity object attached to a entity manager.
 
 EntityObject (EntityManager &entityManager, String name)
 Creates a new entity object attached to a entity manager.
 
 EntityObject (EntityManager &entityManager, const Entity &parent, String name)
 Creates a new entity object attached to a entity manager.
 
template<typename... OtherComponents>
requires (EntityObject<OtherComponents...>::ComponentSet::template includes<Components>&&...)
 EntityObject (const EntityObject< OtherComponents... > &entity)
 Creates a new entity object by down-casting other compatible entity object.
 
 EntityObject (const EntityObject &other) noexcept=default
 Creates a new entity by copying another.
 
EntityObjectoperator= (const EntityObject &other) noexcept=default
 Assing the contents of the entity by copying the contents of another.
 
CE_FLATTEN_INLINE decltype(autooperator-> () const
 Creates a new ComponentAccessor for this entity object.
 
UInt64 getHash () const
 Computes a hash that uniquely represents the entity object.
 
bool compareHash (UInt64 &hash) const
 
- Public Member Functions inherited from CeresEngine::Entity
 Entity () noexcept=default
 Creates a new empty entity.
 
 Entity (EntityManager &entityManager, EntityID id) noexcept
 Creates a new entity attached to a entity manager.
 
 Entity (std::nullptr_t) noexcept
 Creates a new empty entity.
 
Entityoperator= (std::nullptr_t) noexcept
 Empties a EntityObject. The entity is not deallocated.
 
 Entity (const Entity &other) noexcept=default
 Creates a new entity by copying another.
 
Entityoperator= (const Entity &other) noexcept=default
 Assing the contents of the entity by copying the contents of another.
 
bool valid () const noexcept
 Checks if the entity is still valid.
 
Entity copy () const
 Copies the entity and all its components.
 
template<typename Block >
Entity copy (Block &&block) const
 Copies the entity and all its components. Executes block as.
 
void destroy () const
 Destroys the entity.
 
AbstractComponentadd (const ComponentType &type, const Box &initialValue=nullptr) const
 Adds a new component of the given type.
 
template<CComponent C, typename... Args>
Cadd (Args &&... args) const noexcept(std::is_nothrow_constructible_v< C, Args... >)
 Adds a new component.
 
template<CComponent C, typename Block , typename... Args>
CaddWith (Block &&block, Args &&... args) const noexcept(std::is_nothrow_constructible_v< C, Args... >)
 Adds a new component and calls block with the added component.
 
template<CComponent C, typename... Args>
Cset (Args &&... args) const noexcept(std::is_nothrow_constructible_v< C, Args... >)
 Sets a component.
 
void remove (const ComponentType &type) const
 Removes a component of the given type from the entity.
 
template<CComponent C>
void remove () const noexcept(std::is_nothrow_destructible_v< C >)
 Removes a component of type C from the entity.
 
AbstractComponentget (const ComponentType &type) const
 Gets a component of the given type.
 
template<CComponent C>
Cget () const noexcept
 Gets a component of type C.
 
template<CComponent C0, CComponent C1, CComponent... Cs>
Tuple< C0 &, C1 &, Cs &... > get () const noexcept
 Gets a set of components of types C0, C1 and Cs.
 
AbstractComponentgetOr (const ComponentType &type) const
 Gets a component of the given type.
 
template<CComponent C>
CgetOr () const noexcept(std::is_nothrow_constructible_v< C >)
 Gets a component of type C.
 
template<CComponent C0, CComponent C1, CComponent... Cs>
Tuple< C0 &, C1 &, Cs &... > getOr () const noexcept
 Gets a set of components of types C0, C1 and Cs.
 
AbstractComponentgetIf (const ComponentType &type) const
 Gets a component of the given type.
 
template<CComponent C>
CgetIf () const noexcept
 Gets a component of type C.
 
template<CComponent C0, CComponent C1, CComponent... Cs>
Tuple< C0 *, C1 *, Cs *... > getIf () const noexcept
 Gets a set of components of types C0, C1 and Cs.
 
Vector< AbstractComponent * > getComponents () const
 
bool has (const ComponentType &type) const noexcept
 Checks if the entity has all components of the given type.
 
template<CComponent C, CComponent... Cs>
bool has () const noexcept
 Checks if the entity has all components of types Cs.
 
void clear () const
 Removes all components attached to the entity, without destroying the entity itself.
 
bool empty () const noexcept
 Checks if the entity is empty (i.e.
 
template<typename E >
bool is () const noexcept
 Checks if the entity matches the criteria to be considered a E entity object.
 
template<typename E >
E as () const
 Converts the entity handle type into a E entity object.
 
template<typename... Cs>
EntityObject< Cs... > having () const
 Converts the entity handle type into a EntityObject<Cs...> object.
 
template<typename E >
E being () const noexcept
 Converts the entity handle type into a E entity object.
 
Entity getParent () const noexcept
 Gets the parent entity.
 
template<typename P >
P getParent () const noexcept
 Gets the parent entity of type T.
 
void setParent (Entity parent) const
 Sets the parent entity.
 
const Vector< Entity > & getChildren () const noexcept
 Gets the list of children entities.
 
template<typename T >
Generator< TgetChildren () const noexcept
 Gets the list of children entities.
 
void setChildren (const Vector< Entity > &children)
 Sets all entities as children of this entity.
 
StringView getName () const noexcept
 Gets the name for the entity.
 
void setName (String name) const
 Sets the name for the entity.
 
template<CEntityEvent E, typename... Args>
void emit (Args &&... args) const
 Emits a event of type E to all event listeners.
 
template<CEntityEvent E>
void emit (const E &event) const
 Emits a event of type E to all event listeners.
 
template<CEntityEvent E, typename Listener >
WeakEventConnection on (Listener &&listener) const
 Subscribes a listener that waits for events from the entity.
 
template<CEntityAction A, typename... Args>
bool canAct (Args &&... args) const
 Checks if the the entity is capable of acting on the given action A.
 
template<CEntityAction A, typename... Args>
decltype(autoact (Args &&... args) const
 Orders the entity to act on the action A.
 
template<typename T , CComponent Component>
auto mutate (T Component::*ptr) const
 Accesses an element from an existing Component.
 
template<typename MutateFunc , typename T , CComponent Component>
decltype(automutate (MutateFunc &&func, T Component::*ptr) const
 Accesses an element from an existing Component.
 
template<typename T , CComponent Component>
const Tread (const T Component::*ptr) const
 Accesses an element from an existing Component.
 
EntityID getEntityID () const noexcept
 
EntityManagergetEntityManager () const noexcept
 
 operator bool () const noexcept
 Checks if the entity is still valid.
 
Entity operator[] (const EntityIndex childIndex) const
 
Entity operator[] (const StringView childName) const
 

Static Public Member Functions

static EntityObject make (Entity entity)
 Makes the entity an entity object by adding any missing component and casting it.
 

Static Public Attributes

template<typename C >
static constexpr bool includes = ComponentSet::template includes<C>
 Checks if all Components are valid component types.
 
static const _MaskObject mask
 

Private Member Functions

 EntityObject (EntityManager &entityManager, EntityID entityID)
 Creates a new entity object attached to a entity manager.
 

Friends

class EntityManager
 
template<CComponent... ACs>
std::ostream & operator<< (std::ostream &os, const EntityObject< ACs... > &entity)
 Compares two entities for equality.
 

Additional Inherited Members

- Public Attributes inherited from CeresEngine::Entity
EntityManagerentityManager = nullptr
 The owning entity manager.
 
EntityID entityID = {}
 The entity ID.
 

Detailed Description

template<CComponent... Components>
class CeresEngine::EntityObject< Components >

A type-safe entity type.

This type can be used as-is or subclassed to offer higher type-safe guarantees. Subclasses however cannot add new data members to the entity as those won't be saved in the entity.

This class can act as a kind of mixin class than can be used to create entities that obligatorily have the given component types. The entity data can be read or mutated through it's operator->() that returns a special type that combines all Component::Accessor types for all the given Components.

It is guaranteed that a given EntityObject instance has all of the given components. Access to the underlying components can be done using the get() method although that is discouraged in favour of using the higher-level Component::Accessor interface using operator->().

The EntityObject instance itself doesn't store any data other than a reference to the owning EntityManager and it's EntityID. All data storage and persistence is delegated to a set of Components.

If defining a custom component type it is also recommended to implement a Component::Accessor type. This allows entity objects that use that component to offer a richer interface for users while delegating the internals of component data management to the Accessor implementation.

An entity object can be hashed if all it's 'Components' are also hashable. The hash can be calculated by calling getHash(). A helper function to check the previous hash and computing a new one is also available as compareHash() and can be used by systems such as the renderer to determine if an entity has changed since the last call.

Template Parameters
ComponentsThe components required by the entity object.

Member Typedef Documentation

◆ ComponentSet

template<CComponent... Components>
using CeresEngine::EntityObject< Components >::ComponentSet = CeresEngine::ComponentSet<Components...>

The component set for the entity object.

◆ Type

template<CComponent... Components>
using CeresEngine::EntityObject< Components >::Type = EntityObject<Components...>

Underlying EntityObject.

Used for creating Entity alias without a provided constructor

Constructor & Destructor Documentation

◆ EntityObject() [1/9]

template<CComponent... Components>
CeresEngine::EntityObject< Components >::EntityObject ( )
inlinedefault

Creates a new entity object.

◆ EntityObject() [2/9]

template<CComponent... Components>
CeresEngine::EntityObject< Components >::EntityObject ( std::nullptr_t  )
inlinenoexcept

Creates a new empty entity.

◆ EntityObject() [3/9]

template<CComponent... Components>
CeresEngine::EntityObject< Components >::EntityObject ( EntityManager entityManager)
inlineexplicit

Creates a new entity object attached to a entity manager.

Parameters
entityManagerThe entity manager owning the entity

◆ EntityObject() [4/9]

template<CComponent... Components>
CeresEngine::EntityObject< Components >::EntityObject ( EntityManager entityManager,
const Entity parent 
)
inlineexplicit

Creates a new entity object attached to a entity manager.

Parameters
entityManagerThe entity manager owning the entity

◆ EntityObject() [5/9]

template<CComponent... Components>
CeresEngine::EntityObject< Components >::EntityObject ( EntityManager entityManager,
String  name 
)
inlineexplicit

Creates a new entity object attached to a entity manager.

Parameters
entityManagerThe entity manager owning the entity

◆ EntityObject() [6/9]

template<CComponent... Components>
CeresEngine::EntityObject< Components >::EntityObject ( EntityManager entityManager,
const Entity parent,
String  name 
)
inlineexplicit

Creates a new entity object attached to a entity manager.

Parameters
entityManagerThe entity manager owning the entity

◆ EntityObject() [7/9]

template<CComponent... Components>
template<typename... OtherComponents>
requires (EntityObject<OtherComponents...>::ComponentSet::template includes<Components>&&...)
CeresEngine::EntityObject< Components >::EntityObject ( const EntityObject< OtherComponents... > &  entity)
inline

Creates a new entity object by down-casting other compatible entity object.

Template Parameters
OtherComponentsThe components attached to the other entity object.
Parameters
entityThe entity object to be down-casted from.

◆ EntityObject() [8/9]

template<CComponent... Components>
CeresEngine::EntityObject< Components >::EntityObject ( const EntityObject< Components > &  other)
inlinedefaultnoexcept

Creates a new entity by copying another.

NOTE: this will only copy the entity handle, the components and the entity ID will still be the same.

Parameters
otherThe entity to copy from

◆ EntityObject() [9/9]

template<CComponent... Components>
CeresEngine::EntityObject< Components >::EntityObject ( EntityManager entityManager,
EntityID  entityID 
)
inlineprivate

Creates a new entity object attached to a entity manager.

Parameters
entityManagerThe entity manager owning the entity
entityIDThe entity ID

Member Function Documentation

◆ compareHash()

template<CComponent... Components>
bool CeresEngine::EntityObject< Components >::compareHash ( UInt64 hash) const
inline

◆ getHash()

template<CComponent... Components>
UInt64 CeresEngine::EntityObject< Components >::getHash ( ) const
inline

Computes a hash that uniquely represents the entity object.

Returns
A hash that uniquely represents the object.

◆ make()

template<CComponent... Components>
static EntityObject CeresEngine::EntityObject< Components >::make ( Entity  entity)
inlinestatic

Makes the entity an entity object by adding any missing component and casting it.

Parameters
entityThe entity to be made an entity object
Returns
The made entity object.

◆ operator->()

template<CComponent... Components>
CE_FLATTEN_INLINE decltype(auto) CeresEngine::EntityObject< Components >::operator-> ( ) const
inline

Creates a new ComponentAccessor for this entity object.

Returns
A component accessor for the entity object.
Returns
this. This allows the usage of operator-> when returning a ComponentAccessor by value.

◆ operator=()

template<CComponent... Components>
EntityObject & CeresEngine::EntityObject< Components >::operator= ( const EntityObject< Components > &  other)
inlinedefaultnoexcept

Assing the contents of the entity by copying the contents of another.

NOTE: this will only copy the entity handle, the components and the entity ID will still be the same.

Parameters
otherThe entity to copy from
Returns
This

Friends And Related Symbol Documentation

◆ EntityManager

template<CComponent... Components>
friend class EntityManager
friend

◆ operator<<

template<CComponent... Components>
template<CComponent... ACs>
std::ostream & operator<< ( std::ostream &  os,
const EntityObject< ACs... > &  entity 
)
friend

Compares two entities for equality.

Two entity are considered equal iff their index and version match and they are managed by the same entity manager.

Parameters
aThe left hand side operator argument
bThe right hand side operator argument
Returns
True if the two entities can be considered equal Compares two entities for inequality. Two entity are considered equal iff their index and version match and they are managed by the same entity manager.
Parameters
aThe left hand side operator argument
bThe right hand side operator argument
Returns
True if the two IDs can be considered equal Compares two entities for equality. Two entity are considered equal iff their index and version match and they are managed by the same entity manager.
Parameters
aThe left hand side operator argument
bThe right hand side operator argument
Returns
True if the two entities can be considered equal Compares two entities for inequality. Two entity are considered equal iff their index and version match and they are managed by the same entity manager.
Parameters
aThe left hand side operator argument
bThe right hand side operator argument
Returns
True if the two IDs can be considered equal Compares two entities for equality. Two entity are considered equal iff their index and version match and they are managed by the same entity manager.
Parameters
aThe left hand side operator argument
bThe right hand side operator argument
Returns
True if the two entities can be considered equal Compares two entities for inequality. Two entity are considered equal iff their index and version match and they are managed by the same entity manager.
Parameters
aThe left hand side operator argument
bThe right hand side operator argument
Returns
True if the two IDs can be considered equal Checks if a entity is empty.
Parameters
aThe left hand side operator argument
bThe right hand side operator argument
Returns
True if the entity is empty Prints a string representation of the Entity
Parameters
osThe output stream to print to
entityThe entity ID to be printed
Returns
The given os

Member Data Documentation

◆ includes

template<CComponent... Components>
template<typename C >
constexpr bool CeresEngine::EntityObject< Components >::includes = ComponentSet::template includes<C>
inlinestaticconstexpr

Checks if all Components are valid component types.

A trait that checks if the component type C is included in the entity object type

Template Parameters
Cthe component type

◆ mask

template<CComponent... Components>
const _MaskObject CeresEngine::EntityObject< Components >::mask
inlinestatic

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