Muto 0.0.1
Cross-Platform Application Template
GitHub Repo | Muto Updates | Muto's Author
Loading...
Searching...
No Matches
Muto::Entity Class Reference

Represents an entity in a scene. More...

#include <Entity.h>

Public Member Functions

template<typename T , typename... Args>
T & AddComponent (Args &&... args)
 Adds a component of type T to the entity with the provided arguments Otherwise, asserts if the entity already has the component.
 
template<typename T , typename... Args>
T & AddOrReplaceComponent (Args &&... args)
 Adds or replaces a component of type T to the entity with the provided arguments.
 
 Entity ()=default
 
 Entity (const Entity &other)=default
 Copy constructor for Entity.
 
 Entity (entt::entity handle, Scene *scene)
 Constructs an Entity with the given handle and scene.
 
template<typename T >
T & GetComponent ()
 Retrieves a reference to the component of type T attached to the entity if it exists. Otherwise, asserts.
 
const UUID & GetID ()
 Retrieves a const reference to the UUID of the entity.
 
const std::string & GetName ()
 Retrieves a const reference to the name of the entity.
 
template<typename T , typename... Args>
T & GetOrAddComponent (Args &&... args)
 Retrieves a reference to the component of type T attached to the entity if it exists. Otherwise, adds the component with the provided arguments and returns it.
 
template<typename T >
bool HasComponent () const
 Checks if the entity has a component of type T.
 
 operator bool () const
 
 operator entt::entity () const
 
 operator uint32_t () const
 
bool operator!= (const Entity &other) const
 
bool operator== (const Entity &other) const
 
template<typename T >
void RemoveComponent ()
 Removes the component of type T from the entity if it exists. Otherwise, asserts.
 

Private Attributes

entt::entity m_EntityID {entt::null}
 The unique identifier of the entity within the scene.
 
Scene * m_Scene = nullptr
 Pointer to the scene that contains the entity.
 

Detailed Description

Represents an entity in a scene.

Contains a handle to an entity (entt::entity) in a scene and provides methods to manipulate its components.

Constructor & Destructor Documentation

◆ Entity() [1/3]

Muto::Entity::Entity ( )
default

◆ Entity() [2/3]

Muto::Entity::Entity ( entt::entity  handle,
Scene *  scene 
)

Constructs an Entity with the given handle and scene.

Parameters
handleThe handle to the entity.
sceneThe scene to which the entity belongs.
8 {
9 }
Scene * m_Scene
Pointer to the scene that contains the entity.
Definition Entity.h:131
entt::entity m_EntityID
The unique identifier of the entity within the scene.
Definition Entity.h:129
constexpr Scope< T > CreateScope(Args &&... args)
Creates a Scope (unique_ptr) for the given type and constructor arguments.
Definition Defines_Macros.h:51

References Entity(), and m_Scene.

Referenced by Entity().

◆ Entity() [3/3]

Muto::Entity::Entity ( const Entity &  other)
default

Copy constructor for Entity.

Member Function Documentation

◆ AddComponent()

template<typename T , typename... Args>
T & Muto::Entity::AddComponent ( Args &&...  args)
inline

Adds a component of type T to the entity with the provided arguments Otherwise, asserts if the entity already has the component.

Template Parameters
TThe type of component to add.
ArgsThe types of arguments to forward to the component's constructor.
Parameters
argsThe arguments to forward to the component's constructor.
Returns
A reference to the newly added component.
49 {
50 MU_CORE_ASSERT(!HasComponent<T>(), "Entity already has component!");
51 T& component = m_Scene->m_Registry.emplace<T>(m_EntityID, std::forward<Args>(args)...);
53 return component;
54 }
#define MU_CORE_ASSERT(x,...)
Definition Asserts.h:32
entt::registry m_Registry
Definition Scene.h:41
void OnComponentAdded(Entity entity, T &component)
Definition Scene.cpp:313
@ T
Definition KeyCodes.h:60

References m_Scene.

