src/ecs: move mach-ecs@83a3ed801008a976dd79e10068157b02c3b76a36 package to here
Helps hexops/mach#1165 Signed-off-by: Stephen Gutekanst <stephen@hexops.com>
This commit is contained in:
parent
d4cd79440e
commit
5f70579360
8 changed files with 1891 additions and 0 deletions
64
src/ecs/comptime.zig
Normal file
64
src/ecs/comptime.zig
Normal file
|
|
@ -0,0 +1,64 @@
|
|||
const std = @import("std");
|
||||
const builtin = @import("builtin");
|
||||
|
||||
const Archetype = @import("Archetype.zig");
|
||||
const StringTable = @import("StringTable.zig");
|
||||
|
||||
pub const is_debug = builtin.mode == .Debug;
|
||||
|
||||
/// Returns a unique comptime usize integer representing the type T. Value will change across
|
||||
/// different compilations.
|
||||
pub fn typeId(comptime T: type) u32 {
|
||||
_ = T;
|
||||
return @truncate(@intFromPtr(&struct {
|
||||
var x: u8 = 0;
|
||||
}.x));
|
||||
}
|
||||
|
||||
/// Asserts that T matches the type of the column.
|
||||
pub inline fn debugAssertColumnType(storage: *Archetype, column: *Archetype.Column, comptime T: type) void {
|
||||
if (is_debug) {
|
||||
if (typeId(T) != column.type_id) std.debug.panic("unexpected type: {s} expected: {s}", .{
|
||||
@typeName(T),
|
||||
storage.component_names.string(column.name),
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
/// Asserts that a tuple `row` to be e.g. appended to an archetype has values that actually match
|
||||
/// all of the columns of the archetype table.
|
||||
pub inline fn debugAssertRowType(storage: *Archetype, row: anytype) void {
|
||||
if (is_debug) {
|
||||
inline for (std.meta.fields(@TypeOf(row)), 0..) |field, index| {
|
||||
debugAssertColumnType(storage, &storage.columns[index], field.type);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// TODO: comptime refactor
|
||||
pub fn ArchetypeSlicer(comptime all_components: anytype) type {
|
||||
return struct {
|
||||
archetype: *Archetype,
|
||||
|
||||
pub fn slice(
|
||||
slicer: @This(),
|
||||
comptime namespace_name: std.meta.FieldEnum(@TypeOf(all_components)),
|
||||
comptime component_name: std.meta.DeclEnum(@field(all_components, @tagName(namespace_name))),
|
||||
) []@field(
|
||||
@field(all_components, @tagName(namespace_name)),
|
||||
@tagName(component_name),
|
||||
) {
|
||||
const Type = @field(
|
||||
@field(all_components, @tagName(namespace_name)),
|
||||
@tagName(component_name),
|
||||
);
|
||||
if (namespace_name == .entity and component_name == .id) {
|
||||
const name_id = slicer.archetype.component_names.index("id").?;
|
||||
return slicer.archetype.getColumnValues(name_id, Type).?[0..slicer.archetype.len];
|
||||
}
|
||||
const name = @tagName(namespace_name) ++ "." ++ @tagName(component_name);
|
||||
const name_id = slicer.archetype.component_names.index(name).?;
|
||||
return slicer.archetype.getColumnValues(name_id, Type).?[0..slicer.archetype.len];
|
||||
}
|
||||
};
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue