Simple strong typed I mean no pointers
ecs system. It’s inspired by fast-ecs.
- Entity: is an id number.
- Component: data storage POD.
- System: any logic/fuction/memfun that operate on these components.
- Archetype: a collection of entities that have the same components, we would call it components-store.
- Archetypes Manager: a collection of Archetypes.
- Entity: struct storing a number with a pointer to components-store to do useful stuff with it and it isn’t store in the system, so you mostly would do insertion with them.
- Component: any POD containing data otherwise you are abusing
ECS
. - Archetype: components-store, it’s a vector of
SortedTuples
. Sorted tuples arestd::tuple
with types rearranged in descending order to reduce memory difference when using tuple vs struct. this class supports adding new entity–components that Archetype contains–, checking for components existence, and removing/adding a componentthis method creates a new Archetype and they are intended to be used before ECS creation as we cannot change ECS type
. - Archetypes Manager: Class
ECS
contains a sorted tuple of Archetypes and support add new entity–creating new one–, along with running over all entities or all entities in a certain Archetype.
// COMPONENTS
struct Position {
float x, y;
};
struct Direction {
float angle;
};
struct Mesh {
std::string name;
};
// Archetypes
using Physics = ecs::Archetype<Position, Direction>;
using Render = ecs::Archetype<Position, Mesh>;
// example
int main(int, char*[]) {
auto world = ecs::ECS<Physics, Render>();
auto e1 = world.Add<Physics>(); // new entity from physics Archetype
e1.Set<Position>(1.4f, 1.134f);
auto e2 = world.Add<Render>();
e2.Set<Position>(200.24f, 100.0f);
e2.Set<Mesh>("ECS Rendering!");
// loop over physics entities
world.Run<Physics>([](auto& e) {
auto position = e.template Get<Position>();
auto direction = e.template Get<Direction>();
std::cout << position.x << std::endl;
std::cout << position.y << std::endl;
std::cout << direction.angle << std::endl;
e.template Set<Position>(128.0231f, 1231.0f);
});
return 0;
}
- CMake 3.0 or newer
- working C++17 compiler
- Compiler defines
__PRETTY_FUNCTION__
as constexpr
mkdir build
cd build
cmake ..
make
- [ ] add unit tests
- [ ] message passing!
See LICENSE file
Copyright (c) 2021 Islam Omar ([email protected])