High-level wrapper around Flecs, a powerful ECS (Entity Component System) library, written in Zig language
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 e728e47136 Fix and rename lookupType 10 months ago
libs Update Zig and Flecs, big refactor 10 months ago
src Fix and rename lookupType 10 months ago
.editorconfig Initial commit 1 year ago
.gitignore Initial commit 1 year ago
.gitmodules Re-add Flecs as a submodule, v3.2.5 1 year ago
README.md Use "flecszigble" name when important this lib 10 months ago
UNLICENSE.txt Initial commit 1 year ago
build.zig Update Zig and Flecs, big refactor 10 months ago
build.zig.zon Update Zig and Flecs, big refactor 10 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, flecs.c.EcsOnUpdate, "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();
    }
}