◆ AddOrReplaceComponent()

template<typename T , typename... Args>
T & Muto::Entity::AddOrReplaceComponent ( Args &&...  args)
inline

Adds or replaces a component of type T to the entity with the provided arguments.

Template Parameters
TThe type of component to add or replace.
ArgsThe types of arguments to forward to the component's constructor.
Parameters
argsThe arguments to forward to the component's constructor.
Returns
A reference to the newly added or replaced component.
64 {
65 return m_Scene->m_Registry.emplace_or_replace<T>(m_EntityID, std::forward<Args>(args)...);
66 }

◆ GetComponent()

template<typename T >
T & Muto::Entity::GetComponent ( )
inline

Retrieves a reference to the component of type T attached to the entity if it exists. Otherwise, asserts.

Template Parameters
TThe type of component to retrieve.
Returns
A reference to the component.
74 {
75 MU_CORE_ASSERT(HasComponent<T>(), "Entity does not have component!");
76 return m_Scene->m_Registry.get<T>(m_EntityID);
77 }

◆ GetID()

const UUID & Muto::Entity::GetID ( )
inline

Retrieves a const reference to the UUID of the entity.

Returns
A const reference to the UUID of the entity.
108 {
109 return GetComponent<UUIDComponent>().ID;
110 }

◆ GetName()

const std::string & Muto::Entity::GetName ( )
inline

Retrieves a const reference to the name of the entity.

Returns
A const reference to the name of the entity.
116 {
117 return GetComponent<NameComponent>().Name;
118 }

◆ GetOrAddComponent()

template<typename T , typename... Args>
T & Muto::Entity::GetOrAddComponent ( Args &&...  args)
inline

Retrieves a reference to the component of type T attached to the entity if it exists. Otherwise, adds the component with the provided arguments and returns it.

Template Parameters
TThe type of component to add.
ArgsThe types of arguments to forward to the component's constructor.
Parameters
argsThe arguments to forward to the component's constructor.
Returns
A reference to the newly added component.
87 {
88 if (HasComponent<T>())
89 return GetComponent<T>();
90 else
91 return AddComponent<T>(std::forward<Args>(args)...);
92 }

◆ HasComponent()

template<typename T >
bool Muto::Entity::HasComponent ( ) const
inline

Checks if the entity has a component of type T.

Template Parameters
TThe type of component to check for.
Returns
true if the entity has the component, false otherwise.
37 {
38 return m_Scene->m_Registry.all_of<T>(m_EntityID);
39 }

◆ operator bool()

Muto::Entity::operator bool ( ) const
inline
121{ return m_EntityID != entt::null; }

◆ operator entt::entity()

Muto::Entity::operator entt::entity ( ) const
inline
122{ return m_EntityID; }

◆ operator uint32_t()

Muto::Entity::operator uint32_t ( ) const
inline
123{ return (uint32_t)m_EntityID; }

◆ operator!=()

bool Muto::Entity::operator!= ( const Entity &  other) const
inline
125{ return !(*this == other); }

◆ operator==()

bool Muto::Entity::operator== ( const Entity &  other) const
inline
124{ return m_EntityID == other.m_EntityID && m_Scene == other.m_Scene; }

◆ RemoveComponent()

template<typename T >
void Muto::Entity::RemoveComponent ( )
inline

Removes the component of type T from the entity if it exists. Otherwise, asserts.

Template Parameters
TThe type of component to remove.
99 {
100 MU_CORE_ASSERT(HasComponent<T>(), "Entity does not have component!");
101 m_Scene->m_Registry.remove<T>(m_EntityID);
102 }

Member Data Documentation

◆ m_EntityID

entt::entity Muto::Entity::m_EntityID {entt::null}
private

The unique identifier of the entity within the scene.

129{entt::null};

◆ m_Scene

Scene* Muto::Entity::m_Scene = nullptr
private

Pointer to the scene that contains the entity.

Referenced by AddComponent(), and Entity().


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