From 4cd3d37657792c722a0437eea81c57b365404188 Mon Sep 17 00:00:00 2001 From: Mike Date: Wed, 3 Jun 2020 15:25:27 -0700 Subject: [PATCH] basic group ready kind of --- zig-ecs/src/ecs/groups.zig | 14 +++++++++++++- zig-ecs/src/ecs/registry.zig | 13 ++++++++++++- 2 files changed, 25 insertions(+), 2 deletions(-) diff --git a/zig-ecs/src/ecs/groups.zig b/zig-ecs/src/ecs/groups.zig index 2384c3b..02428d4 100644 --- a/zig-ecs/src/ecs/groups.zig +++ b/zig-ecs/src/ecs/groups.zig @@ -97,7 +97,7 @@ test "BasicGroup creation" { std.debug.assert(group.len() == 0); } -test "BasicGroup exclides" { +test "BasicGroup excludes" { var reg = Registry.init(std.testing.allocator); defer reg.deinit(); @@ -118,4 +118,16 @@ test "BasicGroup exclides" { reg.add(e0, @as(u32, 55)); std.debug.assert(group.len() == 0); +} + +test "BasicGroup create late" { + var reg = Registry.init(std.testing.allocator); + defer reg.deinit(); + + var e0 = reg.create(); + reg.add(e0, @as(i32, 44)); + reg.add(e0, @as(u32, 55)); + + var group = reg.group(.{}, .{ i32, u32 }, .{}); + std.testing.expectEqual(group.len(), 1); } \ No newline at end of file diff --git a/zig-ecs/src/ecs/registry.zig b/zig-ecs/src/ecs/registry.zig index 89c7c23..03cf99c 100644 --- a/zig-ecs/src/ecs/registry.zig +++ b/zig-ecs/src/ecs/registry.zig @@ -417,6 +417,7 @@ pub const Registry = struct { // we need to create a new GroupData var new_group_data = GroupData.initPtr(self.allocator, self, hash, &[_]u32{}, includes_arr[0..], excludes_arr[0..]); + self.groups.append(new_group_data) catch unreachable; // wire up our listeners inline for (owned) |t| self.onConstruct(t).connectBound(new_group_data, "maybeValidIf"); @@ -427,7 +428,17 @@ pub const Registry = struct { inline for (includes) |t| self.onDestruct(t).connectBound(new_group_data, "discardIf"); inline for (excludes) |t| self.onConstruct(t).connectBound(new_group_data, "discardIf"); - self.groups.append(new_group_data) catch unreachable; + // pre-fill the GroupData with any existing entitites that match + if (owned.len == 0) { + var tmp_view = self.view(owned ++ includes, excludes); + var view_iter = tmp_view.iterator(); + while (view_iter.next()) |entity| { + new_group_data.entity_set.add(entity); + } + } else { + unreachable; + } + return BasicGroup(includes.len, excludes.len).init(&new_group_data.entity_set, self, includes_arr, excludes_arr); }