21using ComponentID = std::size_t;
22using GroupID = std::size_t;
29 static ComponentID lastID = 0u;
44using ComponentBitSet = std::bitset<MAX_COMPONENTS>;
45using ComponentArray = std::array<std::shared_ptr<Component>,
MAX_COMPONENTS>;
47using GroupBitSet = std::bitset<MAX_GROUPS>;
111 std::string
getID()
const;
169 [
this](
const std::shared_ptr<Component>&
mComponent) {
170 return dynamic_cast<T*>(mComponent.get()) != nullptr;
189 template<
typename T,
typename...
TArgs>
195 std::shared_ptr<T> x = std::make_shared<T>(std::forward<TArgs>(
mArgs)...);
197 components.emplace_back(x);
215 throw std::runtime_error(
"Component requested does not exist on entity.");
224 std::vector<std::shared_ptr<Component>> components;
226 ComponentArray componentArray;
227 ComponentBitSet componentBitSet;
228 GroupBitSet groupBitSet;
279 std::vector<std::shared_ptr<Entity>> entities;
280 std::array<std::vector<Entity*>,
MAX_GROUPS> groupedEntities;
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