From f2a302b7a451b729173f02b6bda33b8c707acc1b Mon Sep 17 00:00:00 2001 From: Mike Date: Thu, 11 Jun 2020 17:16:28 -0700 Subject: [PATCH] cleaning --- zig-ecs/src/ecs/component_storage.zig | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/zig-ecs/src/ecs/component_storage.zig b/zig-ecs/src/ecs/component_storage.zig index 6a2556d..954241a 100644 --- a/zig-ecs/src/ecs/component_storage.zig +++ b/zig-ecs/src/ecs/component_storage.zig @@ -84,8 +84,9 @@ pub fn ComponentStorage(comptime CompT: type, comptime EntityT: type) type { store.safe_swap = struct { fn swap(self: *Self, lhs: EntityT, rhs: EntityT) void { - if (!is_empty_struct) + if (!is_empty_struct) { std.mem.swap(CompT, &self.instances.items[self.set.index(lhs)], &self.instances.items[self.set.index(rhs)]); + } self.set.swap(lhs, rhs); } }.swap; @@ -103,8 +104,9 @@ pub fn ComponentStorage(comptime CompT: type, comptime EntityT: type) type { self.update.deinit(); self.destruction.deinit(); - if (self.allocator) |allocator| + if (self.allocator) |allocator| { allocator.destroy(self); + } } pub fn onConstruct(self: *Self) Sink(EntityT) { @@ -122,14 +124,16 @@ pub fn ComponentStorage(comptime CompT: type, comptime EntityT: type) type { /// Increases the capacity of a component storage pub fn reserve(self: *Self, cap: usize) void { self.set.reserve(cap); - if (!is_empty_struct) - self.instances.items.reserve(cap); + if (!is_empty_struct) { + elf.instances.items.reserve(cap); + } } /// Assigns an entity to a storage and assigns its object pub fn add(self: *Self, entity: EntityT, value: CompT) void { - if (!is_empty_struct) + if (!is_empty_struct) { _ = self.instances.append(value) catch unreachable; + } self.set.add(entity); self.construction.publish(entity); } @@ -137,8 +141,9 @@ pub fn ComponentStorage(comptime CompT: type, comptime EntityT: type) type { /// Removes an entity from a storage pub fn remove(self: *Self, entity: EntityT) void { self.destruction.publish(entity); - if (!is_empty_struct) + if (!is_empty_struct) { _ = self.instances.swapRemove(self.set.index(entity)); + } self.set.remove(entity); }