This is a straightforward and accessible C++ Entity Component System (ECS) library that provides an uncomplicated way to manage game entities, components, and systems. ECS is a widely-used design pattern in game development that simplifies the organization of game objects' data and logic, making it ideal for both beginners and experienced developers.
The Entity Component System (ECS) architecture is commonly used in game development to structure game entities, their attributes (components), and the logic that operates on them (systems). This repository provides a user-friendly C++ implementation of an ECS framework, making it ideal for simple and accessible game development projects.
In ECS, entities, components, and systems play distinct roles in managing game data and behavior.
An entity is a fundamental object in the ECS architecture. It is essentially a container for components, representing a game object. Entities are lightweight and typically consist of an identifier and a collection of attached components.
All your entity class must inherit the ECS Entity
A component represents a specific aspect or attribute of an entity. Components are designed to be small, reusable, and self-contained, focusing on a single piece of functionality or data. For example, you might have components for graphics, physics, or behavior.
All your components class must inherit the ECS Component
A system is responsible for processing entities and their associated components. Systems encapsulate the logic that operates on entities and their components. Each system typically focuses on a specific aspect of the game, such as rendering, collision detection, or AI behavior.
All your systems class must inherit the ECS System
The ECSRegistry
class is the heart of this ECS library, providing methods for managing entities, components, and systems.
- Effortless Entity Management: Create, register, and remove game entities with simplicity in mind.
- Component Attachment Made Easy: Attach components to entities and remove them without hassle.
- Straightforward System Integration: Register systems to process entities and their components with ease.
- Dynamic and Efficient: Offers flexibility and performance through a dynamic component-based architecture.
- User-Friendly Type-Safe Operations: Utilizes C++ templates to ensure type safety when working with components, making it accessible for developers of all levels.
To use this ECS library in your project, follow these steps:
The code inside to ECSRegistry
class ECSRegistry {
public:
std::vector<Entity> mEntities;
std::unordered_map<size_t, std::vector<Component>> mComponents;
std::vector<System> mSystems;
};
To create an entity, follow these steps:
#include "ecs.hpp"
ecs::ECSRegistry* registry = new ecs::ECSRegistry;
ecs::Entity* entity = new ecs::Entity();
ecs::RegisterEntity(registry->mEntities, *entity);
//or
ecs::Entity entity;
ecs::RegisterEntity(registry->mEntities, entity);
To attach a component to an entity, follow these steps:
#include "ecs.hpp"
#include "TransformComponent.h" // Replace with your component class header
ecs::ECSRegistry* registry = new ecs::ECSRegistry;
ecs::Entity* entity = new ecs::Entity();
TransformComponent* component = new TransformComponent();
ecs::RegisterEntity(registry->mEntities, entity);
ecs::RegisterComponentToEntity(registry->mComponents ,entity, component);
To register a system, follow these steps:
#include "ecs.hpp"
#include "YourSystem.h" // Replace with your system class header
ecs::ECSRegistry* registry = new ecs::ECSRegistry;
ecs::RegisterSystem(registry->mSystems, new YourSystem(1, 0));
Here are some examples of how to use methods provided by ECSRegistry:
Method to reserve capacity for a vector.
ecs::ECSRegistry* registry = new ecs::ECSRegistry;
ecs::ReserveVectorCapacity(registry->mEntities, 100);
Method to reserve capacity for a map of vectors.
ecs::ECSRegistry* registry = new ecs::ECSRegistry;
ecs::ReserveMapCapacity<TransformComponent>(registry->mComponents, 100);
This project is licensed under the Apache-2.0 license.