fixes and tests

master
Mike 5 years ago
parent 73080c2cfc
commit e76971101b

@ -231,7 +231,7 @@ pub const Registry = struct {
/// Returns the number of existing components of the given type
pub fn len(self: *Registry, comptime T: type) usize {
self.assure(T).len();
return self.assure(T).len();
}
/// Increases the capacity of the registry or of the pools for the given component

@ -43,12 +43,12 @@ pub fn BasicView(comptime T: type) type {
return self.storage.getConst(entity);
}
pub fn entityIterator(self: Self) utils.ReverseSliceIterator(Entity) {
return self.storage.set.reverseIterator();
pub fn iterator(self: Self) utils.ReverseSliceIterator(T) {
return utils.ReverseSliceIterator(T).init(self.storage.instances.items);
}
pub fn componentIterator(self: Self) utils.ReverseSliceIterator(T) {
return utils.ReverseSliceIterator(T).init(self.storage.instances.items);
pub fn entityIterator(self: Self) utils.ReverseSliceIterator(Entity) {
return self.storage.set.reverseIterator();
}
};
}
@ -165,7 +165,7 @@ test "single basic view" {
std.testing.expectEqual(view.len(), 2);
var i: usize = 0;
var iter = view.componentIterator();
var iter = view.iterator();
while (iter.next()) |comp| {
if (i == 0) std.testing.expectEqual(comp, 50);
if (i == 1) std.testing.expectEqual(comp, 30);

@ -145,6 +145,48 @@ test "sort OwningGroup by Component" {
}
}
test "sort OwningGroup by Component ensure unsorted non-matches" {
var reg = Registry.init(std.testing.allocator);
defer reg.deinit();
var group = reg.group(.{ Sprite, Renderable }, .{}, .{});
var i: usize = 0;
while (i < 5) : (i += 1) {
var e = reg.create();
reg.add(e, Sprite{ .x = @intToFloat(f32, i) });
reg.add(e, Renderable{ .x = @intToFloat(f32, i) });
var e2 = reg.create();
reg.add(e2, Sprite{ .x = @intToFloat(f32, i + 1 * 50) });
}
std.testing.expectEqual(group.len(), 5);
std.testing.expectEqual(reg.len(Sprite), 10);
const SortContext = struct {
fn sort(this: void, a: Sprite, b: Sprite) bool {
// sprites with x > 50 shouldnt match in the group
std.testing.expect(a.x < 50 and b.x < 50);
return a.x > b.x;
}
};
group.sort(Sprite, {}, SortContext.sort);
// all the
var view = reg.view(.{Sprite}, .{});
var count: usize = 0;
var iter = view.iterator();
while (iter.next()) |sprite| {
count += 1;
// all sprite.x > 50 should be at the end and we iterate backwards
if (count < 6) {
std.testing.expect(sprite.x >= 50);
}
}
}
test "nested OwningGroups add/remove components" {
var reg = Registry.init(std.testing.allocator);
defer reg.deinit();

Loading…
Cancel
Save