CoriEngine
Loading...
Searching...
No Matches
Entity.hpp
Go to the documentation of this file.
1#pragma once
2#include <entt/entt.hpp>
4
5namespace Cori {
6 namespace World {
7 namespace Systems {
8 class Hierarchy;
9 }
10 class Entity;
11 namespace Internal {
12 struct SceneID {
13 explicit SceneID(const uint32_t id) : m_OwningSceneID(id) {}
14
15 private:
16 friend Entity;
17 uint32_t m_OwningSceneID;
18 };
19 }
20
25 class Entity {
26 public:
27 Entity() = default;
28
29 Entity(entt::handle handle) : m_EntityHandle(handle) {} // NOLINT
30
38 template<typename T, typename... Args>
39 T& AddComponent(Args&&... args) {
40 return m_EntityHandle.emplace<T>(std::forward<Args>(args)...);
41 }
42
50 template<typename T, typename... Args>
51 T& ReplaceComponent(Args&&... args) {
52 return m_EntityHandle.replace<T>(std::forward<Args>(args)...);
53 }
54
62 template<typename T, typename... Args>
63 T& AddOrReplaceComponent(Args&&... args) {
64 return m_EntityHandle.emplace_or_replace<T>(std::forward<Args>(args)...);
65 }
66
73 template<typename... T>
74 [[nodiscard]] decltype(auto) GetComponents() {
75 if (!HasComponents<T...>()) {
76 CORI_CORE_FATAL_TAGGED({ Logger::Tags::World::Self, Logger::Tags::World::Entity::Self }, "Failed to get component(s) from entity. Entity '{}' does not have one or more of the following component type(s) '{}'", GetDebugData(), ([] {
77 std::ostringstream oss;
78 ((oss << ", " << CORI_CLEAN_TYPE_NAME(T)), ...);
79 return oss.str();
80 })());
81 }
82 return m_EntityHandle.get<T...>();
83 }
84
91 template<typename... T>
92 [[nodiscard]] decltype(auto) GetComponents() const {
93 if (!HasComponents<T...>()) {
94 CORI_CORE_FATAL_TAGGED({ Logger::Tags::World::Self, Logger::Tags::World::Entity::Self }, "Failed to get const component(s) from entity. Entity '{}' does not have one or more of the following component type(s) '{}'", GetDebugData(), ([] {
95 std::ostringstream oss;
96 ((oss << ", " << CORI_CLEAN_TYPE_NAME(T)), ...);
97 return oss.str();
98 })());
99 }
100
101 return m_EntityHandle.get<const T...>();
102 }
103
111 template<typename T, typename... Args>
112 T& GetOrAddComponent(Args&&... args) {
113 return m_EntityHandle.get_or_emplace<T>(std::forward<Args>(args)...);
114 }
115
121 template<typename... T>
122 [[nodiscard]] bool HasComponents() const {
123 return m_EntityHandle.all_of<T...>();
124 }
125
132 template<typename... T>
133 entt::handle::size_type RemoveComponents() { // NOLINT
134 return m_EntityHandle.remove<T...>();
135 }
136
137 // erases the components without checking if an entity have they
143 template<typename... T>
144 void EraseComponents() { // NOLINT
145 m_EntityHandle.erase<T...>();
146 }
147
148 explicit operator bool() const { return static_cast<bool>(m_EntityHandle); }
149
150 bool operator==(const Entity& other) const {
151 return m_EntityHandle == other.m_EntityHandle;
152 }
153 bool operator!=(const Entity& other) const {
154 return m_EntityHandle != other.m_EntityHandle;
155 }
156
161 [[nodiscard]] bool IsValid() const {
162 return static_cast<bool>(m_EntityHandle);
163 }
164
170 void SetActive(const bool state);
171
176 [[nodiscard]] bool IsActiveLocally() const;
177
182 [[nodiscard]] bool IsActiveGlobally() const;
183
189 [[nodiscard]] uint32_t GetID() const {
190 return entt::to_integral(m_EntityHandle.entity());
191 }
192
197 [[nodiscard]] uint32_t GetVersion() const {
198 return entt::to_version(m_EntityHandle.entity());
199 }
200
205 [[nodiscard]] uint64_t GetEUID() const {
206 return static_cast<uint64_t>(GetID()) << 32 | GetVersion();
207 }
208
213 [[nodiscard]] uint32_t GetOwnerSceneID() const {
214 return GetComponents<Internal::SceneID>().m_OwningSceneID;
215 }
216
222 [[nodiscard]] std::string GetDebugData(bool showUUID = false) const;
223
229 std::expected<void, Core::CoriError<>> SetParent(Entity parent);
230
235 [[nodiscard]] std::expected<std::vector<Entity>, Core::CoriError<>> GetSiblings() const;
236
241 [[nodiscard]] std::expected<Entity, Core::CoriError<>> GetParent() const;
242
247 [[nodiscard]] std::expected<std::vector<Entity>, Core::CoriError<>> GetChildren() const;
248
254 [[nodiscard]] std::expected<Entity, Core::CoriError<>> FindChildByName(const char* name) const;
255
261 [[nodiscard]] std::expected<Entity, Core::CoriError<>> FindChildByName(const std::string_view name) const;
262
268 [[nodiscard]] std::expected<Entity, Core::CoriError<>> FindChildByName(const std::string& name) const;
269
273 void DestroyChildren();
274
280 [[nodiscard]] entt::entity GetRawEntity() const { return m_EntityHandle.entity(); }
281
287 [[nodiscard]] entt::handle GetRawHandle() const { return m_EntityHandle; }
288
292 void PrintHierarchy() const;
293
298 [[nodiscard]] std::string_view GetName() const;
299
304 void SetName(const std::string& name);
305
309 void UnlinkFromParent();
310 protected:
311 friend Systems::Hierarchy;
312 void UpdateInactivityFlagsRecursive(entt::entity parent, bool parentIsActive);
313 private:
314
315 static void DrawHierarchyRecursive(const Entity& entity, const std::string& prefix, const bool isLast);
316
317 entt::handle m_EntityHandle;
318
319 friend class Scene;
320 };
321 }
322}
#define CORI_CLEAN_TYPE_NAME(tn)
#define CORI_CORE_FATAL_TAGGED(...)
Definition Logger.hpp:1040
Custom error class mainly used in std::expected.
Definition Error.hpp:27
Entities are the essential part of WorldSystem.
Definition Entity.hpp:25
friend class Scene
Definition Entity.hpp:319
void SetActive(const bool state)
Changes the activity state of the entity.
Definition Entity.cpp:7
std::expected< std::vector< Entity >, Core::CoriError<> > GetSiblings() const
Creates and returns a vector containing all entity siblings.
Definition Entity.cpp:86
void UnlinkFromParent()
Unlinks the entity from its parent if it has one.
Definition Entity.cpp:143
bool IsValid() const
Checks if the actual entity behind this handle is valid.
Definition Entity.hpp:161
bool IsActiveGlobally() const
Checks if the entity is active locally (doesn't have InactiveGloballyFlag), just a convenience functi...
Definition Entity.cpp:61
bool operator==(const Entity &other) const
Definition Entity.hpp:150
void PrintHierarchy() const
Prints the full entity hierarchy tree in the console.
Definition Entity.cpp:110
std::string_view GetName() const
Retrieves the name of the entity.
Definition Entity.cpp:114
entt::handle GetRawHandle() const
Gets a raw entt::handle if you need to interact with entt directly.
Definition Entity.hpp:287
std::expected< std::vector< Entity >, Core::CoriError<> > GetChildren() const
Creates and returns a vector containing all entity children (does not include grandchildren and so on...
Definition Entity.cpp:90
T & GetOrAddComponent(Args &&... args)
Retries or adds a component to the entity.
Definition Entity.hpp:112
void DestroyChildren()
Destroys all children (and they grandchildren) that the entity has.
Definition Entity.cpp:106
bool operator!=(const Entity &other) const
Definition Entity.hpp:153
std::expected< Entity, Core::CoriError<> > GetParent() const
Retries the parent entity of the entity if any.
Definition Entity.cpp:82
std::expected< void, Core::CoriError<> > SetParent(Entity parent)
Links the entity to a parent entity.
Definition Entity.cpp:78
uint32_t GetVersion() const
Gets the entity version.
Definition Entity.hpp:197
bool HasComponents() const
Checks if the entity has all the specified components.
Definition Entity.hpp:122
void EraseComponents()
Erases components from the entity without checking if the entity actually have them.
Definition Entity.hpp:144
T & ReplaceComponent(Args &&... args)
Replaces the component with a newly created one.
Definition Entity.hpp:51
T & AddComponent(Args &&... args)
Adds a component to the entity.
Definition Entity.hpp:39
uint64_t GetEUID() const
Gets the EUID (entity unique ID). A combination of entity ID and version, it is unique for every enti...
Definition Entity.hpp:205
std::string GetDebugData(bool showUUID=false) const
Gets the debuting string for logging.
Definition Entity.cpp:65
decltype(auto) GetComponents()
Retries the references to the requested components of the entity.
Definition Entity.hpp:74
bool IsActiveLocally() const
Checks if the entity is active locally (doesn't have InactiveLocallyFlag), just a convenience functio...
Definition Entity.cpp:57
uint32_t GetID() const
Gets the entity ID.
Definition Entity.hpp:189
uint32_t GetOwnerSceneID() const
Retries the ID of the owner scene. You can get the actual SceneHandle by providing SceneManager with ...
Definition Entity.hpp:213
entt::entity GetRawEntity() const
Gets a raw entt::entity if you need to interact with entt directly.
Definition Entity.hpp:280
T & AddOrReplaceComponent(Args &&... args)
Adds a component to the entity, or replaces it if the entity already has this component.
Definition Entity.hpp:63
entt::handle::size_type RemoveComponents()
Removes components from the entity if entity has them.
Definition Entity.hpp:133
Entity(entt::handle handle)
Definition Entity.hpp:29
void SetName(const std::string &name)
Changes the entity name.
Definition Entity.cpp:122
decltype(auto) GetComponents() const
Retries the references to the requested components of the entity. Const variant.
Definition Entity.hpp:92
void UpdateInactivityFlagsRecursive(entt::entity parent, bool parentIsActive)
Definition Entity.cpp:151
std::expected< Entity, Core::CoriError<> > FindChildByName(const char *name) const
Finds a children entity by name.
Definition Entity.cpp:94
Anything connected to WorldSystem (ECS) is in this namespace.
Global engine namespace.
static constexpr char Self[]
Definition Logger.hpp:127
static constexpr char Self[]
Definition Logger.hpp:119
SceneID(const uint32_t id)
Definition Entity.hpp:13