|
|
@ -15,8 +15,9 @@ const entity_traits = if (@hasDecl(root, "EntityTraits")) root.EntityTraits.init
|
|
|
|
const EntityHandles = Handles(entity_traits.entity_type, entity_traits.index_type, entity_traits.version_type);
|
|
|
|
const EntityHandles = Handles(entity_traits.entity_type, entity_traits.index_type, entity_traits.version_type);
|
|
|
|
pub const Entity = entity_traits.entity_type;
|
|
|
|
pub const Entity = entity_traits.entity_type;
|
|
|
|
|
|
|
|
|
|
|
|
pub const BasicView = @import("view.zig").BasicView;
|
|
|
|
pub const BasicView = @import("views.zig").BasicView;
|
|
|
|
pub const BasicMultiView = @import("view.zig").BasicMultiView;
|
|
|
|
pub const BasicMultiView = @import("views.zig").BasicMultiView;
|
|
|
|
|
|
|
|
pub const NonOwningGroup = @import("groups.zig").NonOwningGroup;
|
|
|
|
|
|
|
|
|
|
|
|
/// Stores an ArrayList of components. The max amount that can be stored is based on the type below
|
|
|
|
/// Stores an ArrayList of components. The max amount that can be stored is based on the type below
|
|
|
|
pub fn Storage(comptime CompT: type) type {
|
|
|
|
pub fn Storage(comptime CompT: type) type {
|
|
|
@ -31,14 +32,51 @@ pub const Registry = struct {
|
|
|
|
handles: EntityHandles,
|
|
|
|
handles: EntityHandles,
|
|
|
|
components: std.AutoHashMap(u8, usize),
|
|
|
|
components: std.AutoHashMap(u8, usize),
|
|
|
|
contexts: std.AutoHashMap(u8, usize),
|
|
|
|
contexts: std.AutoHashMap(u8, usize),
|
|
|
|
|
|
|
|
groups: std.ArrayList(GroupData),
|
|
|
|
allocator: *std.mem.Allocator,
|
|
|
|
allocator: *std.mem.Allocator,
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const GroupData = struct {
|
|
|
|
|
|
|
|
hash: u32,
|
|
|
|
|
|
|
|
entity_set: SparseSet(Entity, u16) = undefined,
|
|
|
|
|
|
|
|
owned: []u32,
|
|
|
|
|
|
|
|
include: []u32,
|
|
|
|
|
|
|
|
exclude: []u32,
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
pub fn init(allocator: *std.mem.Allocator, registry: *Registry, hash: u32, owned: []u32, include: []u32, exclude: []u32) GroupData {
|
|
|
|
|
|
|
|
std.debug.assert(std.mem.indexOfAny(u32, owned, include) == null);
|
|
|
|
|
|
|
|
std.debug.assert(std.mem.indexOfAny(u32, owned, exclude) == null);
|
|
|
|
|
|
|
|
std.debug.assert(std.mem.indexOfAny(u32, include, exclude) == null);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const group_data = GroupData{
|
|
|
|
|
|
|
|
.hash = hash,
|
|
|
|
|
|
|
|
.entity_set = SparseSet(Entity, u16).init(allocator),
|
|
|
|
|
|
|
|
.owned = std.mem.dupe(allocator, u32, owned) catch unreachable,
|
|
|
|
|
|
|
|
.include = std.mem.dupe(allocator, u32, include) catch unreachable,
|
|
|
|
|
|
|
|
.exclude = std.mem.dupe(allocator, u32, exclude) catch unreachable,
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return group_data;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
pub fn deinit(self: *GroupData, allocator: *std.mem.Allocator) void {
|
|
|
|
|
|
|
|
self.entity_set.deinit();
|
|
|
|
|
|
|
|
allocator.free(self.owned);
|
|
|
|
|
|
|
|
allocator.free(self.include);
|
|
|
|
|
|
|
|
allocator.free(self.exclude);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
pub fn hasSameConstraints(self: *GroupData, owned: []u32, include: []u32, exclude: []u32) bool {
|
|
|
|
|
|
|
|
return std.mem.eql(u32, self.owned, owned) and std.mem.eql(u32, self.include, include) and std.mem.eql(u32, self.exclude, exclude);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
pub fn init(allocator: *std.mem.Allocator) Registry {
|
|
|
|
pub fn init(allocator: *std.mem.Allocator) Registry {
|
|
|
|
return Registry{
|
|
|
|
return Registry{
|
|
|
|
.typemap = TypeMap.init(allocator),
|
|
|
|
.typemap = TypeMap.init(allocator),
|
|
|
|
.handles = EntityHandles.init(allocator),
|
|
|
|
.handles = EntityHandles.init(allocator),
|
|
|
|
.components = std.AutoHashMap(u8, usize).init(allocator),
|
|
|
|
.components = std.AutoHashMap(u8, usize).init(allocator),
|
|
|
|
.contexts = std.AutoHashMap(u8, usize).init(allocator),
|
|
|
|
.contexts = std.AutoHashMap(u8, usize).init(allocator),
|
|
|
|
|
|
|
|
.groups = std.ArrayList(GroupData).init(allocator),
|
|
|
|
.allocator = allocator,
|
|
|
|
.allocator = allocator,
|
|
|
|
};
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -51,8 +89,13 @@ pub const Registry = struct {
|
|
|
|
storage.deinit();
|
|
|
|
storage.deinit();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
for (self.groups.items) |*grp| {
|
|
|
|
|
|
|
|
grp.deinit(self.allocator);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
self.components.deinit();
|
|
|
|
self.components.deinit();
|
|
|
|
self.contexts.deinit();
|
|
|
|
self.contexts.deinit();
|
|
|
|
|
|
|
|
self.groups.deinit();
|
|
|
|
self.typemap.deinit();
|
|
|
|
self.typemap.deinit();
|
|
|
|
self.handles.deinit();
|
|
|
|
self.handles.deinit();
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -276,95 +319,73 @@ pub const Registry = struct {
|
|
|
|
excludes_arr[i] = @as(u32, self.typemap.get(t));
|
|
|
|
excludes_arr[i] = @as(u32, self.typemap.get(t));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return BasicMultiView(includes.len, excludes.len).init(includes_arr, excludes_arr, self);
|
|
|
|
return BasicMultiView(includes.len, excludes.len).init(self, includes_arr, excludes_arr);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/// returns the Type that a view will be based on the includes and excludes
|
|
|
|
fn ViewType(comptime includes: var, comptime excludes: var) type {
|
|
|
|
fn ViewType(comptime includes: var, comptime excludes: var) type {
|
|
|
|
if (includes.len == 1 and excludes.len == 0) return BasicView(includes[0]);
|
|
|
|
if (includes.len == 1 and excludes.len == 0) return BasicView(includes[0]);
|
|
|
|
return BasicMultiView(includes.len, excludes.len);
|
|
|
|
return BasicMultiView(includes.len, excludes.len);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const Position = struct { x: f32, y: f32 };
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
test "context get/set/unset" {
|
|
|
|
|
|
|
|
var reg = Registry.init(std.testing.allocator);
|
|
|
|
|
|
|
|
defer reg.deinit();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
var ctx = reg.getContext(Position);
|
|
|
|
pub fn group(self: *Registry, comptime owned: var, comptime includes: var, comptime excludes: var) GroupType(owned, includes, excludes) {
|
|
|
|
std.testing.expectEqual(ctx, null);
|
|
|
|
if (@typeInfo(@TypeOf(owned)) != .Struct)
|
|
|
|
|
|
|
|
@compileError("Expected tuple or struct argument, found " ++ @typeName(@TypeOf(owned)));
|
|
|
|
var pos = Position{ .x = 5, .y = 5 };
|
|
|
|
if (@typeInfo(@TypeOf(includes)) != .Struct)
|
|
|
|
reg.setContext(&pos);
|
|
|
|
@compileError("Expected tuple or struct argument, found " ++ @typeName(@TypeOf(includes)));
|
|
|
|
ctx = reg.getContext(Position);
|
|
|
|
if (@typeInfo(@TypeOf(excludes)) != .Struct)
|
|
|
|
std.testing.expectEqual(ctx.?, &pos);
|
|
|
|
@compileError("Expected tuple or struct argument, found " ++ @typeName(@TypeOf(excludes)));
|
|
|
|
|
|
|
|
std.debug.assert(includes.len + owned.len > 0);
|
|
|
|
reg.unsetContext(Position);
|
|
|
|
std.debug.assert(includes.len + owned.len + excludes.len >= 1);
|
|
|
|
ctx = reg.getContext(Position);
|
|
|
|
|
|
|
|
std.testing.expectEqual(ctx, null);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// this test should fail
|
|
|
|
var owned_arr: [owned.len]u32 = undefined;
|
|
|
|
test "context not pointer" {
|
|
|
|
inline for (owned) |t, i| {
|
|
|
|
var reg = Registry.init(std.testing.allocator);
|
|
|
|
_ = self.assure(t);
|
|
|
|
defer reg.deinit();
|
|
|
|
owned_arr[i] = @as(u32, self.typemap.get(t));
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
var pos = Position{ .x = 5, .y = 5 };
|
|
|
|
var includes_arr: [includes.len]u32 = undefined;
|
|
|
|
// reg.setContext(pos);
|
|
|
|
inline for (includes) |t, i| {
|
|
|
|
}
|
|
|
|
_ = self.assure(t);
|
|
|
|
|
|
|
|
includes_arr[i] = @as(u32, self.typemap.get(t));
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
test "component context get/set/unset" {
|
|
|
|
var excludes_arr: [excludes.len]u32 = undefined;
|
|
|
|
const SomeType = struct { dummy: u1 };
|
|
|
|
inline for (excludes) |t, i| {
|
|
|
|
|
|
|
|
_ = self.assure(t);
|
|
|
|
|
|
|
|
excludes_arr[i] = @as(u32, self.typemap.get(t));
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
var reg = Registry.init(std.testing.allocator);
|
|
|
|
// create a unique hash to identify the group
|
|
|
|
defer reg.deinit();
|
|
|
|
var group_data: ?*GroupData = null;
|
|
|
|
|
|
|
|
comptime const hash = owned.len + (31 * includes.len) + (31 * 31 * excludes.len);
|
|
|
|
|
|
|
|
|
|
|
|
var ctx = reg.getContext(SomeType);
|
|
|
|
for (self.groups.items) |*grp| {
|
|
|
|
std.testing.expectEqual(ctx, null);
|
|
|
|
if (grp.hash == hash and grp.hasSameConstraints(owned_arr[0..], includes_arr[0..], excludes_arr[0..])) {
|
|
|
|
|
|
|
|
group_data = grp;
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
var pos = SomeType{ .dummy = 0 };
|
|
|
|
|
|
|
|
reg.setContext(&pos);
|
|
|
|
|
|
|
|
ctx = reg.getContext(SomeType);
|
|
|
|
|
|
|
|
std.testing.expectEqual(ctx.?, &pos);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
reg.unsetContext(SomeType);
|
|
|
|
// non-owning groups
|
|
|
|
ctx = reg.getContext(SomeType);
|
|
|
|
if (owned.len == 0) {
|
|
|
|
std.testing.expectEqual(ctx, null);
|
|
|
|
if (group_data != null) {
|
|
|
|
}
|
|
|
|
return NonOwningGroup(includes.len, excludes.len).init(self, includes_arr, excludes_arr);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
test "destroy" {
|
|
|
|
var new_group_data = GroupData.init(self.allocator, self, hash, &[_]u32{}, includes_arr[0..], excludes_arr[0..]);
|
|
|
|
var reg = Registry.init(std.testing.allocator);
|
|
|
|
new_group_data.entity_set.reserve(5);
|
|
|
|
defer reg.deinit();
|
|
|
|
self.groups.append(new_group_data) catch unreachable;
|
|
|
|
|
|
|
|
return NonOwningGroup(includes.len, excludes.len).init(self, includes_arr, excludes_arr);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
var i = @as(u8, 0);
|
|
|
|
@compileLog("owned groups not implemented");
|
|
|
|
while (i < 255) : (i += 1) {
|
|
|
|
|
|
|
|
const e = reg.create();
|
|
|
|
|
|
|
|
reg.add(e, Position{ .x = @intToFloat(f32, i), .y = @intToFloat(f32, i) });
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
reg.destroy(3);
|
|
|
|
/// returns the Type that a view will be based on the includes and excludes
|
|
|
|
reg.destroy(4);
|
|
|
|
fn GroupType(comptime owned: var, comptime includes: var, comptime excludes: var) type {
|
|
|
|
|
|
|
|
if (owned.len == 0) return NonOwningGroup(includes.len, excludes.len);
|
|
|
|
i = 0;
|
|
|
|
unreachable;
|
|
|
|
while (i < 6) : (i += 1) {
|
|
|
|
|
|
|
|
if (i != 3 and i != 4)
|
|
|
|
|
|
|
|
std.testing.expectEqual(Position{ .x = @intToFloat(f32, i), .y = @intToFloat(f32, i)}, reg.getConst(Position, i));
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
test "remove all" {
|
|
|
|
|
|
|
|
var reg = Registry.init(std.testing.allocator);
|
|
|
|
|
|
|
|
defer reg.deinit();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
var e = reg.create();
|
|
|
|
|
|
|
|
reg.add(e, Position{.x = 1, .y = 1});
|
|
|
|
|
|
|
|
reg.addTyped(u32, e, 666);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
std.testing.expect(reg.has(Position, e));
|
|
|
|
|
|
|
|
std.testing.expect(reg.has(u32, e));
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
reg.removeAll(e);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
std.testing.expect(!reg.has(Position, e));
|
|
|
|
|
|
|
|
std.testing.expect(!reg.has(u32, e));
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|