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
21 constexpr EntityId(ValueType value_ = NullValue());
22
26 constexpr EntityId(Id id);
27
31 static constexpr EntityId Null();
32
37 bool IsValid(const Engine::Core &core) const;
38
44 template <typename TComponent> decltype(auto) AddComponent(Engine::Core &core, TComponent &&component);
45
52 template <typename TComponent, typename... TArgs> decltype(auto) AddComponent(Engine::Core &core, TArgs &&...args);
53
60 template <typename TComponent, typename... TArgs>
61 decltype(auto) AddComponentIfNotExists(Engine::Core &core, TArgs &&...args);
62
67 template <typename... TComponent> bool HasComponents(const Engine::Core &core) const;
68
77 template <typename TTempComponent, typename... TArgs>
78 decltype(auto) AddTemporaryComponent(Engine::Core &core, TArgs &&...args);
79
84 template <typename... TComponent> decltype(auto) GetComponents(Engine::Core &core);
85
90 template <typename... TComponent> decltype(auto) GetComponents(Engine::Core &core) const;
91
98 template <typename TComponent> decltype(auto) TryGetComponent(Engine::Core &core);
99
103 template <typename TComponent> void RemoveComponent(Engine::Core &core);
104
109 static void RemoveTemporaryComponents(Core &core);
110
111 private:
112 inline static std::unordered_map<std::type_index, std::function<void(Core &)>> temporaryComponent = {};
113};
114
119static_assert(sizeof(EntityId) == sizeof(EntityId::ValueType), "EntityId size must be equal to Id size");
120} // namespace Engine
121
125template <> struct std::hash<Engine::EntityId> {
130 std::size_t operator()(const Engine::EntityId &entityId) const noexcept;
131};
132
135template <> struct fmt::formatter<Engine::EntityId> : fmt::formatter<std::string> {
143 template <typename FormatContext> auto format(const Engine::EntityId &entityId, FormatContext &ctx) const;
144};
145
146#include "entity/EntityId.ipp"
The core is the place where all the data of the engine is stored. It contains the registry (entities)...
Definition Core.hpp:33
Definition Core.hpp:17
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
entt::id_type ValueType
Definition Id.hpp:48
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
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.ipp:98
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