Engine²
Open-source game engine written in C++.
Loading...
Searching...
No Matches
EntityId.hpp
Go to the documentation of this file.
1#pragma once
2
3#include "Id.hpp"
4#include "core/Core.hpp"
5#include <fmt/format.h>
6
7namespace Engine {
14struct EntityId : Id {
17
23 constexpr EntityId(ValueType v = NullValue()) : Id{v} {}
24
29 constexpr EntityId(Id id) : Id{id} {}
30
34 constexpr operator ValueType() const { return value; }
35
40 static constexpr EntityId Null() { return EntityId{NullValue()}; }
41
47 inline bool IsValid(const Engine::Core &core) const { return core.IsEntityValid(value); }
48
55 template <typename TComponent> inline decltype(auto) AddComponent(Engine::Core &core, TComponent &&component)
56 {
58 Log::Debug(fmt::format("[EntityID:{}] AddComponent: {}", value, typeid(TComponent).name()));
59 return core.GetRegistry().emplace<TComponent>(value, std::forward<TComponent>(component));
60 }
61
69 template <typename TComponent, typename... TArgs>
70 inline decltype(auto) AddComponent(Engine::Core &core, TArgs &&...args)
71 {
73 Log::Debug(fmt::format("[EntityID:{}] AddComponent: {}", value, typeid(TComponent).name()));
74 return core.GetRegistry().emplace<TComponent>(value, std::forward<TArgs>(args)...);
75 }
76
84 template <typename TComponent, typename... TArgs>
85 inline decltype(auto) AddComponentIfNotExists(Engine::Core &core, TArgs &&...args)
86 {
87 if (this->HasComponents<TComponent>(core))
88 {
89 return this->GetComponents<TComponent>(core);
90 }
91 return this->AddComponent<TComponent>(core, std::forward<TArgs>(args)...);
92 }
93
98 template <typename... TComponent> inline bool HasComponents(const Engine::Core &core) const
99 {
100 return core.GetRegistry().all_of<TComponent...>(value);
101 }
102
112 template <typename TTempComponent, typename... TArgs>
113 inline decltype(auto) AddTemporaryComponent(Engine::Core &core, TArgs &&...args)
114 {
115 if (!temporaryComponent.contains(std::type_index(typeid(TTempComponent))))
116 {
117 temporaryComponent[std::type_index(typeid(TTempComponent))] = [](Core &c) {
118 Log::Debug(fmt::format("RemoveTemporaryComponent: {}", typeid(TTempComponent).name()));
119 c.GetRegistry().clear<TTempComponent>();
120 };
121 }
122
123 return this->AddComponent<TTempComponent>(core, std::forward<TArgs>(args)...);
124 }
125
131 template <typename... TComponent> inline decltype(auto) GetComponents(Engine::Core &core)
132 {
133 return core.GetRegistry().get<TComponent...>(this->value);
134 }
135
141 template <typename... TComponent> inline decltype(auto) GetComponents(Engine::Core &core) const
142 {
143 return core.GetRegistry().get<TComponent...>(this->value);
144 }
145
153 template <typename TComponent> inline decltype(auto) TryGetComponent(Engine::Core &core)
154 {
155 return core.GetRegistry().try_get<TComponent>(this->value);
156 }
157
162 template <typename TComponent> inline void RemoveComponent(Engine::Core &core)
163 {
164 Log::Debug(fmt::format("[EntityID:{}] RemoveComponent: {}", value, typeid(TComponent).name()));
165 core.GetRegistry().remove<TComponent>(value);
166 }
167
174 {
175 if (temporaryComponent.empty())
176 {
177 return;
178 }
179 for (const auto &[typeIndex, func] : temporaryComponent)
180 {
181 func(core);
182 }
183 temporaryComponent.clear();
184 }
185
186 private:
187 inline static std::unordered_map<std::type_index, std::function<void(Core &)>> temporaryComponent = {};
188};
189
194static_assert(sizeof(EntityId) == sizeof(EntityId::ValueType), "EntityId size must be equal to Id size");
195} // namespace Engine
196
200template <> struct std::hash<Engine::EntityId> {
206 std::size_t operator()(const Engine::EntityId &entityId) const noexcept
207 {
208 return std::hash<entt::id_type>{}(entityId.value);
209 }
210};
211
214template <> struct fmt::formatter<Engine::EntityId> : fmt::formatter<std::string> {
223 template <typename FormatContext> auto format(const Engine::EntityId &entityId, FormatContext &ctx) const
224 {
225 if (entityId.IsNull())
226 {
227 return fmt::formatter<std::string>::format("null_entity", ctx);
228 }
229 return fmt::formatter<std::string>::format(Log::EntityToDebugString(entityId.value), ctx);
230 }
231};
The core is the place where all the data of the engine is stored. It contains the registry (entities)...
Definition Core.hpp:33
bool IsEntityValid(Id entity) const
Check if entity is valid in the context of the registry. It check if the id of the entity exist.
Definition Core.cpp:95
Registry & GetRegistry()
Get the entt::registry that contains all components. It should be used to update component through sy...
Definition Core.hpp:50
Definition Core.hpp:17
std::string EntityToDebugString(const T &entity)
Definition EntityToIDString.hpp:12
void Debug(const T &msg) noexcept
Definition Logger.hpp:45
entt::id_type value
Definition Id.hpp:20
Represents a unique identifier for an entity in the Engine's entity-component system....
Definition EntityId.hpp:14
Id::ValueType ValueType
The underlying value type of the EntityId, which is the same as Engine::Id::ValueType.
Definition EntityId.hpp:16
decltype(auto) AddComponentIfNotExists(Engine::Core &core, TArgs &&...args)
Add a component to an entity if it does not already exist.
Definition EntityId.hpp:85
bool IsValid(const Engine::Core &core) const
Checks if the EntityId is valid. An EntityId is considered valid if it corresponds to an existing ent...
Definition EntityId.hpp:47
decltype(auto) AddComponent(Engine::Core &core, TArgs &&...args)
Add a component to an entity.
Definition EntityId.hpp:70
constexpr EntityId(Id id)
Constructs an EntityId from an Engine::Id. This allows for implicit conversion from Engine::Id to Ent...
Definition EntityId.hpp:29
decltype(auto) AddTemporaryComponent(Engine::Core &core, TArgs &&...args)
Add a temporary component to an entity. Temporary component are removed when calling RemoveTemporaryC...
Definition EntityId.hpp:113
static constexpr EntityId Null()
Returns a null EntityId. A null EntityId is an EntityId that does not correspond to any valid entity ...
Definition EntityId.hpp:40
void RemoveComponent(Engine::Core &core)
Remove a component from an entity.
Definition EntityId.hpp:162
bool HasComponents(const Engine::Core &core) const
Check if entity have one or multiple component's type.
Definition EntityId.hpp:98
decltype(auto) GetComponents(Engine::Core &core) const
Get components of type TComponent from the entity.
Definition EntityId.hpp:141
constexpr EntityId(ValueType v=NullValue())
Constructs an EntityId with the given value. By default, it constructs a null EntityId.
Definition EntityId.hpp:23
decltype(auto) TryGetComponent(Engine::Core &core)
Try to get a component of type TComponent from the entity. It returns a pointer to the component if i...
Definition EntityId.hpp:153
decltype(auto) GetComponents(Engine::Core &core)
Get components of type TComponent from an entity.
Definition EntityId.hpp:131
decltype(auto) AddComponent(Engine::Core &core, TComponent &&component)
Add a component to an entity.
Definition EntityId.hpp:55
static void RemoveTemporaryComponents(Core &core)
Remove all temporary components from the registry. This system should be called at the end of each lo...
Definition EntityId.hpp:173
static std::unordered_map< std::type_index, std::function< void(Core &)> > temporaryComponent
Definition EntityId.hpp:187
static constexpr ValueType NullValue()
Get the null value for this ID type. This returns the null value defined by EnTT's id_type,...
Definition Id.hpp:66
constexpr Id(ValueType v=NullValue())
Constructor for Id.
Definition Id.hpp:56
entt::id_type ValueType
Definition Id.hpp:50
auto format(const Engine::EntityId &entityId, FormatContext &ctx) const
Formats an EntityId for output. If the EntityId is null, it will format as "null_entity"....
Definition EntityId.hpp:223
std::size_t operator()(const Engine::EntityId &entityId) const noexcept
Hash function for EntityId. It uses the hash of the underlying ValueType to compute the hash of the E...
Definition EntityId.hpp:206