lamecs
is a header only library for sparse-set based "pure" ecs implementation
#define LAMECS_INFO_ENABLED
#include "lamecs.hpp"
struct pos
{
int x, y, z;
};
struct vel
{
int dx, dy, dz;
};
int main()
{
lamecs::registry registry;
auto e1 = registry.create_entity();
auto e2 = registry.create_entity();
auto e3 = registry.create_entity();
// you dont have to do this since components are registered during .emplace()
registry.register_component<pos>();
// add or overwrite specific component for and entity
registry.emplace<pos>(e1, {0, 0, 0});
registry.emplace<pos>(e2, {0, 0, 1});
registry.emplace<vel>(e1, {1, 0, 0});
registry.emplace<vel>(e2, {0, 1, 1});
registry.emplace<vel>(e3, {0, 1, 3});
// remove component from entity
registry.remove<vel>(e2);
registry.remove_entity(e3);
// access and modify specific components of an entity
auto [p, v] = registry.get_entity<pos, vel>(e1);
// callback style iterating
registry.each<vel>([®istry](lamecs::entity_id id, vel& v)
{
//...
});
registry.each<vel, pos>([®istry](vel& v, pos& p)
{
//...
});
// you can also create "views" to access entities with specific components
for(auto& [id, pos, vel] : registry.view<pos, vel>())
{
//...
}
return 0;
}
It is far from perfect but it was nice learning for me to understand data oriented design and C++ templates
A shout-out to Michele Caini's EnTT and ECS back and forth series, and Chris Christakis's seecs which were awesome learning materials