diff --git a/ecs/src/main.zig b/ecs/src/main.zig index d28d0e04..dd967daa 100644 --- a/ecs/src/main.zig +++ b/ecs/src/main.zig @@ -51,13 +51,13 @@ test "example" { const modules = Modules(.{ .physics = Module(.{ .components = .{ - .id = u16, + .id = u32, }, - .globals = struct{ + .globals = struct { pointer: u8, }, }), - .geometry = Module(.{ + .renderer = Module(.{ .components = .{ .id = u16, }, @@ -77,35 +77,8 @@ test "example" { const player2 = try world.entities.new(); const player3 = try world.entities.new(); try world.entities.setComponent(player1, .physics, .id, 1234); - try world.entities.setComponent(player1, .geometry, .id, 1234); + try world.entities.setComponent(player1, .renderer, .id, 1234); try world.entities.setComponent(player2, .physics, .id, 1234); try world.entities.setComponent(player3, .physics, .id, 1234); - - // TODO: integrate systems with modules. - // const physics = (struct { - // pub fn physics(adapter: *Adapter(modules)) void { - // var iter = adapter.query(&.{"physics"}); - // std.debug.print("\nphysics ran\n", .{}); - // while (iter.next()) |row| { - // std.debug.print("found entity: {}\n", .{row.entity}); - // defer row.unlock(); - // } - // } - // }).physics; - // try world.register("physics", physics); - - // const rendering = (struct { - // pub fn rendering(adapter: *Adapter(modules)) void { - // var iter = adapter.query(&.{"geometry"}); - // std.debug.print("\nrendering ran\n", .{}); - // while (iter.next()) |row| { - // std.debug.print("found entity: {}\n", .{row.entity}); - // defer row.unlock(); - // } - // } - // }).rendering; - // try world.register("rendering", rendering); - - world.tick(); } diff --git a/ecs/src/systems.zig b/ecs/src/systems.zig index 1612b0f4..071b1a73 100644 --- a/ecs/src/systems.zig +++ b/ecs/src/systems.zig @@ -6,20 +6,6 @@ const StructField = std.builtin.Type.StructField; const Entities = @import("entities.zig").Entities; -pub fn Adapter(modules: anytype) type { - const all_components = NamespacedComponents(modules); - return struct { - world: *World(modules), - - const Self = @This(); - pub const Iterator = Entities(all_components).Iterator; - - pub fn query(adapter: *Self, components: []const []const u8) Iterator { - return adapter.world.entities.query(components); - } - }; -} - /// An ECS module can provide components, systems, and global values. pub fn Module(comptime Params: anytype) @TypeOf(Params) { // TODO: validate the type @@ -168,12 +154,10 @@ pub fn World(comptime modules: anytype) type { const all_components = namespacedComponents(modules); return struct { allocator: Allocator, - systems: std.StringArrayHashMapUnmanaged(System) = .{}, entities: Entities(all_components), globals: NamespacedGlobals(modules), const Self = @This(); - pub const System = fn (adapter: *Adapter(modules)) void; pub fn init(allocator: Allocator) !Self { return Self{ @@ -184,7 +168,6 @@ pub fn World(comptime modules: anytype) type { } pub fn deinit(world: *Self) void { - world.systems.deinit(world.allocator); world.entities.deinit(); } @@ -214,25 +197,5 @@ pub fn World(comptime modules: anytype) type { @tagName(global_tag), ) = value; } - - pub fn register(world: *Self, name: []const u8, system: System) !void { - try world.systems.put(world.allocator, name, system); - } - - pub fn unregister(world: *Self, name: []const u8) void { - world.systems.orderedRemove(name); - } - - pub fn tick(world: *Self) void { - var i: usize = 0; - while (i < world.systems.count()) : (i += 1) { - const system = world.systems.entries.get(i).value; - - var adapter = Adapter(modules){ - .world = world, - }; - system(&adapter); - } - } }; }