NAP
Public Types | Public Member Functions | List of all members
EntityInstance Class Reference

#include <entity.h>

Public Types

using ComponentList = std::vector< std::unique_ptr< ComponentInstance > >
 
using ChildList = std::vector< EntityInstance * >
 
using ComponentIterator = utility::UniquePtrVectorWrapper< ComponentList, ComponentInstance * >
 
using ComponentConstIterator = utility::UniquePtrConstVectorWrapper< ComponentList, ComponentInstance * >
 

Public Member Functions

 EntityInstance (Core &core, const Entity *entity)
 
bool init (Scene &scene, EntityCreationParameters &entityCreationParams, utility::ErrorState &errorState)
 
virtual void onDestroy () override
 
void update (double deltaTime)
 
void addComponent (std::unique_ptr< ComponentInstance > component)
 
ComponentInstancefindComponent (const std::string &type) const
 
ComponentInstancefindComponentByID (const std::string &identifier) const
 
template<class T >
T * findComponentByID (const std::string &identifier) const
 
ComponentInstancefindComponent (const rtti::TypeInfo &type) const
 
template<class T >
T * findComponent () const
 
bool hasComponent (const rtti::TypeInfo &type) const
 
template<class T >
bool hasComponent () const
 
ComponentInstancegetComponent (const rtti::TypeInfo &type) const
 
template<class T >
T & getComponent () const
 
void getComponentsOfType (const rtti::TypeInfo &type, std::vector< ComponentInstance * > &components) const
 
template<class T >
void getComponentsOfType (std::vector< T * > &outComponents) const
 
template<class T >
void getComponentsOfTypeRecursive (std::vector< T * > &outComponents)
 
bool hasComponentsOfType (const rtti::TypeInfo &type) const
 
template<class T >
bool hasComponentsOfType () const
 
void addChild (EntityInstance &child)
 
void clearChildren ()
 
void removeChild (const EntityInstance &entityInstance)
 
const ChildListgetChildren () const
 
EntityInstancegetParent () const
 
const EntitygetEntity () const
 
CoregetCore () const
 
ComponentIterator getComponents ()
 
ComponentConstIterator getComponents () const
 
EntityInstanceoperator[] (std::size_t index)
 
const EntityInstanceoperator[] (std::size_t index) const
 
- Public Member Functions inherited from Object
 Object ()
 
virtual ~Object ()
 
virtual bool init (utility::ErrorState &errorState)
 
 Object (Object &)=delete
 
Objectoperator= (const Object &)=delete
 
 Object (Object &&)=delete
 
Objectoperator= (Object &&)=delete
 

Additional Inherited Members

- Static Public Member Functions inherited from Object
static bool isIDProperty (rtti::Instance &object, const rtti::Property &property)
 
- Public Attributes inherited from Object
std::string mID
 Property: 'mID' unique name of the object. Used as an identifier by the system. More...
 

Description

The runtime counterpart of an Entity which is used to group components and child entities. This class only works with run-time versions of both, ie: ComponentInstance and EntityInstance On construction every entity receives a reference to Core and the Entity it originated from.

Inheritance diagram for EntityInstance:
[legend]
Collaboration diagram for EntityInstance:
[legend]

Member Typedef Documentation

◆ ChildList

using ChildList = std::vector<EntityInstance*>

◆ ComponentConstIterator

◆ ComponentIterator

◆ ComponentList

using ComponentList = std::vector<std::unique_ptr<ComponentInstance> >

Constructor & Destructor Documentation

◆ EntityInstance()

EntityInstance ( Core core,
const Entity entity 
)
Parameters
corethe nap core instance associated with the application
entitythe resource that was used to create this instance, this is null when there is no resource associated with the instance, for example: the root entity

Member Function Documentation

◆ addChild()

void addChild ( EntityInstance child)

Add a child entity to this entity. Ownership is not transfered to this entity.

Parameters
childThe entity to add.

◆ addComponent()

void addComponent ( std::unique_ptr< ComponentInstance component)

Add a component to this entity

Parameters
componentThe component to add. Ownership is transfered to this entity

◆ clearChildren()

void clearChildren ( )

Clear all children from this entity

◆ findComponent() [1/3]

T * findComponent

Convenience template function to find the first component of the specified type

◆ findComponent() [2/3]

