Engine²
Open-source game engine written in C++.
Loading...
Searching...
No Matches
EntityId.ipp
Go to the documentation of this file.
1#include "entity/EntityId.hpp"
2
3namespace Engine {
4constexpr EntityId::EntityId(ValueType v) : Id{v} {}
5
6constexpr EntityId::EntityId(Id id) : Id{id} {}
7
8constexpr EntityId EntityId::Null() { return EntityId{NullValue()}; }
9
10inline bool EntityId::IsValid(const Engine::Core &core) const { return core.IsEntityValid(value); }
11
12template <typename TComponent> decltype(auto) EntityId::AddComponent(Engine::Core &core, TComponent &&component)
13{
15 Log::Debug(fmt::format("[EntityID:{}] AddComponent: {}", value, typeid(TComponent).name()));
16 return core.GetRegistry().emplace<TComponent>(value, std::forward<TComponent>(component));
17}
18
19template <typename TComponent, typename... TArgs>
20decltype(auto) EntityId::AddComponent(Engine::Core &core, TArgs &&...args)
21{
23 Log::Debug(fmt::format("[EntityID:{}] AddComponent: {}", value, typeid(TComponent).name()));
24 return core.GetRegistry().emplace<TComponent>(value, std::forward<TArgs>(args)...);
25}
26
27template <typename TComponent, typename... TArgs>
28decltype(auto) EntityId::AddComponentIfNotExists(Engine::Core &core, TArgs &&...args)
29{
30 if (this->HasComponents<TComponent>(core))
31 {
32 return this->GetComponents<TComponent>(core);
33 }
34 return this->AddComponent<TComponent>(core, std::forward<TArgs>(args)...);
35}
36
37template <typename... TComponent> bool EntityId::HasComponents(const Engine::Core &core) const
38{
39 return core.GetRegistry().all_of<TComponent...>(value);
40}
41
42template <typename TTempComponent, typename... TArgs>
43decltype(auto) EntityId::AddTemporaryComponent(Engine::Core &core, TArgs &&...args)
44{
45 if (!temporaryComponent.contains(std::type_index(typeid(TTempComponent))))
46 {
47 temporaryComponent[std::type_index(typeid(TTempComponent))] = [](Core &c) {
48 Log::Debug(fmt::format("RemoveTemporaryComponent: {}", typeid(TTempComponent).name()));
49 c.GetRegistry().clear<TTempComponent>();
50 };
51 }
52
53 return this->AddComponent<TTempComponent>(core, std::forward<TArgs>(args)...);
54}
55
56template <typename... TComponent> decltype(auto) EntityId::GetComponents(Engine::Core &core)
57{
58 return core.GetRegistry().get<TComponent...>(this->value);
59}
60
61template <typename... TComponent> decltype(auto) EntityId::GetComponents(Engine::Core &core) const
62{
63 return core.GetRegistry().get<TComponent...>(this->value);
64}
65
66template <typename TComponent> decltype(auto) EntityId::TryGetComponent(Engine::Core &core)
67{
68 return core.GetRegistry().try_get<TComponent>(this->value);
69}
70
71template <typename TComponent> void EntityId::RemoveComponent(Engine::Core &core)
72{
74 Log::Debug(fmt::format("[EntityID:{}] RemoveComponent: {}", value, typeid(TComponent).name()));
75 core.GetRegistry().remove<TComponent>(value);
76}
77
79{
80 if (temporaryComponent.empty())
81 {
82 return;
83 }
84 for (const auto &[typeIndex, func] : temporaryComponent)
85 {
86 func(core);
87 }
88 temporaryComponent.clear();
89}
90} // namespace Engine
91
92inline std::size_t std::hash<Engine::EntityId>::operator()(const Engine::EntityId &entityId) const noexcept
93{
94 return std::hash<entt::id_type>{}(entityId.value);
95}
96
97template <typename FormatContext>
98auto fmt::formatter<Engine::EntityId>::format(const Engine::EntityId &entityId, FormatContext &ctx) const
99{
100 if (entityId.IsNull())
101 {
102 return fmt::formatter<std::string>::format("null_entity", ctx);
103 }
104 return fmt::formatter<std::string>::format(Log::EntityToDebugString(entityId.value), ctx);
105}
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:99
Registry & GetRegistry()
Get the entt::registry that contains all components. It should be used to update component through sy...
Definition Core.cpp:44
Definition Core.hpp:17
std::string EntityToDebugString(const T &entity)
Definition EntityToIDString.hpp:12
void Debug(const T &msg) noexcept(false)
Definition Logger.hpp:28
constexpr bool IsNull(void) const
Checks if the ID is null/invalid by comparing its value to the null value defined by the derived clas...
Definition Id.ipp:12
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.ipp:28
static constexpr EntityId Null()
Returns a null EntityId. A null EntityId is an EntityId that does not correspond to any valid entity ...
Definition EntityId.ipp:8
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.ipp:10
decltype(auto) AddTemporaryComponent(Engine::Core &core, TArgs &&...args)
Add a temporary component to an entity. Temporary component are removed when calling RemoveTemporaryC...
Definition EntityId.ipp:43
void RemoveComponent(Engine::Core &core)
Remove a component from an entity.
Definition EntityId.ipp:71
bool HasComponents(const Engine::Core &core) const
Check if entity have one or multiple component's type.
Definition EntityId.ipp:37
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.ipp:78
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.ipp:66
decltype(auto) GetComponents(Engine::Core &core)
Get components of type TComponent from an entity.
Definition EntityId.ipp:56
constexpr EntityId(ValueType value_=NullValue())
Constructs an EntityId with the given value. By default, it constructs a null EntityId.
Definition EntityId.ipp:4
decltype(auto) AddComponent(Engine::Core &core, TComponent &&component)
Add a component to an entity.
Definition EntityId.ipp:12
static std::unordered_map< std::type_index, std::function< void(Core &)> > temporaryComponent
Definition EntityId.hpp:112
constexpr Id(ValueType value_=NullValue())
Constructor for Id.
Definition Id.ipp:17
static constexpr ValueType NullValue(void)
Get the null value for this ID type. This returns the null value defined by EnTT's id_type,...
Definition Id.ipp:21
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.ipp:92