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.
copygirl
5e1ad2b90a
|
7 months ago | |
---|---|---|
libs/flecs | 8 months ago | |
src | 7 months ago | |
.editorconfig | 1 year ago | |
.gitignore | 1 year ago | |
README.md | 9 months ago | |
UNLICENSE.txt | 1 year ago | |
build.zig | 8 months ago | |
build.zig.zon | 8 months ago |
README.md
flecs-zig-ble
.. is a Zig wrapper around Flecs, the powerful and fast Entity Component System library. It provides higher-level abstractions around Flecs' primitives and uses Zig's meta-programming functionality for more easily readable and maintainable code.
This library is still very much work-in-progress and not ready for use. It's available for people to have a look, give feedback and take inspirations. I'm attempting to develop this project using a test-driven development approach, translating Flecs' C tests (and later, examples) to Zig, creating wrapper types and functions as I go.
Example
const std = @import("std");
const flecszigble = @import("flecs-zig-ble");
const Context = flecszigble.Context(void);
const World = Context.World;
const Iter = Context.Iter;
const Position = struct { x: f32, y: f32 };
const Velocity = struct { x: f32, y: f32 };
pub fn main() !void {
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
flecszigble.init(gpa.allocator());
const world = try World.init();
defer world.deinit();
// Register components.
_ = try world.component(Position);
_ = try world.component(Velocity);
// Register a system that moves any entity that has a `Position` by `Velocity` each update.
_ = try world.system("Move", move, null, "Position, [in] Velocity");
// 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(Velocity, .{ .x = 1, .y = 2 });
// Progress the world, updating it forever.
while (world.progress(0.0)) { }
}
fn move(it: Iter) void {
// Entities that share components are stored in the same table.
// 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.
const pos_col = it.field(Position, 1);
const vel_col = it.field(Velocity, 2);
// 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();
}
}