Nox Engine
 
Loading...
Searching...
No Matches
ECS.h
Go to the documentation of this file.
1
6#pragma once
7
8#include "Constants.h"
9#include <algorithm>
10#include <array>
11#include <bitset>
12#include <memory>
13#include <stdexcept>
14#include <string>
15#include <vector>
16
17struct Component;
18struct Entity;
19struct Manager;
20
21using ComponentID = std::size_t;
22using GroupID = std::size_t;
23
28inline ComponentID getNewComponentTypeID() {
29 static ComponentID lastID = 0u;
30 return lastID++;
31}
32
38template<typename T>
39inline ComponentID getComponentTypeID() noexcept {
40 static ComponentID typeID = getNewComponentTypeID();
41 return typeID;
42}
43
44using ComponentBitSet = std::bitset<MAX_COMPONENTS>;
45using ComponentArray = std::array<std::shared_ptr<Component>, MAX_COMPONENTS>;
46
47using GroupBitSet = std::bitset<MAX_GROUPS>;
48
53struct Component {
54public:
56
60 virtual void init() {}
61
65 virtual void update() {}
66
70 virtual void reload() {}
71
75 virtual void draw() {}
76
77 virtual ~Component() {}
78};
79
84struct Entity {
85public:
91 Entity(Manager& memManager, std::string _id);
92
96 void update();
100 void draw();
101
106 bool isActive() const;
111 std::string getID() const;
112
116 void enable();
120 void disable();
124 void refresh();
128 void reload();
129
135 bool hasGroup(GroupID memGroup);
140 void delGroup(GroupID memGroup);
145 void addGroup(GroupID memGroup);
146
152 template<typename T>
153 bool hasComponent() const {
154 return componentBitSet[getComponentTypeID<T>()];
155 }
156
161 template<typename T>
163 componentArray[getComponentTypeID<T>()] = nullptr;
164 componentBitSet[getComponentTypeID<T>()] = false;
165 components.erase(
166 std::remove_if(
167 components.begin(),
168 components.end(),
169 [this](const std::shared_ptr<Component>& mComponent) {
170 return dynamic_cast<T*>(mComponent.get()) != nullptr;
171 }
172 ),
173 components.end()
174 );
175 }
176
180 void deleteAllComponents();
181
189 template<typename T, typename... TArgs>
191 if (hasComponent<T>()) {
192 return *std::static_pointer_cast<T>(componentArray[getComponentTypeID<T>()]);
193 }
194
195 std::shared_ptr<T> x = std::make_shared<T>(std::forward<TArgs>(mArgs)...);
196 x->entity = this;
197 components.emplace_back(x);
198
199 componentArray[getComponentTypeID<T>()] = x;
200 componentBitSet[getComponentTypeID<T>()] = true;
201
202 x->init();
203 return *x;
204 }
205
212 template<typename T>
213 T& getComponent() const {
214 if (!componentArray[getComponentTypeID<T>()]) {
215 throw std::runtime_error("Component requested does not exist on entity.");
216 }
217 return *std::static_pointer_cast<T>(componentArray[getComponentTypeID<T>()]);
218 }
219
220private:
221 Manager& manager;
222 bool active = false;
223 std::string id;
224 std::vector<std::shared_ptr<Component>> components;
225
226 ComponentArray componentArray;
227 ComponentBitSet componentBitSet;
228 GroupBitSet groupBitSet;
229};
230
234struct Manager {
235public:
236 Manager();
237
241 void update();
245 void draw();
249 void refreshGroups();
253 void refresh();
254
260 void addToGroup(Entity* memEntity, GroupID memGroup);
266 std::vector<Entity*>& getGroupMembers(GroupID memGroup);
272 Entity& addEntity(std::string id);
276 void destroyAll();
277
278private:
279 std::vector<std::shared_ptr<Entity>> entities;
280 std::array<std::vector<Entity*>, MAX_GROUPS> groupedEntities;
281};
Defines global constants and enums used throughout the engine and game.
const std::size_t MAX_COMPONENTS
Maximum number of components an entity can have.
Definition Constants.h:29
const std::size_t MAX_GROUPS
Maximum number of groups available in the manager.
Definition Constants.h:30
ComponentID getNewComponentTypeID()
Generates a unique ID for a new component type at runtime.
Definition ECS.h:28
ComponentID getComponentTypeID() noexcept
Gets the unique ID for a given component type.
Definition ECS.h:39
The base class for all components in the ECS.
Definition ECS.h:53
virtual void init()
Called once when the component is added to an entity.
Definition ECS.h:60
virtual void update()
Called once per frame. Contains the component's main logic.
Definition ECS.h:65
virtual void draw()
Called once per frame after update. Used for rendering.
Definition ECS.h:75
Entity * entity
Pointer to the entity that owns this component.
Definition ECS.h:55
virtual void reload()
Called to reset the component's state.
Definition ECS.h:70
A general-purpose object in the game world.
Definition ECS.h:84
void update()
Calls the update method on all of its components.
Definition ECS.cpp:9
void enable()
Activates the entity, enabling its update and draw calls.
Definition ECS.cpp:31
void delComponent()
Removes a component of a specific type from the entity.
Definition ECS.h:162
void disable()
Deactivates the entity, disabling its update and draw calls.
Definition ECS.cpp:35
bool hasGroup(GroupID memGroup)
Checks if the entity belongs to a specific group.
Definition ECS.cpp:48
void deleteAllComponents()
Removes all components from the entity.
Definition ECS.cpp:63
void delGroup(GroupID memGroup)
Removes the entity from a specific group.
Definition ECS.cpp:52
void draw()
Calls the draw method on all of its components.
Definition ECS.cpp:16
void reload()
Calls the reload method on all of its components.
Definition ECS.cpp:41
bool hasComponent() const
Checks if the entity has a component of a specific type.
Definition ECS.h:153
void refresh()
Marks the entity to be refreshed in the manager's group lists.
Definition ECS.cpp:39
void addGroup(GroupID memGroup)
Adds the entity to a specific group.
Definition ECS.cpp:56
T & addComponent(TArgs &&... mArgs)
Adds a component to the entity.
Definition ECS.h:190
std::string getID() const
Gets the entity's string identifier.
Definition ECS.cpp:27
T & getComponent() const
Retrieves a component from the entity.
Definition ECS.h:213
bool isActive() const
Checks if the entity is currently active.
Definition ECS.cpp:23
Manages all entities and their groups within the game.
Definition ECS.h:234
Entity & addEntity(std::string id)
Creates a new entity and adds it to the manager.
Definition ECS.cpp:129
std::vector< Entity * > & getGroupMembers(GroupID memGroup)
Retrieves all entities belonging to a specific group.
Definition ECS.cpp:125
void addToGroup(Entity *memEntity, GroupID memGroup)
Adds an entity to a specified group.
Definition ECS.cpp:121
void refreshGroups()
Re-evaluates group memberships for all entities.
Definition ECS.cpp:90
void destroyAll()
Destroys all entities managed by this object.
Definition ECS.cpp:135
void draw()
Draws all active entities.
Definition ECS.cpp:84
void refresh()
Removes disabled entities and re-evaluates groups.
Definition ECS.cpp:104
void update()
Updates all active entities.
Definition ECS.cpp:77