ecs: pass an all_components parameter to everything
Signed-off-by: Stephen Gutekanst <stephen@hexops.com>
This commit is contained in:
parent
858c14bbae
commit
1f7ea529f4
3 changed files with 383 additions and 367 deletions
|
|
@ -4,52 +4,59 @@ const Allocator = mem.Allocator;
|
|||
const testing = std.testing;
|
||||
|
||||
const Entities = @import("entities.zig").Entities;
|
||||
const Iterator = Entities.Iterator;
|
||||
|
||||
pub const Adapter = struct {
|
||||
world: *World,
|
||||
pub fn Adapter(all_components: anytype) type {
|
||||
return struct {
|
||||
world: *World(all_components),
|
||||
|
||||
pub fn query(adapter: *Adapter, components: []const []const u8) Iterator {
|
||||
return adapter.world.entities.query(components);
|
||||
}
|
||||
};
|
||||
const Self = @This();
|
||||
pub const Iterator = Entities(all_components).Iterator;
|
||||
|
||||
pub const System = fn (adapter: *Adapter) void;
|
||||
|
||||
pub const World = struct {
|
||||
allocator: Allocator,
|
||||
systems: std.StringArrayHashMapUnmanaged(System) = .{},
|
||||
entities: Entities,
|
||||
|
||||
pub fn init(allocator: Allocator) !World {
|
||||
return World{
|
||||
.allocator = allocator,
|
||||
.entities = try Entities.init(allocator),
|
||||
};
|
||||
}
|
||||
|
||||
pub fn deinit(world: *World) void {
|
||||
world.systems.deinit(world.allocator);
|
||||
world.entities.deinit();
|
||||
}
|
||||
|
||||
pub fn register(world: *World, name: []const u8, system: System) !void {
|
||||
try world.systems.put(world.allocator, name, system);
|
||||
}
|
||||
|
||||
pub fn unregister(world: *World, name: []const u8) void {
|
||||
world.systems.orderedRemove(name);
|
||||
}
|
||||
|
||||
pub fn tick(world: *World) void {
|
||||
var i: usize = 0;
|
||||
while (i < world.systems.count()) : (i += 1) {
|
||||
const system = world.systems.entries.get(i).value;
|
||||
|
||||
var adapter = Adapter{
|
||||
.world = world,
|
||||
};
|
||||
system(&adapter);
|
||||
pub fn query(adapter: *Self, components: []const []const u8) Iterator {
|
||||
return adapter.world.entities.query(components);
|
||||
}
|
||||
}
|
||||
};
|
||||
};
|
||||
}
|
||||
|
||||
pub fn World(all_components: anytype) type {
|
||||
return struct {
|
||||
allocator: Allocator,
|
||||
systems: std.StringArrayHashMapUnmanaged(System) = .{},
|
||||
entities: Entities(all_components),
|
||||
|
||||
const Self = @This();
|
||||
pub const System = fn (adapter: *Adapter(all_components)) void;
|
||||
|
||||
pub fn init(allocator: Allocator) !Self {
|
||||
return Self{
|
||||
.allocator = allocator,
|
||||
.entities = try Entities(all_components).init(allocator),
|
||||
};
|
||||
}
|
||||
|
||||
pub fn deinit(world: *Self) void {
|
||||
world.systems.deinit(world.allocator);
|
||||
world.entities.deinit();
|
||||
}
|
||||
|
||||
pub fn register(world: *Self, name: []const u8, system: System) !void {
|
||||
try world.systems.put(world.allocator, name, system);
|
||||
}
|
||||
|
||||
pub fn unregister(world: *Self, name: []const u8) void {
|
||||
world.systems.orderedRemove(name);
|
||||
}
|
||||
|
||||
pub fn tick(world: *Self) void {
|
||||
var i: usize = 0;
|
||||
while (i < world.systems.count()) : (i += 1) {
|
||||
const system = world.systems.entries.get(i).value;
|
||||
|
||||
var adapter = Adapter(all_components){
|
||||
.world = world,
|
||||
};
|
||||
system(&adapter);
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue