|
|
@ -11,6 +11,7 @@ This library is still very much work-in-progress and not ready for use. It's ava |
|
|
|
|
|
|
|
|
|
|
|
```zig |
|
|
|
```zig |
|
|
|
const std = @import("std"); |
|
|
|
const std = @import("std"); |
|
|
|
|
|
|
|
|
|
|
|
const flecs = @import("flecs-zig-ble"); |
|
|
|
const flecs = @import("flecs-zig-ble"); |
|
|
|
const Context = flecs.Context(void); |
|
|
|
const Context = flecs.Context(void); |
|
|
|
const World = Context.World; |
|
|
|
const World = Context.World; |
|
|
@ -26,25 +27,35 @@ pub fn main() !void { |
|
|
|
const world = try World.init(); |
|
|
|
const world = try World.init(); |
|
|
|
defer world.deinit(); |
|
|
|
defer world.deinit(); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Register components. |
|
|
|
_ = try world.component(Position); |
|
|
|
_ = try world.component(Position); |
|
|
|
_ = try world.component(Velocity); |
|
|
|
_ = try world.component(Velocity); |
|
|
|
|
|
|
|
|
|
|
|
_ = try world.system("Move", move, flecs.c.EcsOnUpdate, "Position, Velocity"); |
|
|
|
// Register a system that moves any entity that has a `Position` by `Velocity` each update. |
|
|
|
|
|
|
|
_ = try world.system("Move", move, flecs.c.EcsOnUpdate, "Position, [in] Velocity"); |
|
|
|
|
|
|
|
|
|
|
|
const e = world.entity(); |
|
|
|
// Create an entity with both `Position` and `Velocity` already pre-added. |
|
|
|
|
|
|
|
const e = try world.entity(.{ .name = "Wroom" }, .{ Position, Velocity }); |
|
|
|
|
|
|
|
// However the components aren't initialized, so do that here: |
|
|
|
e.set(Position, .{ .x = 10, .y = 20 }); |
|
|
|
e.set(Position, .{ .x = 10, .y = 20 }); |
|
|
|
e.set(Velocity, .{ .x = 1, .y = 2 }); |
|
|
|
e.set(Velocity, .{ .x = 1, .y = 2 }); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Progress the world, updating it forever. |
|
|
|
while (world.progress(0.0)) { } |
|
|
|
while (world.progress(0.0)) { } |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
fn move(it: Iter) void { |
|
|
|
fn move(it: Iter) void { |
|
|
|
const pos_field = it.field(Position, 1); |
|
|
|
// Entities that share components are stored in the same table. |
|
|
|
const vel_field = it.field(Velocity, 2); |
|
|
|
// This function will be called for each table that matches the query. |
|
|
|
|
|
|
|
// With `field` we can grab a column from this table as a typed slice. |
|
|
|
for (pos_field, vel_field) |*p, v| { |
|
|
|
const pos_col = it.field(Position, 1); |
|
|
|
p.x += v.x; |
|
|
|
const vel_col = it.field(Velocity, 2); |
|
|
|
p.y += v.y; |
|
|
|
|
|
|
|
|
|
|
|
// Both columns have the same size, so we can iterate them at the same time. |
|
|
|
|
|
|
|
// Since we want to modify `Position` we can get a pointer to the element. |
|
|
|
|
|
|
|
for (pos_col, vel_col) |*pos, vel| { |
|
|
|
|
|
|
|
pos.x += vel.x * it.deltaTime(); |
|
|
|
|
|
|
|
pos.y += vel.y * it.deltaTime(); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
``` |
|
|
|
``` |
|
|
|