ecs: generic iterator type

This change makes the the Iterator generic over the components being
queried. This restores the api to the previous style with components
given to the query() function and next() having no parameters (other
that the iterator itself).
This commit is contained in:
dweiller 2022-07-13 18:45:23 +10:00 committed by Stephen Gutekanst
parent b9fc04de6a
commit ebc09ee55e

View file

@ -401,11 +401,14 @@ pub fn Entities(all_components: anytype) type {
return @tagName(q) ++ "." ++ @tagName(@field(q, @tagName(std.meta.activeTag(q)))); return @tagName(q) ++ "." ++ @tagName(@field(q, @tagName(std.meta.activeTag(q))));
} }
pub const Iterator = struct { pub fn Iter(comptime components: []const Query) type {
return struct {
entities: *Self, entities: *Self,
archetype_index: usize = 0, archetype_index: usize = 0,
row_index: u32 = 0, row_index: u32 = 0,
const Iterator = @This();
pub const Entry = struct { pub const Entry = struct {
entity: EntityID, entity: EntityID,
@ -414,7 +417,7 @@ pub fn Entities(all_components: anytype) type {
} }
}; };
pub fn next(iter: *Iterator, comptime components: []const Query) ?Entry { pub fn next(iter: *Iterator) ?Entry {
const entities = iter.entities; const entities = iter.entities;
// If the archetype table we're looking at does not contain the components we're // If the archetype table we're looking at does not contain the components we're
@ -434,6 +437,7 @@ pub fn Entities(all_components: anytype) type {
return Entry{ .entity = row_entity_id }; return Entry{ .entity = row_entity_id };
} }
}; };
}
fn hasComponents(storage: ArchetypeStorage, comptime components: []const Query) bool { fn hasComponents(storage: ArchetypeStorage, comptime components: []const Query) bool {
var archetype = storage; var archetype = storage;
@ -444,8 +448,8 @@ pub fn Entities(all_components: anytype) type {
return true; return true;
} }
pub fn query(entities: *Self) Iterator { pub fn query(entities: *Self, comptime components: []const Query) Iter(components) {
return Iterator{ return Iter(components){
.entities = entities, .entities = entities,
}; };
} }