CoriEngine
Loading...
Searching...
No Matches
Entity.cpp
Go to the documentation of this file.
1#include "Entity.hpp"
2#include "Scene.hpp"
4
5namespace Cori {
6 namespace World {
7 void Entity::SetActive(const bool state) {
8 if (state) {
11 }
12
15 const auto& hierarchy = GetComponents<Components::Entity::Hierarchy>();
16 const entt::entity parent = hierarchy.m_Parent;
17 const entt::entity firstChild = hierarchy.m_FirstChild;
18 const entt::registry* registry = m_EntityHandle.registry();
19 if (registry->valid(parent)) {
20 if (registry->all_of<Components::Entity::InactiveGloballyFlag>(parent)) {
21 return;
22 }
23 }
24 if (registry->valid(firstChild)) {
25 UpdateInactivityFlagsRecursive(m_EntityHandle.entity(), true);
26 return;
27 }
29 return;
30 }
32 return;
33 }
34 return;
35 }
36
39 }
40
43 const auto& hierarchy = GetComponents<Components::Entity::Hierarchy>();
44 const entt::entity firstChild = hierarchy.m_FirstChild;
45 const entt::registry* registry = m_EntityHandle.registry();
46 if (registry->valid(firstChild)) {
47 UpdateInactivityFlagsRecursive(m_EntityHandle.entity(), false);
48 return;
49 }
51 return;
52 }
54 }
55 }
56
60
64
65 std::string Entity::GetDebugData(bool showUUID) const {
66 if (showUUID) {
67 if (CORI_CORE_CHECK(HasComponents<Components::Entity::UUID>(), "Calling GetDebugData with showUUID=true but entity '{}' doesn't have a UUID component, it will not be shown.", GetDebugData())) {
68 showUUID = false;
69 }
70 }
71 if (IsValid()) {
72 return std::format("(Entity ID: '{}', Version: '{}', Name: '{}'{}", GetID(), GetVersion(), GetName(), showUUID ? std::format(", UUID: '{}')", GetComponents<Components::Entity::UUID>().m_UUID.GetSerializationString()): ")");
73 }
74 return "GetDebugData: Error: This Entity is no longer valid.";
75 }
76
77 // ReSharper disable once CppParameterMayBeConst
78 std::expected<void, Core::CoriError<>> Entity::SetParent(Entity parent) {
79 return Systems::Hierarchy::SetParent(*this, parent);
80 }
81
82 std::expected<Entity, Core::CoriError<>> Entity::GetParent() const {
84 }
85
86 std::expected<std::vector<Entity>, Core::CoriError<>> Entity::GetSiblings() const {
88 }
89
90 std::expected<std::vector<Entity>, Core::CoriError<>> Entity::GetChildren() const {
92 }
93
94 std::expected<Entity, Core::CoriError<>> Entity::FindChildByName(const char* name) const {
95 return Systems::Hierarchy::FindChildByName(*this, name);
96 }
97
98 std::expected<Entity, Core::CoriError<>> Entity::FindChildByName(const std::string_view name) const {
99 return Systems::Hierarchy::FindChildByName(*this, name);
100 }
101
102 std::expected<Entity, Core::CoriError<>> Entity::FindChildByName(const std::string& name) const {
103 return Systems::Hierarchy::FindChildByName(*this, name);
104 }
105
109
113
114 std::string_view Entity::GetName() const {
115 static constexpr char empty[] = "";
118 }
119
120 return {empty};
121 }
122 void Entity::SetName(const std::string& name) {
123 auto& nameComponent = GetOrAddComponent<Components::Entity::Name>();
124 if (nameComponent.m_Name == name) {
125 return;
126 }
127
129 const auto& hierarchy = GetComponents<Components::Entity::Hierarchy>();
130 entt::registry* registry = m_EntityHandle.registry();
131 if (registry->valid(hierarchy.m_Parent)) {
132 const entt::entity parent = hierarchy.m_Parent;
133 auto& cache = registry->get<Components::Entity::ChildCache>(parent);
134 cache.m_Children.erase(nameComponent.m_Name);
135 cache.m_Children.emplace(name, m_EntityHandle.entity());
136 }
137 }
138
139 nameComponent.m_Name = name;
140 }
141
142
146
147 void Entity::DrawHierarchyRecursive(const Entity& entity, const std::string& prefix, const bool isLast) {
148 Systems::Hierarchy::DrawHierarchyRecursive(entity, prefix, isLast);
149 }
150
151 void Entity::UpdateInactivityFlagsRecursive(entt::entity parent, const bool parentIsActive) {
153 entt::registry* registry = m_EntityHandle.registry();
154
155 const bool finalEffectiveState = parentIsActive && !registry->all_of<Components::Entity::InactiveLocallyFlag>(parent);
156
157 if (finalEffectiveState) {
158 registry->remove<Components::Entity::InactiveGloballyFlag>(parent);
159 } else {
160 registry->emplace_or_replace<Components::Entity::InactiveLocallyFlag>(parent);
161 }
162
163 const auto& hierarchy = registry->get<Components::Entity::Hierarchy>(parent);
164 entt::entity currentChild = hierarchy.m_FirstChild;
165 while (registry->valid(currentChild)) {
166 UpdateInactivityFlagsRecursive(currentChild, finalEffectiveState);
167 currentChild = registry->get<Components::Entity::Hierarchy>(currentChild).m_NextSibling;
168 }
169 }
170 }
171}
#define CORI_CORE_CHECK(x,...)
Definition Logger.hpp:1042
#define CORI_PROFILE_FUNCTION()
Definition Profiler.hpp:9
Custom error class mainly used in std::expected.
Definition Error.hpp:27
Entities are the essential part of WorldSystem.
Definition Entity.hpp:25
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
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
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
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 & AddComponent(Args &&... args)
Adds a component to the entity.
Definition Entity.hpp:39
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
void SetName(const std::string &name)
Changes the entity name.
Definition Entity.cpp:122
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
static std::expected< std::vector< Entity >, Core::CoriError<> > GetSiblings(Entity subject)
Definition Hierarchy.cpp:90
static void DrawHierarchyRecursive(const Entity &entity, const std::string &prefix, const bool isLast)
static std::expected< Entity, Core::CoriError<> > GetParent(Entity subject)
static std::expected< Entity, Core::CoriError<> > FindChildByName(Entity subject, const char *name)
static std::expected< void, Core::CoriError<> > SetParent(Entity subject, Entity parent)
Definition Hierarchy.cpp:11
static void UnlinkFromParent(Entity subject)
Definition Hierarchy.cpp:50
static void DestroyChildren(Entity subject)
static std::expected< std::vector< Entity >, Core::CoriError<> > GetChildren(Entity subject)
static void PrintHierarchy(Entity subject)
Anything connected to WorldSystem (ECS) is in this namespace.
Global engine namespace.
Every Entity has a ChildCache component by default, it's essential for entity ChildCache system.
Every Entity has a hierarchy component by default, it's essential for entity hierarchy system.
Entity aquifers this flag when is disabled explicitly or its parent Entity is disabled.
Entity aquifers this flag when is disabled explicitly.