2020-05-31 21:28:29 -07:00
|
|
|
const std = @import("std");
|
|
|
|
const warn = std.debug.warn;
|
|
|
|
const utils = @import("utils.zig");
|
|
|
|
|
|
|
|
const SparseSet = @import("sparse_set.zig").SparseSet;
|
2020-06-01 20:05:07 -07:00
|
|
|
const Signal = @import("../signals/signal.zig").Signal;
|
|
|
|
const Sink = @import("../signals/sink.zig").Sink;
|
2020-05-31 21:28:29 -07:00
|
|
|
|
2020-06-04 19:19:29 -07:00
|
|
|
/// Stores an ArrayList of components along with a SparseSet of entities
|
|
|
|
pub fn ComponentStorage(comptime CompT: type, comptime EntityT: type) type {
|
2020-05-31 21:28:29 -07:00
|
|
|
std.debug.assert(!utils.isComptime(CompT));
|
|
|
|
|
|
|
|
// empty (zero-sized) structs will not have an array created
|
|
|
|
comptime const is_empty_struct = @sizeOf(CompT) == 0;
|
|
|
|
|
|
|
|
// HACK: due to this being stored as untyped ptrs, when deinit is called we are casted to a CompT of some random
|
|
|
|
// non-zero sized type. That will make is_empty_struct false in deinit always so we can't use it. Instead, we stick
|
|
|
|
// a small dummy struct in the instances ArrayList so it can safely be deallocated.
|
|
|
|
// Perhaps we should just allocate instances with a dummy allocator or the tmp allocator?
|
2020-06-11 22:28:29 -07:00
|
|
|
comptime var CompOrAlmostEmptyT = if (is_empty_struct) struct { dummy: u1 } else CompT;
|
2020-05-31 21:28:29 -07:00
|
|
|
|
|
|
|
return struct {
|
|
|
|
const Self = @This();
|
|
|
|
|
2020-06-04 19:19:29 -07:00
|
|
|
set: *SparseSet(EntityT),
|
2020-05-31 21:28:29 -07:00
|
|
|
instances: std.ArrayList(CompOrAlmostEmptyT),
|
|
|
|
allocator: ?*std.mem.Allocator,
|
2020-06-11 22:28:29 -07:00
|
|
|
/// doesnt really belong here...used to denote group ownership
|
|
|
|
super: usize = 0,
|
2020-05-31 21:28:29 -07:00
|
|
|
safe_deinit: fn (*Self) void,
|
2020-06-03 23:33:59 -07:00
|
|
|
safe_swap: fn (*Self, EntityT, EntityT) void,
|
2020-06-01 20:05:07 -07:00
|
|
|
construction: Signal(EntityT),
|
|
|
|
update: Signal(EntityT),
|
|
|
|
destruction: Signal(EntityT),
|
2020-05-31 21:28:29 -07:00
|
|
|
|
|
|
|
pub fn init(allocator: *std.mem.Allocator) Self {
|
|
|
|
var store = Self{
|
2020-06-04 19:19:29 -07:00
|
|
|
.set = SparseSet(EntityT).initPtr(allocator),
|
2020-05-31 21:28:29 -07:00
|
|
|
.instances = undefined,
|
|
|
|
.safe_deinit = struct {
|
|
|
|
fn deinit(self: *Self) void {
|
2020-06-11 22:28:29 -07:00
|
|
|
if (!is_empty_struct) {
|
2020-05-31 21:28:29 -07:00
|
|
|
self.instances.deinit();
|
2020-06-11 22:28:29 -07:00
|
|
|
}
|
2020-05-31 21:28:29 -07:00
|
|
|
}
|
|
|
|
}.deinit,
|
2020-06-03 23:33:59 -07:00
|
|
|
.safe_swap = struct {
|
|
|
|
fn swap(self: *Self, lhs: EntityT, rhs: EntityT) void {
|
2020-06-11 22:28:29 -07:00
|
|
|
if (!is_empty_struct) {
|
2020-06-03 23:33:59 -07:00
|
|
|
std.mem.swap(CompT, &self.instances.items[self.set.index(lhs)], &self.instances.items[self.set.index(rhs)]);
|
2020-06-11 22:28:29 -07:00
|
|
|
}
|
2020-06-03 23:33:59 -07:00
|
|
|
self.set.swap(lhs, rhs);
|
|
|
|
}
|
|
|
|
}.swap,
|
2020-05-31 21:28:29 -07:00
|
|
|
.allocator = null,
|
2020-06-01 20:05:07 -07:00
|
|
|
.construction = Signal(EntityT).init(allocator),
|
|
|
|
.update = Signal(EntityT).init(allocator),
|
|
|
|
.destruction = Signal(EntityT).init(allocator),
|
2020-05-31 21:28:29 -07:00
|
|
|
};
|
|
|
|
|
2020-06-11 22:28:29 -07:00
|
|
|
if (!is_empty_struct) {
|
2020-05-31 21:28:29 -07:00
|
|
|
store.instances = std.ArrayList(CompOrAlmostEmptyT).init(allocator);
|
2020-06-11 22:28:29 -07:00
|
|
|
}
|
2020-05-31 21:28:29 -07:00
|
|
|
|
|
|
|
return store;
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn initPtr(allocator: *std.mem.Allocator) *Self {
|
|
|
|
var store = allocator.create(Self) catch unreachable;
|
2020-06-04 19:19:29 -07:00
|
|
|
store.set = SparseSet(EntityT).initPtr(allocator);
|
2020-06-11 22:28:29 -07:00
|
|
|
if (!is_empty_struct) {
|
2020-05-31 21:28:29 -07:00
|
|
|
store.instances = std.ArrayList(CompOrAlmostEmptyT).init(allocator);
|
2020-06-11 22:28:29 -07:00
|
|
|
}
|
2020-05-31 21:28:29 -07:00
|
|
|
store.allocator = allocator;
|
2020-06-07 17:28:42 -07:00
|
|
|
store.super = 0;
|
2020-06-01 20:05:07 -07:00
|
|
|
store.construction = Signal(EntityT).init(allocator);
|
|
|
|
store.update = Signal(EntityT).init(allocator);
|
|
|
|
store.destruction = Signal(EntityT).init(allocator);
|
2020-05-31 21:28:29 -07:00
|
|
|
|
|
|
|
// since we are stored as a pointer, we need to catpure this
|
|
|
|
store.safe_deinit = struct {
|
|
|
|
fn deinit(self: *Self) void {
|
2020-06-11 22:28:29 -07:00
|
|
|
if (!is_empty_struct) {
|
2020-05-31 21:28:29 -07:00
|
|
|
self.instances.deinit();
|
2020-06-11 22:28:29 -07:00
|
|
|
}
|
2020-05-31 21:28:29 -07:00
|
|
|
}
|
|
|
|
}.deinit;
|
|
|
|
|
2020-06-03 23:33:59 -07:00
|
|
|
store.safe_swap = struct {
|
|
|
|
fn swap(self: *Self, lhs: EntityT, rhs: EntityT) void {
|
2020-06-11 17:16:28 -07:00
|
|
|
if (!is_empty_struct) {
|
2020-06-03 23:33:59 -07:00
|
|
|
std.mem.swap(CompT, &self.instances.items[self.set.index(lhs)], &self.instances.items[self.set.index(rhs)]);
|
2020-06-11 17:16:28 -07:00
|
|
|
}
|
2020-06-03 23:33:59 -07:00
|
|
|
self.set.swap(lhs, rhs);
|
|
|
|
}
|
|
|
|
}.swap;
|
|
|
|
|
2020-05-31 21:28:29 -07:00
|
|
|
return store;
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn deinit(self: *Self) void {
|
|
|
|
// great care must be taken here. Due to how Registry keeps this struct as pointers anything touching a type
|
|
|
|
// will be wrong since it has to cast to a random struct when deiniting. Because of all that, is_empty_struct
|
|
|
|
// will allways be false here so we have to deinit the instances no matter what.
|
|
|
|
self.safe_deinit(self);
|
|
|
|
self.set.deinit();
|
2020-06-01 20:05:07 -07:00
|
|
|
self.construction.deinit();
|
|
|
|
self.update.deinit();
|
|
|
|
self.destruction.deinit();
|
2020-05-31 21:28:29 -07:00
|
|
|
|
2020-06-11 17:16:28 -07:00
|
|
|
if (self.allocator) |allocator| {
|
2020-05-31 21:28:29 -07:00
|
|
|
allocator.destroy(self);
|
2020-06-11 17:16:28 -07:00
|
|
|
}
|
2020-05-31 21:28:29 -07:00
|
|
|
}
|
|
|
|
|
2020-06-01 20:05:07 -07:00
|
|
|
pub fn onConstruct(self: *Self) Sink(EntityT) {
|
|
|
|
return self.construction.sink();
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn onUpdate(self: *Self) Sink(EntityT) {
|
|
|
|
return self.update.sink();
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn onDestruct(self: *Self) Sink(EntityT) {
|
|
|
|
return self.destruction.sink();
|
|
|
|
}
|
|
|
|
|
2020-05-31 21:28:29 -07:00
|
|
|
/// Increases the capacity of a component storage
|
|
|
|
pub fn reserve(self: *Self, cap: usize) void {
|
|
|
|
self.set.reserve(cap);
|
2020-06-11 17:16:28 -07:00
|
|
|
if (!is_empty_struct) {
|
|
|
|
elf.instances.items.reserve(cap);
|
|
|
|
}
|
2020-05-31 21:28:29 -07:00
|
|
|
}
|
|
|
|
|
2020-06-01 20:05:07 -07:00
|
|
|
/// Assigns an entity to a storage and assigns its object
|
2020-05-31 21:28:29 -07:00
|
|
|
pub fn add(self: *Self, entity: EntityT, value: CompT) void {
|
2020-06-11 17:16:28 -07:00
|
|
|
if (!is_empty_struct) {
|
2020-05-31 21:28:29 -07:00
|
|
|
_ = self.instances.append(value) catch unreachable;
|
2020-06-11 17:16:28 -07:00
|
|
|
}
|
2020-05-31 21:28:29 -07:00
|
|
|
self.set.add(entity);
|
2020-06-01 20:05:07 -07:00
|
|
|
self.construction.publish(entity);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Removes an entity from a storage
|
|
|
|
pub fn remove(self: *Self, entity: EntityT) void {
|
2020-06-03 23:33:59 -07:00
|
|
|
self.destruction.publish(entity);
|
2020-06-11 17:16:28 -07:00
|
|
|
if (!is_empty_struct) {
|
2020-06-01 20:05:07 -07:00
|
|
|
_ = self.instances.swapRemove(self.set.index(entity));
|
2020-06-11 17:16:28 -07:00
|
|
|
}
|
2020-06-01 20:05:07 -07:00
|
|
|
self.set.remove(entity);
|
2020-05-31 21:28:29 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Checks if a view contains an entity
|
|
|
|
pub fn contains(self: Self, entity: EntityT) bool {
|
|
|
|
return self.set.contains(entity);
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn len(self: Self) usize {
|
|
|
|
return self.set.len();
|
|
|
|
}
|
|
|
|
|
|
|
|
pub usingnamespace if (is_empty_struct)
|
2020-06-11 22:28:29 -07:00
|
|
|
struct {
|
|
|
|
/// Sort Entities according to the given comparison function
|
|
|
|
pub fn sort(self: Self, comptime sortFn: fn (void, EntityT, EntityT) bool) void {
|
|
|
|
self.set.sort(sortFn);
|
|
|
|
}
|
|
|
|
}
|
2020-05-31 21:28:29 -07:00
|
|
|
else
|
|
|
|
struct {
|
|
|
|
/// Direct access to the array of objects
|
|
|
|
pub fn raw(self: Self) []CompT {
|
|
|
|
return self.instances.items;
|
|
|
|
}
|
|
|
|
|
2020-06-01 20:05:07 -07:00
|
|
|
/// Replaces the given component for an entity
|
|
|
|
pub fn replace(self: *Self, entity: EntityT, value: CompT) void {
|
|
|
|
self.get(entity).* = value;
|
|
|
|
self.update.publish(entity);
|
|
|
|
}
|
|
|
|
|
2020-05-31 21:28:29 -07:00
|
|
|
/// Returns the object associated with an entity
|
|
|
|
pub fn get(self: *Self, entity: EntityT) *CompT {
|
|
|
|
std.debug.assert(self.contains(entity));
|
|
|
|
return &self.instances.items[self.set.index(entity)];
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn getConst(self: *Self, entity: EntityT) CompT {
|
|
|
|
return self.instances.items[self.set.index(entity)];
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns a pointer to the object associated with an entity, if any.
|
|
|
|
pub fn tryGet(self: *Self, entity: EntityT) ?*CompT {
|
|
|
|
return if (self.set.contains(entity)) &self.instances.items[self.set.index(entity)] else null;
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn tryGetConst(self: *Self, entity: EntityT) ?CompT {
|
|
|
|
return if (self.set.contains(entity)) self.instances.items[self.set.index(entity)] else null;
|
|
|
|
}
|
2020-06-11 22:28:29 -07:00
|
|
|
|
|
|
|
/// Sort Entities or Components according to the given comparison function
|
|
|
|
pub fn sort(self: Self, comptime T: type, comptime sortFn: fn (void, T, T) bool) void {
|
|
|
|
std.debug.assert(T == EntityT or T == CompT);
|
|
|
|
if (T == EntityT) {
|
|
|
|
self.set.sortSub(sortFn, CompT, self.instances.items);
|
|
|
|
} else if (T == CompT) {
|
|
|
|
// essentially need to be able to call a sort method with a bound fn. That fn would then use sortFn along
|
|
|
|
// with self.instances.
|
|
|
|
// fn sorter(self: Self, a: T, b: T, sortFn) bool {
|
|
|
|
// return sortFn(self.instances[a], self.instances[b]);
|
|
|
|
// }
|
|
|
|
//return compare(std::as_const(instances[underlying_type::index(lhs)]), std::as_const(instances[underlying_type::index(rhs)]));
|
|
|
|
}
|
|
|
|
}
|
2020-05-31 21:28:29 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
/// Direct access to the array of entities
|
2020-06-09 10:21:42 -07:00
|
|
|
pub fn data(self: Self) []const EntityT {
|
2020-05-31 21:28:29 -07:00
|
|
|
return self.set.data();
|
|
|
|
}
|
|
|
|
|
2020-06-09 10:21:42 -07:00
|
|
|
/// Direct access to the array of entities
|
|
|
|
pub fn dataPtr(self: Self) *const []EntityT {
|
|
|
|
return self.set.dataPtr();
|
|
|
|
}
|
|
|
|
|
2020-05-31 21:28:29 -07:00
|
|
|
/// Swaps entities and objects in the internal packed arrays
|
|
|
|
pub fn swap(self: *Self, lhs: EntityT, rhs: EntityT) void {
|
2020-06-03 23:33:59 -07:00
|
|
|
self.safe_swap(self, lhs, rhs);
|
2020-05-31 21:28:29 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn clear(self: *Self) void {
|
2020-06-11 22:28:29 -07:00
|
|
|
if (!is_empty_struct) {
|
2020-05-31 21:28:29 -07:00
|
|
|
self.instances.items.len = 0;
|
2020-06-11 22:28:29 -07:00
|
|
|
}
|
2020-05-31 21:28:29 -07:00
|
|
|
self.set.clear();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
test "add/try-get/remove/clear" {
|
2020-06-04 19:19:29 -07:00
|
|
|
var store = ComponentStorage(f32, u32).init(std.testing.allocator);
|
2020-05-31 21:28:29 -07:00
|
|
|
defer store.deinit();
|
|
|
|
|
|
|
|
store.add(3, 66.45);
|
|
|
|
std.testing.expectEqual(store.tryGetConst(3).?, 66.45);
|
|
|
|
if (store.tryGet(3)) |found| std.testing.expectEqual(@as(f32, 66.45), found.*);
|
|
|
|
|
|
|
|
store.remove(3);
|
|
|
|
|
|
|
|
var val_null = store.tryGet(3);
|
|
|
|
std.testing.expectEqual(val_null, null);
|
|
|
|
|
|
|
|
store.clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
test "add/get/remove" {
|
2020-06-04 19:19:29 -07:00
|
|
|
var store = ComponentStorage(f32, u32).init(std.testing.allocator);
|
2020-05-31 21:28:29 -07:00
|
|
|
defer store.deinit();
|
|
|
|
|
|
|
|
store.add(3, 66.45);
|
|
|
|
if (store.tryGet(3)) |found| std.testing.expectEqual(@as(f32, 66.45), found.*);
|
|
|
|
std.testing.expectEqual(store.tryGetConst(3).?, 66.45);
|
|
|
|
|
|
|
|
store.remove(3);
|
|
|
|
std.testing.expectEqual(store.tryGet(3), null);
|
|
|
|
}
|
|
|
|
|
|
|
|
test "iterate" {
|
2020-06-04 19:19:29 -07:00
|
|
|
var store = ComponentStorage(f32, u32).initPtr(std.testing.allocator);
|
2020-05-31 21:28:29 -07:00
|
|
|
defer store.deinit();
|
|
|
|
|
|
|
|
store.add(3, 66.45);
|
|
|
|
store.add(5, 66.45);
|
|
|
|
store.add(7, 66.45);
|
|
|
|
|
2020-06-09 10:21:42 -07:00
|
|
|
for (store.data()) |entity, i| {
|
2020-06-11 22:28:29 -07:00
|
|
|
if (i == 0) {
|
2020-05-31 21:28:29 -07:00
|
|
|
std.testing.expectEqual(entity, 3);
|
2020-06-11 22:28:29 -07:00
|
|
|
}
|
|
|
|
if (i == 1) {
|
2020-05-31 21:28:29 -07:00
|
|
|
std.testing.expectEqual(entity, 5);
|
2020-06-11 22:28:29 -07:00
|
|
|
}
|
|
|
|
if (i == 2) {
|
2020-05-31 21:28:29 -07:00
|
|
|
std.testing.expectEqual(entity, 7);
|
2020-06-11 22:28:29 -07:00
|
|
|
}
|
2020-05-31 21:28:29 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
test "empty component" {
|
|
|
|
const Empty = struct {};
|
|
|
|
|
2020-06-04 19:19:29 -07:00
|
|
|
var store = ComponentStorage(Empty, u32).initPtr(std.testing.allocator);
|
2020-05-31 21:28:29 -07:00
|
|
|
defer store.deinit();
|
|
|
|
|
|
|
|
store.add(3, Empty{});
|
|
|
|
store.remove(3);
|
|
|
|
}
|
2020-06-01 20:05:07 -07:00
|
|
|
|
|
|
|
fn construct(e: u32) void {
|
|
|
|
std.debug.assert(e == 3);
|
|
|
|
}
|
|
|
|
fn update(e: u32) void {
|
|
|
|
std.debug.assert(e == 3);
|
|
|
|
}
|
|
|
|
fn destruct(e: u32) void {
|
|
|
|
std.debug.assert(e == 3);
|
|
|
|
}
|
|
|
|
|
|
|
|
test "signals" {
|
2020-06-04 19:19:29 -07:00
|
|
|
var store = ComponentStorage(f32, u32).init(std.testing.allocator);
|
2020-06-01 20:05:07 -07:00
|
|
|
defer store.deinit();
|
|
|
|
|
|
|
|
store.onConstruct().connect(construct);
|
|
|
|
store.onUpdate().connect(update);
|
|
|
|
store.onDestruct().connect(destruct);
|
|
|
|
|
|
|
|
store.add(3, 66.45);
|
|
|
|
store.replace(3, 45.64);
|
|
|
|
store.remove(3);
|
|
|
|
|
|
|
|
store.onConstruct().disconnect(construct);
|
|
|
|
store.onUpdate().disconnect(update);
|
|
|
|
store.onDestruct().disconnect(destruct);
|
|
|
|
|
|
|
|
store.add(4, 66.45);
|
|
|
|
store.replace(4, 45.64);
|
|
|
|
store.remove(4);
|
|
|
|
}
|
2020-06-11 22:28:29 -07:00
|
|
|
|
|
|
|
const asc_u32 = std.sort.asc(u32);
|
|
|
|
const desc_u32 = std.sort.desc(u32);
|
|
|
|
|
|
|
|
test "sort empty component" {
|
|
|
|
const Empty = struct {};
|
|
|
|
|
|
|
|
var store = ComponentStorage(Empty, u32).initPtr(std.testing.allocator);
|
|
|
|
defer store.deinit();
|
|
|
|
|
|
|
|
store.add(1, Empty{});
|
|
|
|
store.add(2, Empty{});
|
|
|
|
store.add(0, Empty{});
|
|
|
|
|
|
|
|
store.sort(asc_u32);
|
|
|
|
for (store.data()) |e, i| {
|
|
|
|
std.testing.expectEqual(@intCast(u32, i), e);
|
|
|
|
}
|
|
|
|
|
|
|
|
store.sort(desc_u32);
|
|
|
|
var counter: u32 = 2;
|
|
|
|
for (store.data()) |e, i| {
|
|
|
|
std.testing.expectEqual(counter, e);
|
|
|
|
if (counter > 0) counter -= 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const asc_f32 = std.sort.asc(f32);
|
|
|
|
const desc_f32 = std.sort.desc(f32);
|
|
|
|
|
|
|
|
test "sort component" {
|
|
|
|
std.debug.warn("\n", .{});
|
|
|
|
|
|
|
|
var store = ComponentStorage(f32, u32).initPtr(std.testing.allocator);
|
|
|
|
defer store.deinit();
|
|
|
|
|
|
|
|
store.add(1, @as(f32, 1.1));
|
|
|
|
store.add(2, @as(f32, 2.2));
|
|
|
|
store.add(0, @as(f32, 0.0));
|
|
|
|
|
|
|
|
store.sort(f32, asc_f32);
|
|
|
|
for (store.raw()) |e, i| {
|
|
|
|
// std.debug.warn("{}: {}\n", .{i, e});
|
|
|
|
// std.testing.expectEqual(@intCast(u32, i), e);
|
|
|
|
}
|
|
|
|
|
|
|
|
store.sort(f32, desc_f32);
|
|
|
|
for (store.raw()) |e, i| {
|
|
|
|
// std.testing.expectEqual(counter, e);
|
|
|
|
}
|
|
|
|
}
|