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:
25 Entity(Core &core, EntityId entityId);
26
29 ~Entity() = default;
30
33 bool IsAlive() const;
34
37 EntityId Id() const;
38
41 explicit(false) operator EntityId() const;
42
45 void Kill();
46
51 template <typename TComponent> decltype(auto) AddComponent(TComponent &&component);
52
58 template <typename TComponent, typename... TArgs> decltype(auto) AddComponent(TArgs &&...args);
59
65 template <typename TComponent, typename... TArgs> decltype(auto) AddComponentIfNotExists(TArgs &&...args);
66
74 template <typename TTempComponent, typename... TArgs> decltype(auto) AddTemporaryComponent(TArgs &&...args);
75
79 static void RemoveTemporaryComponents(Core &core);
80
83 template <typename TComponent> void RemoveComponent();
84
88 template <typename... TComponent> bool HasComponents() const;
89
93 template <typename... TComponent> decltype(auto) GetComponents();
94
98 template <typename... TComponent> decltype(auto) GetComponents() const;
99
104 template <typename TComponent> decltype(auto) TryGetComponent();
105
110 bool operator==(const Entity &rhs) const;
111
116 bool operator==(const EntityId &rhs) const;
117
118 private:
122 Core &GetCore() const;
123
125 std::reference_wrapper<Core> _core;
130};
131
132} // namespace Engine
133
137template <> struct fmt::formatter<Engine::Entity> : fmt::formatter<Engine::EntityId> {
144 template <typename FormatContext> auto format(const Engine::Entity &entity, FormatContext &ctx) const;
145};
146
147#include "entity/Entity.ipp"
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.ipp:14
~Entity()=default
Default destructor for Entity. It does not perform any special cleanup, as the Core is responsible fo...
static void RemoveTemporaryComponents(Core &core)
Remove all temporary components from the registry.
Definition Entity.cpp:15
EntityId Id() const
Get the EntityId associated with this Entity.
Definition Entity.cpp:11
decltype(auto) AddTemporaryComponent(TArgs &&...args)
Add a temporary component to an entity. Temporary component are removed when calling RemoveTemporaryC...
Definition Entity.ipp:19
Core & GetCore() const
Get a reference to the Core instance that this Entity belongs to. This is used internally to access t...
Definition Entity.cpp:21
Entity(Core &core, EntityId entityId)
Create an Entity from EntityId and the Core it belongs to.
Definition Entity.cpp:5
decltype(auto) AddComponent(TComponent &&component)
Add a component to an entity.
Definition Entity.ipp:4
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.ipp:41
bool IsAlive() const
Check if the entity is alive.
Definition Entity.cpp:7
bool HasComponents() const
Check if entity have one or multiple component's type.
Definition Entity.ipp:26
bool operator==(const Entity &rhs) const
Equality operator for Entity. It compares the underlying EntityId values to determine if two Entity i...
Definition Entity.cpp:17
EntityId _entityId
The EntityId that identifies the entity in the Core's registry. This is the underlying identifier for...
Definition Entity.hpp:129
decltype(auto) GetComponents()
Get components of type TComponent from the entity.
Definition Entity.ipp:31
void Kill()
Kill the entity, removing all components and making it invalid. After calling this method,...
Definition Entity.cpp:9
std::reference_wrapper< Core > _core
A reference to the Core instance that this Entity belongs to.
Definition Entity.hpp:125
void RemoveComponent()
Remove a component from an entity.
Definition Entity.ipp:24
Definition Core.hpp:17
Represents a unique identifier for an entity in the Engine's entity-component system....
Definition EntityId.hpp:14
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.ipp:48