- Notifications
You must be signed in to change notification settings - Fork19
Zig build package and bindings forhttps://github.com/SanderMertens/flecs
License
NotificationsYou must be signed in to change notification settings
zig-gamedev/zflecs
Folders and files
| Name | Name | Last commit message | Last commit date | |
|---|---|---|---|---|
Repository files navigation
Zig build package and bindings forflecs ECS v4.1.1
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 usezflecs:
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)About
Zig build package and bindings forhttps://github.com/SanderMertens/flecs
Topics
Resources
License
Uh oh!
There was an error while loading.Please reload this page.
Stars
Watchers
Forks
Releases
No releases published
Packages0
No packages published
Uh oh!
There was an error while loading.Please reload this page.