master
Mike 5 years ago
parent f0c711123e
commit f2a302b7a4

@ -84,8 +84,9 @@ pub fn ComponentStorage(comptime CompT: type, comptime EntityT: type) type {
store.safe_swap = struct { store.safe_swap = struct {
fn swap(self: *Self, lhs: EntityT, rhs: EntityT) void { 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)]); std.mem.swap(CompT, &self.instances.items[self.set.index(lhs)], &self.instances.items[self.set.index(rhs)]);
}
self.set.swap(lhs, rhs); self.set.swap(lhs, rhs);
} }
}.swap; }.swap;
@ -103,8 +104,9 @@ pub fn ComponentStorage(comptime CompT: type, comptime EntityT: type) type {
self.update.deinit(); self.update.deinit();
self.destruction.deinit(); self.destruction.deinit();
if (self.allocator) |allocator| if (self.allocator) |allocator| {
allocator.destroy(self); allocator.destroy(self);
}
} }
pub fn onConstruct(self: *Self) Sink(EntityT) { 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 /// Increases the capacity of a component storage
pub fn reserve(self: *Self, cap: usize) void { pub fn reserve(self: *Self, cap: usize) void {
self.set.reserve(cap); self.set.reserve(cap);
if (!is_empty_struct) if (!is_empty_struct) {
self.instances.items.reserve(cap); elf.instances.items.reserve(cap);
}
} }
/// Assigns an entity to a storage and assigns its object /// Assigns an entity to a storage and assigns its object
pub fn add(self: *Self, entity: EntityT, value: CompT) void { 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.instances.append(value) catch unreachable;
}
self.set.add(entity); self.set.add(entity);
self.construction.publish(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 /// Removes an entity from a storage
pub fn remove(self: *Self, entity: EntityT) void { pub fn remove(self: *Self, entity: EntityT) void {
self.destruction.publish(entity); self.destruction.publish(entity);
if (!is_empty_struct) if (!is_empty_struct) {
_ = self.instances.swapRemove(self.set.index(entity)); _ = self.instances.swapRemove(self.set.index(entity));
}
self.set.remove(entity); self.set.remove(entity);
} }

Loading…
Cancel
Save