kronkworld
A light ECS, in C++.
Overview
KronkWorld is a small ECS (Entity Component System) for C++20. Entities are plain ids. Components are any C++ type, stored in one packed array per type. Systems are classes with a handle(World&) method, run by stage. Resources are global singletons owned by the world: window, delta time, score.
The whole API fits in kw::World. KronkWorld relies on kronkflow to schedule systems. It is the core of KUGE’s scenes.
Changelog
1.0.0
First stable release
- world.spawn creates an entity with all its components in one call.
- Fix: iterating an empty view no longer goes wrong.
- Builds on Windows (MSVC); kronklab and kronkflow are pinned to a set version.
- New regression test for World::remove on a component.
Examples
struct Position { float x, y; };
struct Velocity { float dx, dy; };
struct Player {};
// Create an entity with its components in one go
kw::Entity player = world.spawn(Player{}, Position{0.f, 0.f}, Velocity{60.f, 0.f});
// ...or step by step
kw::Entity rock = world.create();
world.add<Position>(rock, 10.f, 10.f); // constructor args are forwarded
world.get<Position>(rock).x += 1.f; // read / write
bool moves = world.has<Velocity>(rock); // false
world.remove<Position>(rock); // remove one component
world.remove(player); // destroy the entity (ids are recycled) // Callback style
world.view<Position, Velocity>().foreach(
[](kw::Entity e, Position& pos, Velocity& vel) {
pos.x += vel.dx;
pos.y += vel.dy;
});
// Range-for style
for (kw::Entity e : world.view<Player, Position>())
world.get<Position>(e).x = 0.f; class MovementSystem : public kw::ISystem {
public:
bool handle(kw::World& world) override
{
const float dt = world.getResource<Dt>().seconds;
world.view<Position, Velocity>().foreach(
[dt](kw::Entity, Position& pos, Velocity& vel) {
pos.x += vel.dx * dt;
pos.y += vel.dy * dt;
});
return true;
}
};
enum Stage : size_t { Startup, Update, Render };
world.addSystem(Update, std::make_unique<MovementSystem>());