Engine²
Open-source game engine written in C++.
Loading...
Searching...
No Matches
Entity.hpp
Go to the documentation of this file.
1#pragma once
2
4#include "Id.hpp"
5#include "Logger.hpp"
6#include "core/Core.hpp"
7#include "entity/EntityId.hpp"
8#include <entt/entt.hpp>
9#include <typeindex>
10
11namespace Engine {
12
20class Entity {
21 public:
26 Entity(Core &core, EntityId entityId) : _core(core), _entityId(entityId) {}
27
30 ~Entity() = default;
31
34 bool IsAlive() const;
35
39 inline EntityId Id() const { return _entityId; }
40
44 explicit(false) inline operator EntityId() const { return _entityId; }
45
48 void Kill();
49
55 template <typename TComponent> inline decltype(auto) AddComponent(TComponent &&component)
56 {
57 return _entityId.AddComponent(GetCore(), std::forward<TComponent>(component));
58 }
59
66 template <typename TComponent, typename... TArgs> inline decltype(auto) AddComponent(TArgs &&...args)
67 {
68 return _entityId.AddComponent<TComponent>(GetCore(), std::forward<TArgs>(args)...);
69 }
70
77 template <typename TComponent, typename... TArgs> inline decltype(auto) AddComponentIfNotExists(TArgs &&...args)
78 {
79 return _entityId.AddComponentIfNotExists<TComponent>(GetCore(), std::forward<TArgs>(args)...);
80 }
81
89 template <typename TTempComponent, typename... TArgs> inline decltype(auto) AddTemporaryComponent(TArgs &&...args)
90 {
91 return _entityId.AddTemporaryComponent<TTempComponent>(GetCore(), std::forward<TArgs>(args)...);
92 }
93
99
103 template <typename TComponent> inline void RemoveComponent() { _entityId.RemoveComponent<TComponent>(GetCore()); }
104
109 template <typename... TComponent> inline bool HasComponents() const
110 {
111 return _entityId.HasComponents<TComponent...>(GetCore());
112 }
113
118 template <typename... TComponent> inline decltype(auto) GetComponents()
119 {
120 return _entityId.GetComponents<TComponent...>(GetCore());
121 }
122
127 template <typename... TComponent> inline decltype(auto) GetComponents() const
128 {
129 return _entityId.GetComponents<TComponent...>(GetCore());
130 }
131
137 template <typename TComponent> inline decltype(auto) TryGetComponent()
138 {
139 return _entityId.TryGetComponent<TComponent>(GetCore());
140 }
141
147 bool operator==(const Entity &rhs) const { return _entityId.value == rhs._entityId.value; }
148
154 bool operator==(const EntityId &rhs) const { return _entityId.value == rhs.value; }
155
156 private:
161 constexpr Core &GetCore() const { return _core.get(); }
162
164 std::reference_wrapper<Core> _core;
169};
170
171} // namespace Engine
172
176template <> struct fmt::formatter<Engine::Entity> : fmt::formatter<Engine::EntityId> {
184 template <typename FormatContext> auto format(const Engine::Entity &entity, FormatContext &ctx) const
185 {
186 return fmt::formatter<Engine::EntityId>::format(entity.Id(), ctx);
187 }
188};
The core is the place where all the data of the engine is stored. It contains the registry (entities)...
Definition Core.hpp:33
Wrapper class providing a convenient interface for entity manipulation with the Core....
Definition Entity.hpp:20
decltype(auto) AddComponentIfNotExists(TArgs &&...args)
Add a component to an entity if it does not already exist.
Definition Entity.hpp:77
~Entity()=default
Default destructor for Entity. It does not perform any special cleanup, as the Core is responsible fo...
decltype(auto) GetComponents() const
Get components of type TComponent from the entity.
Definition Entity.hpp:127
EntityId Id() const
Get the EntityId associated with this Entity.
Definition Entity.hpp:39
static void RemoveTemporaryComponents(Core &core)
Remove all temporary components from the registry.
Definition Entity.hpp:98
bool operator==(const EntityId &rhs) const
Equality operator for Entity and EntityId. It compares the underlying EntityId value of the Entity wi...
Definition Entity.hpp:154
decltype(auto) AddTemporaryComponent(TArgs &&...args)
Add a temporary component to an entity. Temporary component are removed when calling RemoveTemporaryC...
Definition Entity.hpp:89
Entity(Core &core, EntityId entityId)
Create an Entity from EntityId and the Core it belongs to.
Definition Entity.hpp:26
constexpr Core & GetCore() const
Get a reference to the Core instance that this Entity belongs to. This is used internally to access t...
Definition Entity.hpp:161
decltype(auto) AddComponent(TComponent &&component)
Add a component to an entity.
Definition Entity.hpp:55
decltype(auto) TryGetComponent()
Try to get a component of type TComponent from the entity. It returns a pointer to the component if i...
Definition Entity.hpp:137
bool IsAlive() const
Check if the entity is alive.
Definition Entity.cpp:3
bool HasComponents() const
Check if entity have one or multiple component's type.
Definition Entity.hpp:109
bool operator==(const Entity &rhs) const
Equality operator for Entity. It compares the underlying EntityId values to determine if two Entity i...
Definition Entity.hpp:147
EntityId _entityId
The EntityId that identifies the entity in the Core's registry. This is the underlying identifier for...
Definition Entity.hpp:168
decltype(auto) GetComponents()
Get components of type TComponent from the entity.
Definition Entity.hpp:118
void Kill()
Kill the entity, removing all components and making it invalid. After calling this method,...
Definition Entity.cpp:5
std::reference_wrapper< Core > _core
A reference to the Core instance that this Entity belongs to.
Definition Entity.hpp:164
void RemoveComponent()
Remove a component from an entity.
Definition Entity.hpp:103
decltype(auto) AddComponent(TArgs &&...args)
Add a component to an entity.
Definition Entity.hpp:66
Definition Core.hpp:17
TValue value
The underlying value of the ID.
Definition Id.hpp:20
Represents a unique identifier for an entity in the Engine's entity-component system....
Definition EntityId.hpp:14
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
auto format(const Engine::Entity &entity, FormatContext &ctx) const
Format an Entity for output using the fmt library. It formats the Entity by formatting its underlying...
Definition Entity.hpp:184