move mach.ecs.Module -> mach.Module
Signed-off-by: Stephen Gutekanst <stephen@hexops.com>
This commit is contained in:
parent
279290bbf2
commit
56fc29743f
4 changed files with 13 additions and 12 deletions
|
|
@ -12,14 +12,13 @@
|
|||
//!
|
||||
|
||||
const std = @import("std");
|
||||
const mach = @import("../main.zig");
|
||||
const testing = std.testing;
|
||||
|
||||
pub const EntityID = @import("entities.zig").EntityID;
|
||||
pub const Entities = @import("entities.zig").Entities;
|
||||
pub const Archetype = @import("Archetype.zig");
|
||||
|
||||
pub const Module = @import("modules.zig").Module;
|
||||
pub const Modules = @import("modules.zig").Modules;
|
||||
pub const World = @import("systems.zig").World;
|
||||
|
||||
// TODO:
|
||||
|
|
@ -36,7 +35,6 @@ test "inclusion" {
|
|||
std.testing.refAllDeclsRecursive(@import("query.zig"));
|
||||
std.testing.refAllDeclsRecursive(@import("StringTable.zig"));
|
||||
std.testing.refAllDeclsRecursive(@import("systems.zig"));
|
||||
std.testing.refAllDeclsRecursive(@import("modules.zig"));
|
||||
}
|
||||
|
||||
test "example" {
|
||||
|
|
@ -44,7 +42,7 @@ test "example" {
|
|||
|
||||
comptime var Renderer = type;
|
||||
comptime var Physics = type;
|
||||
Physics = Module(struct {
|
||||
Physics = mach.Module(struct {
|
||||
pointer: u8,
|
||||
|
||||
pub const name = .physics;
|
||||
|
|
@ -57,7 +55,7 @@ test "example" {
|
|||
}
|
||||
});
|
||||
|
||||
Renderer = Module(struct {
|
||||
Renderer = mach.Module(struct {
|
||||
pub const name = .renderer;
|
||||
pub const components = struct {
|
||||
pub const id = u16;
|
||||
|
|
|
|||
|
|
@ -1,166 +0,0 @@
|
|||
const std = @import("std");
|
||||
const testing = std.testing;
|
||||
const StructField = std.builtin.Type.StructField;
|
||||
|
||||
const EntityID = @import("entities.zig").EntityID;
|
||||
|
||||
/// Verifies that T matches the expected layout of an ECS module
|
||||
pub fn Module(comptime T: type) type {
|
||||
if (@typeInfo(T) != .Struct) @compileError("Module must be a struct type. Found:" ++ @typeName(T));
|
||||
if (!@hasDecl(T, "name")) @compileError("Module must have `pub const name = .foobar;`");
|
||||
if (@typeInfo(@TypeOf(T.name)) != .EnumLiteral) @compileError("Module must have `pub const name = .foobar;`, found type:" ++ @typeName(T.name));
|
||||
if (@hasDecl(T, "components")) {
|
||||
if (@typeInfo(T.components) != .Struct) @compileError("Module.components must be `pub const components = struct { ... };`, found type:" ++ @typeName(T.components));
|
||||
}
|
||||
return T;
|
||||
}
|
||||
|
||||
fn NamespacedComponents(comptime modules: anytype) type {
|
||||
var fields: []const StructField = &[0]StructField{};
|
||||
inline for (modules) |M| {
|
||||
const components = if (@hasDecl(M, "components")) M.components else struct {};
|
||||
fields = fields ++ [_]std.builtin.Type.StructField{.{
|
||||
.name = @tagName(M.name),
|
||||
.type = type,
|
||||
.default_value = &components,
|
||||
.is_comptime = true,
|
||||
.alignment = @alignOf(@TypeOf(components)),
|
||||
}};
|
||||
}
|
||||
|
||||
// Builtin components
|
||||
const entity_components = struct {
|
||||
pub const id = EntityID;
|
||||
};
|
||||
fields = fields ++ [_]std.builtin.Type.StructField{.{
|
||||
.name = "entity",
|
||||
.type = type,
|
||||
.default_value = &entity_components,
|
||||
.is_comptime = true,
|
||||
.alignment = @alignOf(@TypeOf(entity_components)),
|
||||
}};
|
||||
|
||||
return @Type(.{
|
||||
.Struct = .{
|
||||
.layout = .Auto,
|
||||
.is_tuple = false,
|
||||
.fields = fields,
|
||||
.decls = &[_]std.builtin.Type.Declaration{},
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
fn NamespacedState(comptime modules: anytype) type {
|
||||
var fields: []const StructField = &[0]StructField{};
|
||||
inline for (modules) |M| {
|
||||
const state_fields = std.meta.fields(M);
|
||||
const State = if (state_fields.len > 0) @Type(.{
|
||||
.Struct = .{
|
||||
.layout = .Auto,
|
||||
.is_tuple = false,
|
||||
.fields = state_fields,
|
||||
.decls = &[_]std.builtin.Type.Declaration{},
|
||||
},
|
||||
}) else struct {};
|
||||
fields = fields ++ [_]std.builtin.Type.StructField{.{
|
||||
.name = @tagName(M.name),
|
||||
.type = State,
|
||||
.default_value = null,
|
||||
.is_comptime = false,
|
||||
.alignment = @alignOf(State),
|
||||
}};
|
||||
}
|
||||
return @Type(.{
|
||||
.Struct = .{
|
||||
.layout = .Auto,
|
||||
.is_tuple = false,
|
||||
.fields = fields,
|
||||
.decls = &[_]std.builtin.Type.Declaration{},
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
pub fn Modules(comptime mods: anytype) type {
|
||||
inline for (mods) |M| _ = Module(M);
|
||||
return struct {
|
||||
pub const modules = mods;
|
||||
|
||||
pub const components = NamespacedComponents(mods){};
|
||||
|
||||
pub const State = NamespacedState(mods);
|
||||
};
|
||||
}
|
||||
|
||||
test "module" {
|
||||
_ = Module(struct {
|
||||
// Physics module state
|
||||
pointer: usize,
|
||||
|
||||
// Globally unique module name
|
||||
pub const name = .engine_physics;
|
||||
|
||||
/// Physics module components
|
||||
pub const components = struct {
|
||||
/// A location component
|
||||
pub const location = @Vector(3, f32);
|
||||
};
|
||||
|
||||
pub fn tick(adapter: anytype) void {
|
||||
_ = adapter;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
test "modules" {
|
||||
const Physics = Module(struct {
|
||||
// Physics module state
|
||||
pointer: usize,
|
||||
|
||||
// Globally unique module name
|
||||
pub const name = .engine_physics;
|
||||
|
||||
/// Physics module components
|
||||
pub const components = struct {
|
||||
/// A location component
|
||||
pub const location = @Vector(3, f32);
|
||||
};
|
||||
|
||||
pub fn tick(adapter: anytype) void {
|
||||
_ = adapter;
|
||||
}
|
||||
});
|
||||
|
||||
const Renderer = Module(struct {
|
||||
pub const name = .engine_renderer;
|
||||
|
||||
/// Renderer module components
|
||||
pub const components = struct {};
|
||||
|
||||
pub fn tick(adapter: anytype) void {
|
||||
_ = adapter;
|
||||
}
|
||||
});
|
||||
|
||||
const Sprite2D = Module(struct {
|
||||
pub const name = .engine_sprite2d;
|
||||
});
|
||||
|
||||
const modules = Modules(.{
|
||||
Physics,
|
||||
Renderer,
|
||||
Sprite2D,
|
||||
});
|
||||
testing.refAllDeclsRecursive(modules);
|
||||
testing.refAllDeclsRecursive(Physics);
|
||||
testing.refAllDeclsRecursive(Renderer);
|
||||
testing.refAllDeclsRecursive(Sprite2D);
|
||||
|
||||
// access namespaced components
|
||||
try testing.expectEqual(Physics.components.location, modules.components.engine_physics.location);
|
||||
try testing.expectEqual(Renderer.components, modules.components.engine_renderer);
|
||||
|
||||
// implicitly generated
|
||||
_ = modules.components.entity.id;
|
||||
|
||||
Physics.tick(null);
|
||||
}
|
||||
|
|
@ -2,13 +2,13 @@ const std = @import("std");
|
|||
const mem = std.mem;
|
||||
const StructField = std.builtin.Type.StructField;
|
||||
|
||||
const mach = @import("../main.zig");
|
||||
const Entities = @import("entities.zig").Entities;
|
||||
const Modules = @import("modules.zig").Modules;
|
||||
const EntityID = @import("entities.zig").EntityID;
|
||||
const comp = @import("comptime.zig");
|
||||
|
||||
pub fn World(comptime mods: anytype) type {
|
||||
const modules = Modules(mods);
|
||||
const modules = mach.Modules(mods);
|
||||
|
||||
return struct {
|
||||
allocator: mem.Allocator,
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue