You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
Stefan Partheymüller 6f0eacc8ca
add name for test case
2 years ago
..
.github/workflows Update ci.yml 4 years ago
.vscode fix commas 4 years ago
examples fix some compilation errors caused by language changes in zig v0.10.0 2 years ago
src add name for test case 2 years ago
tests Use std.debug.print instead of std.debug.warn 3 years ago
.gitignore ignore mac files 4 years ago
LICENSE Create LICENSE 3 years ago
README.md Update README.md 3 years ago
build.zig fix: Fix add package for zig 0.9.0 and master 0b22 3 years ago
zig.mod Create zig.mod 3 years ago

README.md

Zig ECS

Zig ECS is a zig port of the fantasic Entt. Entt is highly templated C++ code which depending on your opinion is either a good thing or satan itself in code form. Zig doesn't have the same concept as C++ templates (thank goodness!) so the templated code was changed over to use Zig's generics and compile time metaprogramming.

What does a zigified Entt look like?

Below are examples of a View and a Group, the two main ways to work with entities in the ecs along with the scaffolding code.

Declare some structs to work with:

pub const Velocity = struct { x: f32, y: f32 };
pub const Position = struct { x: f32, y: f32 };

Setup the Registry, which holds the entity data and is where we run our queries:

var reg = ecs.Registry.init(std.testing.allocator);

Create a couple entities and add some components to them

var entity = reg.create();
reg.add(entity, Position{ .x = 0, .y = 0 });
reg.add(entity, Velocity{ .x = 5, .y = 7 });
...

Create and iterate a View that matches all entities with a Velocity and Position component:

var view = reg.view(.{ Velocity, Position }, .{});

var iter = view.iterator();
while (iter.next()) |entity| {
    const pos = view.getConst(Position, entity); // readonly copy
    var vel = view.get(Velocity, entity); // mutable
}

The same example using an owning Group:

var group = reg.group(.{ Velocity, Position }, .{}, .{});
group.each(each);

fn each(e: struct { vel: *Velocity, pos: *Position }) void {
    e.pos.*.x += e.vel.x;
    e.pos.*.y += e.vel.y;
}