Zig build package and bindings for flecs ECS v4.1.5
Examplebuild.zig:
pubfnbuild(b: *std.Build) void {
constexe=b.addExecutable(.{ ... });
constzflecs=b.dependency("zflecs", .{});
exe.root_module.addImport("zflecs", zflecs.module("root"));
exe.linkLibrary(zflecs.artifact("flecs"));
}Now in your code you may import and use zflecs:
conststd=@import("std");
constecs=@import("zflecs");
constPosition=struct { x: f32, y: f32 };
constVelocity=struct { x: f32, y: f32 };
constEats=struct {};
constApples=struct {};
fnmove_system(positions: []Position, velocities: []constVelocity) void {
for (positions, velocities) |*p, v| {
p.x+=v.x;
p.y+=v.y;
}
}
//Optionally, systems can receive the components iterator (usually not necessary)fnmove_system_with_it(it: *ecs.iter_t, positions: []Position, velocities: []constVelocity) void {
consttype_str=ecs.table_str(it.world, it.table).?;
std.debug.print("Move entities with [{s}]\n", .{type_str});
deferecs.os.free(type_str);
for (positions, velocities) |*p, v| {
p.x+=v.x;
p.y+=v.y;
}
}
pubfnmain() !void {
constworld=ecs.init();
defer_=ecs.fini(world);
ecs.COMPONENT(world, Position);
ecs.COMPONENT(world, Velocity);
ecs.TAG(world, Eats);
ecs.TAG(world, Apples);
_=ecs.ADD_SYSTEM(world, "move system", ecs.OnUpdate, move_system);
_=ecs.ADD_SYSTEM(world, "move system with iterator", ecs.OnUpdate, move_system_with_it);
constbob=ecs.new_entity(world, "Bob");
_=ecs.set(world, bob, Position, .{ .x=0, .y=0 });
_=ecs.set(world, bob, Velocity, .{ .x=1, .y=2 });
ecs.add_pair(world, bob, ecs.id(Eats), ecs.id(Apples));
_=ecs.progress(world, 0);
_=ecs.progress(world, 0);
constp=ecs.get(world, bob, Position).?;
std.debug.print("Bob's position is ({d}, {d})\n", .{ p.x, p.y });
}zig build run should result in:
Move entities with [main.Position, main.Velocity, (Identifier,Name), (main.Eats,main.Apples)]
Move entities with [main.Position, main.Velocity, (Identifier,Name), (main.Eats,main.Apples)]
Bob's position is (4, 8)