ECSTASY
All in the name
Loading...
Searching...
No Matches
Entity.hpp
Go to the documentation of this file.
1
11
12#ifndef ECSTASY_RESOURCE_ENTITY_ENTITY_HPP_
13#define ECSTASY_RESOURCE_ENTITY_ENTITY_HPP_
14
15#include <compare>
16#include <stddef.h>
17#include <stdexcept>
18
21
22namespace ecstasy
23{
24 class Entities;
25
35 class Entity {
36 public:
38 using Index = size_t;
40 using Generation = size_t;
41
50 [[nodiscard]] constexpr Index getIndex() const noexcept
51 {
52 return _index;
53 }
54
67 [[nodiscard]] constexpr Generation getGeneration() const noexcept
68 {
69 return _generation;
70 }
71
78 [[nodiscard]] constexpr auto operator<=>(Entity const &other) const noexcept
79 {
80 return this->_index <=> other._index;
81 }
82
89 [[nodiscard]] constexpr bool operator==(Entity const &other) const noexcept
90 {
91 return this->_index == other._index;
92 }
93
110 template <IsStorage S, typename... Args>
111 typename S::Component &add(S &storage, Args &&...args)
112 {
113 if (storage.contains(_index)) [[unlikely]]
114 throw std::logic_error(std::string("Trying to add twice the component ")
115 + typeid(typename S::Component).name() + " on the same entity.");
116 return storage.emplace(_index, std::forward<Args>(args)...);
117 }
118
135 template <IsStorage S>
136 typename S::Component &operator[](S &storage)
137 {
138 if (!storage.contains(_index)) [[unlikely]]
139 return storage.emplace(_index);
140 return storage[_index];
141 }
142
157 template <IsStorage S>
158 const typename S::Component &operator[](S &storage) const
159 {
160 return storage[_index];
161 }
162
177 template <IsStorage S>
178 [[nodiscard]] const typename S::Component &get(S &storage) const
179 {
180 return storage[_index];
181 }
182
197 template <IsStorage S>
198 [[nodiscard]] typename S::Component &get(S &storage)
199 {
200 return storage[_index];
201 }
202
215 template <IsStorage S>
216 [[nodiscard]] bool has(S &storage) const
217 {
218 return storage.contains(_index);
219 }
220
221 protected:
226
236 constexpr Entity(Index index, Generation generation) noexcept : _index(index), _generation(generation){};
237
239 friend class ::ecstasy::Entities;
240 };
241} // namespace ecstasy
242
243#endif /* !ECSTASY_RESOURCE_ENTITY_ENTITY_HPP_ */
Abstract class for all components storage.
Storage concepts and utilities.
Encapsulate an index to an entity.
Definition Entity.hpp:35
constexpr Entity(Index index, Generation generation) noexcept
The entity structure may only be constructed by builders.
Definition Entity.hpp:236
constexpr Index getIndex() const noexcept
Get the entity identifier,.
Definition Entity.hpp:50
const S::Component & operator[](S &storage) const
Try to fetch the instance of component C associated to the current entity.
Definition Entity.hpp:158
constexpr Generation getGeneration() const noexcept
Get the Generation of the entity.
Definition Entity.hpp:67
bool has(S &storage) const
Test if the entity has an associated component in the storage S.
Definition Entity.hpp:216
constexpr bool operator==(Entity const &other) const noexcept
Compare two entities using only their identifier.
Definition Entity.hpp:89
constexpr auto operator<=>(Entity const &other) const noexcept
Compare two entities using only their identifier.
Definition Entity.hpp:78
S::Component & operator[](S &storage)
If the entity already has an instance of component C, returns it.
Definition Entity.hpp:136
size_t Generation
The entity generation type.
Definition Entity.hpp:40
S::Component & get(S &storage)
Try to fetch the instance of component C associated to the current entity.
Definition Entity.hpp:198
S::Component & add(S &storage, Args &&...args)
Add a component to the entity.
Definition Entity.hpp:111
size_t Index
The entity identifier type.
Definition Entity.hpp:38
Index _index
The entity identifier.
Definition Entity.hpp:223
Generation _generation
The entity generation.
Definition Entity.hpp:225
const S::Component & get(S &storage) const
Try to fetch the instance of component C associated to the current entity.
Definition Entity.hpp:178
Concept to check if a type is a storage.
Namespace containing all symbols specific to ecstasy.
Definition ecstasy.hpp:30