SpiecsEngine
 
Loading...
Searching...
No Matches
Entity.h
Go to the documentation of this file.
1/**
2* @file Entity.h.
3* @brief The Entity Class Definitions.
4* @author The Cherno.
5*/
6
7#pragma once
8#include "Core/Core.h"
9#include "entt.hpp"
10#include "World/Components/UUIDComponent.h"
11#include "World/World.h"
12
13namespace Spices {
14
15 /**
16 * @brief Entity Class.
17 * This class defines the specific behaves of Entity.
18 */
19 class Entity
20 {
21 public:
22
23 /**
24 * @brief Constructor Function.
25 */
26 Entity() = default;
27
28 /**
29 * @brief Constructor Function.
30 * Init class variable.
31 * Usually call it.
32 * @param[in] handle entt::entity, ESC identify.
33 * @param[in] world World pointer.
34 */
35 Entity(entt::entity handle, World* world)
37 , m_World(world)
38 {}
39
40 /**
41 * @brief Destructor Function.
42 */
43 virtual ~Entity() = default;
44
45 /**
46 * @brief Template Function.
47 * Used for add specific component to entity.
48 * @tparam T Specific component.
49 * @param[in] args Component construct parameters.
50 * @return Returns The specific component reference that added.
51 */
52 template<typename T, typename... Args>
53 T& AddComponent(Args&&... args)
54 {
55 if (HasComponent<T>())
56 {
57 std::stringstream ss;
58 ss << "Entity: " << (int)m_EntityHandle << " already has such component.";
59
60 SPICES_CORE_WARN(ss.str())
61 return GetComponent<T>();
62 }
63
64 T& component = m_World->AddComponent<T>(m_EntityHandle, std::forward<Args>(args)...);
65 m_World->OnComponentAdded<T>(this, component);
66 return component;
67 }
68
69 /**
70 * @brief Get Component owned by this entity.
71 * @tparam T Which Component we will get.
72 * @return Returns the specific Component.
73 */
74 template<typename T>
75 T& GetComponent() const
76 {
77 return m_World->GetComponent<T>(m_EntityHandle);
78 }
79
80 /**
81 * @brief Remove Component owned from this entity.
82 * @tparam T Which Component we will remove.
83 */
84 template<typename T>
85 void RemoveComponent() const
86 {
87 m_World->RemoveComponent<T>(m_EntityHandle);
88 }
89
90 /**
91 * @brief Remove a entity from this world root.
92 */
94 {
96 }
97
98 /**
99 * @brief Add a entity to this world root.
100 */
102 {
104 }
105
106 /**
107 * @brief If Component is owned by this entity or not.
108 * @tparam T Which Component we will search.
109 * @return Returns true if found.
110 */
111 template<typename T>
112 bool HasComponent() const
113 {
114 return m_World->HasComponent<T>(m_EntityHandle);
115 }
116
117 /**
118 * @brief Get UUID form UUIDComponent.
119 * @return Returns UUID.
120 */
121 const UUID GetUUID() { return GetComponent<UUIDComponent>().GetUUID(); }
122
123 /**
124 * @brief Empty Operation.
125 * @return Returns true if m_EntityHandle is valid.
126 */
127 operator bool() const { return m_EntityHandle != entt::null; }
128
129 /**
130 * @brief Empty Operation.
131 * @return Returns m_EntityHandle's value.
132 */
133 operator uint32_t() const { return static_cast<uint32_t>(m_EntityHandle); }
134
135 /**
136 * @brief Empty Operation.
137 * @return Returns m_EntityHandle.
138 */
139 operator entt::entity() const { return m_EntityHandle; }
140
141 /**
142 * @brief Equal Operation.
143 * @param[in] other Another Entity.
144 * @return Returns true if euqal.
145 */
146 bool operator ==(const Entity& other) const
147 {
149 }
150
151 /**
152 * @brief Not equal Operation.
153 * @param[in] other Another Entity.
154 * @return Returns true if not equal.
155 */
156 bool operator !=(const Entity& other) const
157 {
158 return !operator==(other);
159 }
160
161 private:
162
163 /**
164 * @brief This entity's identify in ECS.
165 */
167
168 /**
169 * @brief A specific world Pointer.
170 */
171 World* m_World = nullptr;
172 };
173}
#define SPICES_PROFILE_ZONE
virtual void OnPreActivate() override
This interface define the specific world behave before on activated.
virtual void OnDeactivate() override
This interface defines the specific world behave after on activated.
virtual void OnActivate(TimeStep &ts) override
This interface define the specific world behave on activated.
EditorWorld Class. This class defines the specific behave of EditorWorld.
Definition EditorWorld.h:18
T & AddComponent(Args &&... args)
Template Function. Used for add specific component to entity.
Definition Entity.h:53
T & GetComponent() const
Get Component owned by this entity.
Definition Entity.h:75
void RemoveComponent() const
Remove Component owned from this entity.
Definition Entity.h:85
Entity()=default
Constructor Function.
void AddToRoot()
Add a entity to this world root.
Definition Entity.h:101
World * m_World
A specific world Pointer.
Definition Entity.h:171
const UUID GetUUID()
Get UUID form UUIDComponent.
Definition Entity.h:121
Entity(entt::entity handle, World *world)
Constructor Function. Init class variable. Usually call it.
Definition Entity.h:35
void RemoveFromRoot()
Remove a entity from this world root.
Definition Entity.h:93
operator bool() const
Empty Operation.
Definition Entity.h:127
virtual ~Entity()=default
Destructor Function.
bool HasComponent() const
If Component is owned by this entity or not.
Definition Entity.h:112
operator uint32_t() const
Empty Operation.
Definition Entity.h:133
Entity Class. This class defines the specific behaves of Entity.
Definition Entity.h:20
Wrapper of Gltf file data.
This Class handles our engine time step during frames. Global Unique.
Definition TimeStep.h:22
UUIDComponent Class. This class defines the specific behaves of UUIDComponent.
This class helps to generate a uuid for one resource.
Definition UUID.h:16
virtual void OnDeactivate() override
This interface defines the specific world behave after on activated.
virtual void OnActivate(TimeStep &ts) override
This interface define the specific world behave on activated.
virtual void OnPreActivate() override
This interface define the specific world behave before on activated.
void RemoveFromRoot(Entity &entity)
Remove a entity from this world root.
Definition World.cpp:58
void AddToRoot(Entity &entity)
Add a entity to this world root.
Definition World.cpp:67
World Class. This class defines the basic behaves of World. When we create an new world,...
Definition World.h:41