ComponentInstance* findComponent ( const rtti::TypeInfo type) const

Finds the first component of the specified type.

Parameters
typethe type of component to find.
Returns
The found component. Null if not found.

◆ findComponent() [3/3]

ComponentInstance* findComponent ( const std::string &  type) const

Finds the first component of the specified type.

Parameters
typeThe type name of the component to find.
Returns
The found component. Null if not found.

◆ findComponentByID() [1/2]

ComponentInstance* findComponentByID ( const std::string &  identifier) const

Finds the first component with the specified ID as declared in JSON.

Parameters
identifierThe name of the component to find.
Returns
the found component. Null if not found.

◆ findComponentByID() [2/2]

T * findComponentByID ( const std::string &  identifier) const

Finds the first component with the specified ID as declared in JSON as type T

Parameters
identifierThe name of the component to find
Returns
the found component. Null if not found or not derived from T

◆ getChildren()

const ChildList& getChildren ( ) const

Get all children of this entity

◆ getComponent() [1/2]

T & getComponent

Convenience function to get a component of the specified type. If multiple components of the matching type exist the first one is returned. Asserts if not found.

Returns
the found component

◆ getComponent() [2/2]

ComponentInstance& getComponent ( const rtti::TypeInfo type) const

Get a component of the specified type. Asserts if not found. If multiple components of the matching type exist the first one is returned

Returns
The component

◆ getComponents() [1/2]

ComponentIterator getComponents ( )
Returns
Non const component iterator

◆ getComponents() [2/2]

ComponentConstIterator getComponents ( ) const
Returns
const component iterator

◆ getComponentsOfType() [1/2]

void getComponentsOfType ( const rtti::TypeInfo type,
std::vector< ComponentInstance * > &  components 
) const

Get all direct entity components of the specified type.

Parameters
typeThe type of the component to find
componentsThe list of components found

◆ getComponentsOfType() [2/2]

void getComponentsOfType ( std::vector< T * > &  outComponents) const

Convenience template function to get all direct child components of the specified type T

Parameters
outComponentsall direct child components of type T, note that this list is not cleared before searching

◆ getComponentsOfTypeRecursive()

void getComponentsOfTypeRecursive ( std::vector< T * > &  outComponents)

Convenience function that returns all components of the specified type in the entity structure recursively

Parameters
outComponentsall child components of type T, note that this list is not cleared before search

◆ getCore()

Core* getCore ( ) const
Returns
core

◆ getEntity()

const Entity* getEntity ( ) const

Get the entity resource of this instance

Returns
the entity resource associated with this instance, nullptr if no resource is associated with this entity, ie: the root

◆ getParent()

EntityInstance* getParent ( ) const

Get the parent of this entity (if any)

◆ hasComponent() [1/2]

bool hasComponent

Convenience function to check whether this entity has a component of the specified type

◆ hasComponent() [2/2]

bool hasComponent ( const rtti::TypeInfo type) const

Check whether this entity has a component of the specified type

Parameters
typeThe type of component to search for

◆ hasComponentsOfType() [1/2]

bool hasComponentsOfType

Convenience template function to check whether this entity has any component of the specified type

◆ hasComponentsOfType() [2/2]

bool hasComponentsOfType ( const rtti::TypeInfo type) const

Check whether this entity has any of components of the specified type.

Parameters
typeThe type of component to check for

◆ init()

bool init ( Scene scene,
EntityCreationParameters entityCreationParams,
utility::ErrorState errorState 
)

Initialize this entity.

Parameters
scenescene this entity belongs to.
entityCreationParamsParameters required to create new entity instances during init
errorStatecontains the error if initialization fails.

◆ onDestroy()

virtual void onDestroy ( )
overridevirtual

Destroy this entity

Reimplemented from Object.

◆ operator[]() [1/2]

EntityInstance& operator[] ( std::size_t  index)
Returns
child entity at index. Asserts when out of range

◆ operator[]() [2/2]

const EntityInstance& operator[] ( std::size_t  index) const
Returns
child entity at index. Asserts when out of range.

◆ removeChild()

void removeChild ( const EntityInstance entityInstance)

Removes a single child from the entity instance

Parameters
entityInstanceto remove.

◆ update()

void update ( double  deltaTime)

Update this entity hierarchy