Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/Catchouli/coment
A C++11 header-only ECS (entity-component-system) library (no longer updated but feel free to use or extend it)
https://github.com/Catchouli/coment
Last synced: 3 months ago
JSON representation
A C++11 header-only ECS (entity-component-system) library (no longer updated but feel free to use or extend it)
- Host: GitHub
- URL: https://github.com/Catchouli/coment
- Owner: catchouli
- Created: 2012-07-17T17:57:30.000Z (over 12 years ago)
- Default Branch: master
- Last Pushed: 2017-05-12T07:52:56.000Z (over 7 years ago)
- Last Synced: 2024-04-24T20:23:48.155Z (7 months ago)
- Language: C++
- Homepage:
- Size: 21.1 MB
- Stars: 55
- Watchers: 14
- Forks: 8
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
coment
A C++11 component-entity system
This branch is a complete rewrite of the original 1.0 branch, which was heavily influenced in design by C++03.
The library is header only. To use, simply add the include directory to your project/makefile's includes and try some of the following samples. For more fully-fledged samples and examples, check out the samples directory.
If your project uses boost, delete the boost directory from coment/include to prevent mixing boost versions. The included boost headers are from header-only libraries, so are standalone if you don't use boost.
Contact me (cat): (leave an issue)
Samples
The 'balls' sample is the simplest place to get started. For windows, the dependencies are included. On other platforms, SDL2 and opengl are required.
Usage
The World
The primary point of access to the engine is through the World class, which can be constructed like so:
```C++
#includecoment::World world;
```The world is a manager for Managers and Systems (more on those later.) It also provides proxy APIs for the default managers, which provide the default behaviour for coment.
Entities
An entity is an object in the world which has a number of components registered to it. These components represent the entity's data, and can be added to or removed from the entity at runtime. Entities can be created like so:
```C++
coment::Entity e = world.createEntity();
```Components
Components are data classes that can be added to an entity. It is recommended that these classes are pure data, but they can contain logic if it suits your design. Components generally inherit from coment::Component. This is not currently enforced, but may be in the future.
A component is created and added to an entity as follows:
```C++
struct Position
: coment::Component
{
Position(float x = 0.0f, float y = 0.0f) : x(x), y(y) {}
float x, y;
}coment::World world;
coment::Entity e = world.createEntity();
Position* p = e.addComponent(50.0f, 39.0f);
p = e.getComponent(); // unnecessary since addComponent already returns a pointer
e.removeComponent(); // p is now invalid
```Initialisation arguments can be passed into addComponent and are forwarded to one of the object's constructors.
Entities can then be automatically mapped based on their constituent components, allowing logic to be defined based on the entitys' data. These maps are automatically kept up to date when entities and components are added to or removed from the engine, and are represented internally by a flat array for efficient iteration.
```C++
auto moverMap = world.getEntityMap();for (auto& e : moverMap)
{
// Get components
Position* p = e.getComponent();
const Velocity* v = e.getComponent();
p.x += dt * v.x;
p.y += dt * v.y;
}
```Entities in moverMap are guaranteed to at least have the components Position and Velocity, but may have other components.
Entity::getComponent() will return nullptr if the entity does not have a component, which can be used to check if an entity has a component of a particular type and provide a default if not. (In other words, optional components.)
The pointers returned for components are temporary, and should not be stored. They may be reallocated at any time between callbacks when a coment API has been called.
Systems
Systems are classes managed by the world which provide game logic for your entities. To do this, the base System class defines callbacks which are used to notify all systems and allow them to do any necessary processing.
Systems can be added or removed from to the world in a similar way that components are added to entities. Similarly, arguments can also be passed for initialisation.
```C++
class MovementSystem
: public coment::System
{
void onUpdate() override
{
static auto moverMap = world.getEntityMap();
for (auto& e : moverMap)
{
// Get components
Position* p = e.getComponent();
const Velocity* v = e.getComponent();
p.x += dt * v.x;
p.y += dt * v.y;
}
}
};// Add the system to the world, so that it can start updating entities
world.addSystem();
```Available callbacks include: onEntityAdded(e), onEntityRemoved(e), preUpdate(), onUpdate(), and postUpdate().
Managers
Managers provide the behaviour for the whole engine, and are compartmentalised into Manager classes that are synchronised through callbacks similar to System callbacks. The following default managers are provided: EntityManager, ComponentManager. The Entity and World classes provide proxy APIs for these managers. They can also be obtained directly using World::getManager().
Custom mamangers can also be provided in a similar way to Systems. The callbacks provided include: onEntityAdded(e), onEntityRemoved(e), preUpdate(), postUpdate().