module: rename all_components -> component_types_by_name

Signed-off-by: Stephen Gutekanst <stephen@hexops.com>
This commit is contained in:
Stephen Gutekanst 2024-05-04 12:45:29 -07:00
parent 7ccfcefa96
commit b37ece1b9a
4 changed files with 34 additions and 34 deletions

View file

@ -271,22 +271,22 @@ pub inline fn debugAssertRowType(storage: *Archetype, row: anytype) void {
} }
// TODO: comptime refactor // TODO: comptime refactor
pub fn Slicer(comptime all_components: anytype) type { pub fn Slicer(comptime component_types_by_name: anytype) type {
return struct { return struct {
archetype: *Archetype, archetype: *Archetype,
pub fn slice( pub fn slice(
slicer: @This(), slicer: @This(),
// TODO: cleanup comptime // TODO: cleanup comptime
comptime namespace_name: std.meta.FieldEnum(@TypeOf(all_components)), comptime namespace_name: std.meta.FieldEnum(@TypeOf(component_types_by_name)),
comptime component_name: std.meta.FieldEnum(@TypeOf(@field(all_components, @tagName(namespace_name)))), comptime component_name: std.meta.FieldEnum(@TypeOf(@field(component_types_by_name, @tagName(namespace_name)))),
) []@field( ) []@field(
@field(all_components, @tagName(namespace_name)), @field(component_types_by_name, @tagName(namespace_name)),
@tagName(component_name), @tagName(component_name),
).type { ).type {
// TODO: cleanup comptime // TODO: cleanup comptime
const Type = @field( const Type = @field(
@field(all_components, @tagName(namespace_name)), @field(component_types_by_name, @tagName(namespace_name)),
@tagName(component_name), @tagName(component_name),
).type; ).type;
if (namespace_name == .entity and component_name == .id) { if (namespace_name == .entity and component_name == .id) {

View file

@ -67,8 +67,8 @@ fn byTypeId(context: void, lhs: Archetype.Column, rhs: Archetype.Column) bool {
/// row index, enabling entities to "move" from one archetype table to another seamlessly and /// row index, enabling entities to "move" from one archetype table to another seamlessly and
/// making lookup by entity ID a few cheap array indexing operations. /// making lookup by entity ID a few cheap array indexing operations.
/// * ComponentStorage(T) is a column of data within a table for a single type of component `T`. /// * ComponentStorage(T) is a column of data within a table for a single type of component `T`.
pub fn Entities(comptime all_components: anytype) type { pub fn Entities(comptime component_types_by_name: anytype) type {
// TODO: validate all_components is a namespaced component set in the form we expect // TODO: validate component_types_by_name is a namespaced component set in the form we expect
return struct { return struct {
allocator: Allocator, allocator: Allocator,
@ -105,7 +105,7 @@ pub fn Entities(comptime all_components: anytype) type {
}; };
/// A complex query for entities matching a given criteria /// A complex query for entities matching a given criteria
pub const Query = query_mod.Query(all_components); pub const Query = query_mod.Query(component_types_by_name);
pub const QueryTag = query_mod.QueryTag; pub const QueryTag = query_mod.QueryTag;
pub fn init(allocator: Allocator) !Self { pub fn init(allocator: Allocator) !Self {
@ -282,10 +282,10 @@ pub fn Entities(comptime all_components: anytype) type {
entities: *Self, entities: *Self,
entity: EntityID, entity: EntityID,
// TODO: cleanup comptime // TODO: cleanup comptime
comptime namespace_name: std.meta.FieldEnum(@TypeOf(all_components)), comptime namespace_name: std.meta.FieldEnum(@TypeOf(component_types_by_name)),
comptime component_name: std.meta.FieldEnum(@TypeOf(@field(all_components, @tagName(namespace_name)))), comptime component_name: std.meta.FieldEnum(@TypeOf(@field(component_types_by_name, @tagName(namespace_name)))),
component: @field( component: @field(
@field(all_components, @tagName(namespace_name)), @field(component_types_by_name, @tagName(namespace_name)),
@tagName(component_name), @tagName(component_name),
).type, ).type,
) !void { ) !void {
@ -485,15 +485,15 @@ pub fn Entities(comptime all_components: anytype) type {
entities: *Self, entities: *Self,
entity: EntityID, entity: EntityID,
// TODO: cleanup comptime // TODO: cleanup comptime
comptime namespace_name: std.meta.FieldEnum(@TypeOf(all_components)), comptime namespace_name: std.meta.FieldEnum(@TypeOf(component_types_by_name)),
comptime component_name: std.meta.FieldEnum(@TypeOf(@field(all_components, @tagName(namespace_name)))), comptime component_name: std.meta.FieldEnum(@TypeOf(@field(component_types_by_name, @tagName(namespace_name)))),
) ?@field( ) ?@field(
@field(all_components, @tagName(namespace_name)), @field(component_types_by_name, @tagName(namespace_name)),
@tagName(component_name), @tagName(component_name),
).type { ).type {
// TODO: cleanup comptime // TODO: cleanup comptime
const Component = comptime @field( const Component = comptime @field(
@field(all_components, @tagName(namespace_name)), @field(component_types_by_name, @tagName(namespace_name)),
@tagName(component_name), @tagName(component_name),
).type; ).type;
@ -528,8 +528,8 @@ pub fn Entities(comptime all_components: anytype) type {
entities: *Self, entities: *Self,
entity: EntityID, entity: EntityID,
// TODO: cleanup comptime // TODO: cleanup comptime
comptime namespace_name: std.meta.FieldEnum(@TypeOf(all_components)), comptime namespace_name: std.meta.FieldEnum(@TypeOf(component_types_by_name)),
comptime component_name: std.meta.FieldEnum(@TypeOf(@field(all_components, @tagName(namespace_name)))), comptime component_name: std.meta.FieldEnum(@TypeOf(@field(component_types_by_name, @tagName(namespace_name)))),
) !void { ) !void {
const name_str = @tagName(namespace_name) ++ "." ++ @tagName(component_name); const name_str = @tagName(namespace_name) ++ "." ++ @tagName(component_name);
const name_id = try entities.component_names.indexOrPut(entities.allocator, name_str); const name_id = try entities.component_names.indexOrPut(entities.allocator, name_str);
@ -613,8 +613,8 @@ pub fn Entities(comptime all_components: anytype) type {
pub fn query( pub fn query(
entities: *Self, entities: *Self,
q: Query, q: Query,
) ArchetypeIterator(all_components) { ) ArchetypeIterator(component_types_by_name) {
return ArchetypeIterator(all_components).init(entities, q); return ArchetypeIterator(component_types_by_name).init(entities, q);
} }
// TODO: queryDynamic // TODO: queryDynamic
@ -637,8 +637,8 @@ pub fn Entities(comptime all_components: anytype) type {
} }
// TODO: move this type somewhere else // TODO: move this type somewhere else
pub fn ArchetypeIterator(comptime all_components: anytype) type { pub fn ArchetypeIterator(comptime component_types_by_name: anytype) type {
const EntitiesT = Entities(all_components); const EntitiesT = Entities(component_types_by_name);
return struct { return struct {
entities: *EntitiesT, entities: *EntitiesT,
query: EntitiesT.Query, query: EntitiesT.Query,
@ -654,12 +654,12 @@ pub fn ArchetypeIterator(comptime all_components: anytype) type {
}; };
} }
// TODO: all_components is a superset of queried items, not type-safe. // TODO: component_types_by_name is a superset of queried items, not type-safe.
pub fn next(iter: *Self) ?Archetype.Slicer(all_components) { pub fn next(iter: *Self) ?Archetype.Slicer(component_types_by_name) {
while (iter.index < iter.entities.archetypes.items.len) { while (iter.index < iter.entities.archetypes.items.len) {
const archetype = &iter.entities.archetypes.items[iter.index]; const archetype = &iter.entities.archetypes.items[iter.index];
iter.index += 1; iter.index += 1;
if (iter.match(archetype)) return Archetype.Slicer(all_components){ .archetype = archetype }; if (iter.match(archetype)) return Archetype.Slicer(component_types_by_name){ .archetype = archetype };
} }
return null; return null;
} }
@ -749,7 +749,7 @@ test "example" {
const Rotation = struct { degrees: f32 }; const Rotation = struct { degrees: f32 };
const all_components = ComponentTypesByName(.{ const component_types_by_name = ComponentTypesByName(.{
struct { struct {
pub const name = .game; pub const name = .game;
pub const components = .{ pub const components = .{
@ -762,7 +762,7 @@ test "example" {
//------------------------------------------------------------------------- //-------------------------------------------------------------------------
// Create a world. // Create a world.
var world = try Entities(all_components).init(allocator); var world = try Entities(component_types_by_name).init(allocator);
defer world.deinit(); defer world.deinit();
//------------------------------------------------------------------------- //-------------------------------------------------------------------------
@ -853,7 +853,7 @@ test "many entities" {
const Rotation = struct { degrees: f32 }; const Rotation = struct { degrees: f32 };
const all_components = ComponentTypesByName(.{ const component_types_by_name = ComponentTypesByName(.{
struct { struct {
pub const name = .game; pub const name = .game;
pub const components = .{ pub const components = .{
@ -865,7 +865,7 @@ test "many entities" {
}){}; }){};
// Create many entities // Create many entities
var world = try Entities(all_components).init(allocator); var world = try Entities(component_types_by_name).init(allocator);
defer world.deinit(); defer world.deinit();
for (0..8192) |_| { for (0..8192) |_| {
const player = try world.new(); const player = try world.new();

View file

@ -99,7 +99,7 @@ test "entities DB" {
try testing.expectEqual(@as(usize, 1001), ids[0]); try testing.expectEqual(@as(usize, 1001), ids[0]);
// TODO: can't write @as type here easily due to generic parameter, should be exposed // TODO: can't write @as type here easily due to generic parameter, should be exposed
// ?Archetype.Slicer(all_components) // ?Archetype.Slicer(component_types_by_name)
try testing.expectEqual(iter.next(), null); try testing.expectEqual(iter.next(), null);
//------------------------------------------------------------------------- //-------------------------------------------------------------------------

View file

@ -8,17 +8,17 @@ pub const QueryTag = enum {
}; };
/// A complex query for entities matching a given criteria /// A complex query for entities matching a given criteria
pub fn Query(comptime all_components: anytype) type { pub fn Query(comptime component_types_by_name: anytype) type {
return union(QueryTag) { return union(QueryTag) {
// TODO: cleanup comptime // TODO: cleanup comptime
/// Enum matching a namespace. e.g. `.game` or `.physics2d` /// Enum matching a namespace. e.g. `.game` or `.physics2d`
pub const Namespace = std.meta.FieldEnum(@TypeOf(all_components)); pub const Namespace = std.meta.FieldEnum(@TypeOf(component_types_by_name));
// TODO: cleanup comptime // TODO: cleanup comptime
/// Enum matching a component within a namespace /// Enum matching a component within a namespace
/// e.g. `var a: Component(.physics2d) = .location` /// e.g. `var a: Component(.physics2d) = .location`
pub fn Component(comptime namespace: Namespace) type { pub fn Component(comptime namespace: Namespace) type {
const components = @field(all_components, @tagName(namespace)); const components = @field(component_types_by_name, @tagName(namespace));
if (@typeInfo(@TypeOf(components)).Struct.fields.len == 0) return enum {}; if (@typeInfo(@TypeOf(components)).Struct.fields.len == 0) return enum {};
return std.meta.FieldEnum(@TypeOf(components)); return std.meta.FieldEnum(@TypeOf(components));
} }
@ -70,7 +70,7 @@ test "query" {
const Rotation = struct { degrees: f32 }; const Rotation = struct { degrees: f32 };
const all_components = ComponentTypesByName(.{ const component_types_by_name = ComponentTypesByName(.{
struct { struct {
pub const name = .game; pub const name = .game;
pub const components = .{ pub const components = .{
@ -89,7 +89,7 @@ test "query" {
}, },
}){}; }){};
const Q = Query(all_components); const Q = Query(component_types_by_name);
// Namespace type lets us select a single namespace. // Namespace type lets us select a single namespace.
try testing.expectEqual(@as(Q.Namespace, .game), .game); try testing.expectEqual(@as(Q.Namespace, .game), .game);