all: get ECS running on revised module system
All ECS `examples/` now run on the revised module system. Signed-off-by: Stephen Gutekanst <stephen@hexops.com>
This commit is contained in:
parent
cd2f3fbc3f
commit
3898995c4c
6 changed files with 249 additions and 219 deletions
|
|
@ -50,7 +50,7 @@ test "example" {
|
|||
pub const id = u32;
|
||||
};
|
||||
|
||||
pub fn tick(physics: *World(.{ Renderer, Physics }).Mod(Physics)) !void {
|
||||
pub fn tick(physics: *World(.{ Renderer, Physics }).Mod(Physics)) void {
|
||||
_ = physics;
|
||||
}
|
||||
});
|
||||
|
|
@ -64,7 +64,7 @@ test "example" {
|
|||
pub fn tick(
|
||||
physics: *World(.{ Renderer, Physics }).Mod(Physics),
|
||||
renderer: *World(.{ Renderer, Physics }).Mod(Renderer),
|
||||
) !void {
|
||||
) void {
|
||||
_ = renderer;
|
||||
_ = physics;
|
||||
}
|
||||
|
|
@ -72,7 +72,8 @@ test "example" {
|
|||
|
||||
//-------------------------------------------------------------------------
|
||||
// Create a world.
|
||||
var world = try World(.{ Renderer, Physics }).init(allocator);
|
||||
var world: World(.{ Renderer, Physics }) = undefined;
|
||||
try world.init(allocator);
|
||||
defer world.deinit();
|
||||
|
||||
// Initialize module state.
|
||||
|
|
@ -113,5 +114,6 @@ test "example" {
|
|||
|
||||
//-------------------------------------------------------------------------
|
||||
// Send events to modules
|
||||
try world.send(null, .tick, .{});
|
||||
world.modules.send(.tick, .{});
|
||||
try world.dispatch();
|
||||
}
|
||||
|
|
|
|||
|
|
@ -8,25 +8,30 @@ const EntityID = @import("entities.zig").EntityID;
|
|||
const comp = @import("comptime.zig");
|
||||
|
||||
pub fn World(comptime mods: anytype) type {
|
||||
const Injectable = struct {}; // TODO
|
||||
const modules = mach.Modules(mods, Injectable);
|
||||
|
||||
const StateT = NamespacedState(mods);
|
||||
const ns_components = NamespacedComponents(mods){};
|
||||
return struct {
|
||||
allocator: mem.Allocator,
|
||||
entities: Entities(NamespacedComponents(mods){}),
|
||||
mod: Mods(),
|
||||
modules: Modules,
|
||||
mod: Mods,
|
||||
|
||||
const Self = @This();
|
||||
const Modules = mach.Modules(mods);
|
||||
|
||||
pub const IsInjectedArgument = void;
|
||||
|
||||
const WorldT = @This();
|
||||
pub fn Mod(comptime Module: anytype) type {
|
||||
const module_tag = Module.name;
|
||||
const State = @TypeOf(@field(@as(NamespacedState(mods), undefined), @tagName(module_tag)));
|
||||
const components = @field(NamespacedComponents(mods){}, @tagName(module_tag));
|
||||
const State = @TypeOf(@field(@as(StateT, undefined), @tagName(module_tag)));
|
||||
const components = @field(ns_components, @tagName(module_tag));
|
||||
return struct {
|
||||
state: State,
|
||||
entities: *Entities(NamespacedComponents(mods){}),
|
||||
entities: *Entities(ns_components),
|
||||
allocator: mem.Allocator,
|
||||
|
||||
pub const IsInjectedArgument = void;
|
||||
|
||||
/// Sets the named component to the specified value for the given entity,
|
||||
/// moving the entity from it's current archetype table to the new archetype
|
||||
/// table if required.
|
||||
|
|
@ -36,8 +41,8 @@ pub fn World(comptime mods: anytype) type {
|
|||
comptime component_name: std.meta.DeclEnum(components),
|
||||
component: @field(components, @tagName(component_name)),
|
||||
) !void {
|
||||
const mod_ptr: *Self.Mods() = @alignCast(@fieldParentPtr(Mods(), @tagName(module_tag), m));
|
||||
const world = @fieldParentPtr(Self, "mod", mod_ptr);
|
||||
const mod_ptr: *Mods = @alignCast(@fieldParentPtr(Mods, @tagName(module_tag), m));
|
||||
const world = @fieldParentPtr(WorldT, "mod", mod_ptr);
|
||||
try world.entities.setComponent(entity, module_tag, component_name, component);
|
||||
}
|
||||
|
||||
|
|
@ -48,8 +53,8 @@ pub fn World(comptime mods: anytype) type {
|
|||
entity: EntityID,
|
||||
comptime component_name: std.meta.DeclEnum(components),
|
||||
) ?@field(components, @tagName(component_name)) {
|
||||
const mod_ptr: *Self.Mods() = @alignCast(@fieldParentPtr(Mods(), @tagName(module_tag), m));
|
||||
const world = @fieldParentPtr(Self, "mod", mod_ptr);
|
||||
const mod_ptr: *Mods = @alignCast(@fieldParentPtr(Mods, @tagName(module_tag), m));
|
||||
const world = @fieldParentPtr(WorldT, "mod", mod_ptr);
|
||||
return world.entities.getComponent(entity, module_tag, component_name);
|
||||
}
|
||||
|
||||
|
|
@ -59,36 +64,36 @@ pub fn World(comptime mods: anytype) type {
|
|||
entity: EntityID,
|
||||
comptime component_name: std.meta.DeclEnum(components),
|
||||
) !void {
|
||||
const mod_ptr: *Self.Mods() = @alignCast(@fieldParentPtr(Mods(), @tagName(module_tag), m));
|
||||
const world = @fieldParentPtr(Self, "mod", mod_ptr);
|
||||
const mod_ptr: *Mods = @alignCast(@fieldParentPtr(Mods, @tagName(module_tag), m));
|
||||
const world = @fieldParentPtr(WorldT, "mod", mod_ptr);
|
||||
try world.entities.removeComponent(entity, module_tag, component_name);
|
||||
}
|
||||
|
||||
pub fn send(m: *@This(), comptime msg_tag: anytype, args: anytype) !void {
|
||||
const mod_ptr: *Self.Mods() = @alignCast(@fieldParentPtr(Mods(), @tagName(module_tag), m));
|
||||
const world = @fieldParentPtr(Self, "mod", mod_ptr);
|
||||
return world.sendStr(module_tag, @tagName(msg_tag), args);
|
||||
pub inline fn send(m: *@This(), comptime event_name: anytype, args: anytype) void {
|
||||
const mod_ptr: *Mods = @alignCast(@fieldParentPtr(Mods, @tagName(module_tag), m));
|
||||
const world = @fieldParentPtr(WorldT, "mod", mod_ptr);
|
||||
world.modules.sendToModule(module_tag, event_name, args);
|
||||
}
|
||||
|
||||
/// Returns a new entity.
|
||||
pub fn newEntity(m: *@This()) !EntityID {
|
||||
const mod_ptr: *Self.Mods() = @alignCast(@fieldParentPtr(Mods(), @tagName(module_tag), m));
|
||||
const world = @fieldParentPtr(Self, "mod", mod_ptr);
|
||||
const mod_ptr: *Mods = @alignCast(@fieldParentPtr(Mods, @tagName(module_tag), m));
|
||||
const world = @fieldParentPtr(WorldT, "mod", mod_ptr);
|
||||
return world.entities.new();
|
||||
}
|
||||
|
||||
/// Removes an entity.
|
||||
pub fn removeEntity(m: *@This(), entity: EntityID) !void {
|
||||
const mod_ptr: *Self.Mods() = @alignCast(@fieldParentPtr(Mods(), @tagName(module_tag), m));
|
||||
const world = @fieldParentPtr(Self, "mod", mod_ptr);
|
||||
const mod_ptr: *Mods = @alignCast(@fieldParentPtr(Mods, @tagName(module_tag), m));
|
||||
const world = @fieldParentPtr(WorldT, "mod", mod_ptr);
|
||||
try world.entities.removeEntity(entity);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
fn Mods() type {
|
||||
pub const Mods = blk: {
|
||||
var fields: []const StructField = &[0]StructField{};
|
||||
inline for (modules.modules) |M| {
|
||||
for (mods) |M| {
|
||||
fields = fields ++ [_]std.builtin.Type.StructField{.{
|
||||
.name = @tagName(M.name),
|
||||
.type = Mod(M),
|
||||
|
|
@ -97,7 +102,7 @@ pub fn World(comptime mods: anytype) type {
|
|||
.alignment = @alignOf(Mod(M)),
|
||||
}};
|
||||
}
|
||||
return @Type(.{
|
||||
break :blk @Type(.{
|
||||
.Struct = .{
|
||||
.layout = .Auto,
|
||||
.is_tuple = false,
|
||||
|
|
@ -105,97 +110,64 @@ pub fn World(comptime mods: anytype) type {
|
|||
.decls = &[_]std.builtin.Type.Declaration{},
|
||||
},
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
pub fn init(allocator: mem.Allocator) !Self {
|
||||
return Self{
|
||||
.allocator = allocator,
|
||||
.entities = try Entities(NamespacedComponents(mods){}).init(allocator),
|
||||
.mod = undefined,
|
||||
};
|
||||
}
|
||||
const Injectable = blk: {
|
||||
var types: []const type = &[0]type{};
|
||||
types = types ++ [_]type{*@This()};
|
||||
for (@typeInfo(Mods).Struct.fields) |field| {
|
||||
const ModPtr = @TypeOf(@as(*field.type, undefined));
|
||||
types = types ++ [_]type{ModPtr};
|
||||
}
|
||||
break :blk std.meta.Tuple(types);
|
||||
};
|
||||
fn injectable(world: *@This()) Injectable {
|
||||
var v: Injectable = undefined;
|
||||
outer: inline for (@typeInfo(Injectable).Struct.fields) |field| {
|
||||
if (field.type == *@This()) {
|
||||
@field(v, field.name) = world;
|
||||
continue :outer;
|
||||
} else {
|
||||
inline for (@typeInfo(Mods).Struct.fields) |injectable_field| {
|
||||
if (*injectable_field.type == field.type) {
|
||||
@field(v, field.name) = &@field(world.mod, injectable_field.name);
|
||||
|
||||
pub fn deinit(world: *Self) void {
|
||||
world.entities.deinit();
|
||||
}
|
||||
|
||||
/// Broadcasts an event to all modules that are subscribed to it.
|
||||
///
|
||||
/// The message tag corresponds with the handler method name to be invoked. For example,
|
||||
/// if `send(.tick)` is invoked, all modules which declare a `pub fn tick` will be invoked.
|
||||
///
|
||||
/// Events sent by Mach itself, or the application itself, may be single words. To prevent
|
||||
/// name conflicts, events sent by modules provided by a library should prefix their events
|
||||
/// with their module name. For example, a module named `.ziglibs_imgui` should use event
|
||||
/// names like `.ziglibsImguiClick`, `.ziglibsImguiFoobar`.
|
||||
pub fn send(world: *Self, comptime optional_module_tag: anytype, comptime msg_tag: anytype, args: anytype) !void {
|
||||
return world.sendStr(optional_module_tag, @tagName(msg_tag), args);
|
||||
}
|
||||
|
||||
pub fn sendStr(world: *Self, comptime optional_module_tag: anytype, comptime msg: anytype, args: anytype) !void {
|
||||
// Check for any module that has a handler function named msg (e.g. `fn init` would match "init")
|
||||
inline for (modules.modules) |M| {
|
||||
const EventHandlers = blk: {
|
||||
switch (@typeInfo(@TypeOf(optional_module_tag))) {
|
||||
.Null => break :blk M,
|
||||
.EnumLiteral => {
|
||||
// Send this message only to the specified module
|
||||
if (M.name != optional_module_tag) continue;
|
||||
if (!@hasDecl(M, "local")) @compileError("Module ." ++ @tagName(M.name) ++ " does not have a `pub const local` event handler for message ." ++ msg);
|
||||
if (!@hasDecl(M.local, msg)) @compileError("Module ." ++ @tagName(M.name) ++ " does not have a `pub const local` event handler for message ." ++ msg);
|
||||
break :blk M.local;
|
||||
},
|
||||
.Optional => if (optional_module_tag) |v| {
|
||||
// Send this message only to the specified module
|
||||
if (M.name != v) continue;
|
||||
if (!@hasDecl(M, "local")) @compileError("Module ." ++ @tagName(M.name) ++ " does not have a `pub const local` event handler for message ." ++ msg);
|
||||
if (!@hasDecl(M.local, msg)) @compileError("Module ." ++ @tagName(M.name) ++ " does not have a `pub const local` event handler for message ." ++ msg);
|
||||
break :blk M.local;
|
||||
},
|
||||
else => @panic("unexpected optional_module_tag type: " ++ @typeName(@TypeOf(optional_module_tag))),
|
||||
}
|
||||
};
|
||||
if (!@hasDecl(EventHandlers, msg)) continue;
|
||||
|
||||
// Determine which parameters the handler function wants. e.g.:
|
||||
//
|
||||
// pub fn init(eng: *mach.Engine) !void
|
||||
// pub fn init(eng: *mach.Engine, mach: *mach.Engine.Mod) !void
|
||||
//
|
||||
const handler = @field(EventHandlers, msg);
|
||||
|
||||
// Build a tuple of parameters that we can pass to the function, based on what
|
||||
// *mach.Mod(T) types it expects as arguments.
|
||||
var params: std.meta.ArgsTuple(@TypeOf(handler)) = undefined;
|
||||
comptime var argIndex = 0;
|
||||
inline for (@typeInfo(@TypeOf(params)).Struct.fields) |param| {
|
||||
comptime var found = false;
|
||||
inline for (@typeInfo(Mods()).Struct.fields) |f| {
|
||||
if (param.type == *f.type) {
|
||||
// TODO: better initialization place for modules
|
||||
@field(@field(world.mod, f.name), "entities") = &world.entities;
|
||||
@field(@field(world.mod, f.name), "allocator") = world.allocator;
|
||||
|
||||
@field(params, param.name) = &@field(world.mod, f.name);
|
||||
found = true;
|
||||
break;
|
||||
} else if (param.type == *Self) {
|
||||
@field(params, param.name) = world;
|
||||
found = true;
|
||||
break;
|
||||
} else if (param.type == f.type) {
|
||||
@compileError("Module handler " ++ @tagName(M.name) ++ "." ++ msg ++ " should be *T not T: " ++ @typeName(param.type));
|
||||
// TODO: better module initialization location
|
||||
@field(v, field.name).entities = &world.entities;
|
||||
@field(v, field.name).allocator = world.allocator;
|
||||
continue :outer;
|
||||
}
|
||||
}
|
||||
if (!found) {
|
||||
@field(params, param.name) = args[argIndex];
|
||||
argIndex += 1;
|
||||
}
|
||||
}
|
||||
|
||||
// Invoke the handler
|
||||
try @call(.auto, handler, params);
|
||||
@compileError("failed to initialize Injectable field (this is a bug): " ++ field.name ++ " " ++ @typeName(field.type));
|
||||
}
|
||||
return v;
|
||||
}
|
||||
|
||||
pub fn dispatch(world: *@This()) !void {
|
||||
try world.modules.dispatch(world.injectable());
|
||||
}
|
||||
|
||||
pub fn dispatchNoError(world: *@This()) void {
|
||||
world.modules.dispatch(world.injectable()) catch |err| @panic(@errorName(err));
|
||||
}
|
||||
|
||||
pub fn init(world: *@This(), allocator: mem.Allocator) !void {
|
||||
// TODO: switch Entities to stack allocation like Modules and World
|
||||
var entities = try Entities(ns_components).init(allocator);
|
||||
errdefer entities.deinit();
|
||||
world.* = @This(){
|
||||
.allocator = allocator,
|
||||
.entities = entities,
|
||||
.modules = undefined,
|
||||
.mod = undefined,
|
||||
};
|
||||
try world.modules.init(allocator);
|
||||
}
|
||||
|
||||
pub fn deinit(world: *@This()) void {
|
||||
world.entities.deinit();
|
||||
world.modules.deinit(world.allocator);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue