Easys
A minimalist, header-only C++ ECS library for efficient and fuss-free entity and component management.
Loading...
Searching...
No Matches
ecs.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <functional>
4#include <iostream>
5#include <memory>
6#include <queue>
7#include <set>
8
9#include "entity.hpp"
10#include "registry.hpp"
11
12namespace Easys {
13
18template <typename... AllComponentTypes>
19class ECS {
20 public:
26 {
27 for (Entity entity = 0; entity < MAX_ENTITIES; entity++)
28 {
29 availableEntityIds_.push(entity);
30 }
31 }
32
39 ECS(const std::set<Entity>& oldEntities)
40 {
41 // I decided against an addEntity(Entity) method to discourage
42 // tampering with entities too much. I think this really should be the ECS's
43 // responsibility.
44 for (Entity entity = 0; entity < MAX_ENTITIES; entity++)
45 {
46 if (oldEntities.contains(entity))
47 entities_.insert(entity);
48 else
49 availableEntityIds_.push(entity);
50 }
51 }
52
59 {
61 {
62 Entity e = availableEntityIds_.front();
63 availableEntityIds_.pop();
64 entities_.insert(e);
65 return e;
66 }
67 // throwing an exception here seems kind of drastic, but on the other hand
68 // maybe not
69 throw std::runtime_error("MAX NUMBER OF ENTITIES REACHED!");
70 }
71
77 inline void removeEntity(const Entity e)
78 {
79 // Remove all components associated with the entity
80 registry_.removeComponents(e);
81 // Remove entity from the set of active entities_
82 entities_.erase(e);
83 // Make the entity ID available again
84 availableEntityIds_.push(e);
85 }
86
92 inline bool hasEntity(const Entity e) const { return entities_.contains(e); }
93
98 inline const std::set<Entity>& getEntities() const { return entities_; }
99
105 template <typename T>
106 inline const std::vector<Entity>& getEntitiesByComponent() const
107 {
108 return registry_.template getEntitiesByComponent<T>();
109 }
110
117 template <typename... Ts>
118 inline std::vector<Entity> getEntitiesByComponents() const
119 {
120 return registry_.template getEntitiesByComponents<Ts...>();
121 }
122
127 inline size_t getEntityCount() const { return entities_.size(); }
128
136 template <typename T>
137 inline void addComponent(const Entity e, T component)
138 {
139 registry_.addComponent(e, std::move(component));
140 }
141
147 template <typename T>
148 inline void removeComponent(const Entity e)
149 {
150 registry_.template removeComponent<T>(e);
151 }
152
157 inline void removeComponents(const Entity e)
158 {
159 registry_.removeComponents(e);
160 }
161
167 template <typename... T>
168 inline void removeComponents(const Entity e)
169 {
170 registry_.template removeComponents<T...>(e);
171 }
172
179 template <typename T>
180 inline T& getComponent(const Entity e)
181 {
182 return registry_.template getComponent<T>(e);
183 }
184
191 template <typename T>
192 inline const T& getComponent(const Entity e) const
193 {
194 return registry_.template getComponent<T>(e);
195 }
196
203 template <typename T>
204 inline bool hasComponent(const Entity e) const
205 {
206 return registry_.template hasComponent<T>(e);
207 }
208
215 template <typename... Ts>
216 inline size_t getComponentCount() const
217 {
218 return registry_.template size<Ts...>();
219 }
220
221 inline size_t getComponentCount() const { return registry_.size(); }
222
227 inline void clear()
228 {
229 registry_.clear();
230 clearEntities();
231 }
232
238 template <typename... Ts>
239 inline void clearComponents()
240 {
241 registry_.template clear<Ts...>();
242 }
243
244 inline void clearComponents() { registry_.clear(); }
245
246 private:
247 std::queue<Entity> availableEntityIds_;
248 std::set<Entity> entities_;
249 Registry<AllComponentTypes...> registry_;
250
251 void clearEntities()
252 {
253 entities_.clear();
254
255 std::queue<Entity> empty;
256 std::swap(availableEntityIds_, empty);
257
258 for (Entity entity = 0; entity < MAX_ENTITIES; entity++) availableEntityIds_.push(entity);
259 }
260};
261
262} // namespace Easys
Manages entities and components in an Entity-Component-System architecture.
Definition ecs.hpp:19
void addComponent(const Entity e, T component)
Adds a component of type T to an entity.
Definition ecs.hpp:137
Entity addEntity()
Adds a new entity to the ECS.
Definition ecs.hpp:58
void clearComponents()
Definition ecs.hpp:244
const T & getComponent(const Entity e) const
Retrieves a reference to a component of type T from an entity.
Definition ecs.hpp:192
size_t getEntityCount() const
Returns the total number of active entities in the ECS.
Definition ecs.hpp:127
size_t getComponentCount() const
Returns the total count of components of the specified types within the ECS.
Definition ecs.hpp:216
void removeEntity(const Entity e)
Removes an entity and all its associated components from the ECS.
Definition ecs.hpp:77
const std::set< Entity > & getEntities() const
Returns a reference to the set of all entities.
Definition ecs.hpp:98
void clear()
Clears all entities and components from the ECS.
Definition ecs.hpp:227
T & getComponent(const Entity e)
Retrieves a reference to a component of type T from an entity.
Definition ecs.hpp:180
std::vector< Entity > getEntitiesByComponents() const
Returns a vector of entities that have all of the specified component types. Use smaller components f...
Definition ecs.hpp:118
size_t getComponentCount() const
Definition ecs.hpp:221
ECS(const std::set< Entity > &oldEntities)
Initializes the ECS with a specific set of entities.
Definition ecs.hpp:39
bool hasEntity(const Entity e) const
Checks if an entity exists within the ECS.
Definition ecs.hpp:92
void clearComponents()
Removes components of specific types from all entities within the ECS.
Definition ecs.hpp:239
ECS()
Initializes the ECS with a predefined maximum number of entities (MAX_ENTITIES).
Definition ecs.hpp:25
void removeComponents(const Entity e)
Removes all components from an entity.
Definition ecs.hpp:157
void removeComponent(const Entity e)
Removes a component of type T from an entity.
Definition ecs.hpp:148
bool hasComponent(const Entity e) const
Checks if an entity has a component of type T.
Definition ecs.hpp:204
const std::vector< Entity > & getEntitiesByComponent() const
Returns a vector of entities that have a component of a specific type.
Definition ecs.hpp:106
void removeComponents(const Entity e)
Removes all components of types T from an entity.
Definition ecs.hpp:168
Definition registry.hpp:15
size_t size() const
Definition registry.hpp:117
void addComponent(const Entity entity, const ComponentType &component)
Definition registry.hpp:21
void clear()
Definition registry.hpp:144
void removeComponents(const Entity entity)
Definition registry.hpp:34
Definition ecs.hpp:12
const Entity MAX_ENTITIES
Definition entity.hpp:12
EASYS_ENTITY_TYPE Entity
Definition entity.hpp:11