ecs: remove half-baked system implementation
Signed-off-by: Stephen Gutekanst <stephen@hexops.com>
This commit is contained in:
parent
f74faf90df
commit
98ec7f5190
2 changed files with 4 additions and 68 deletions
|
|
@ -51,13 +51,13 @@ test "example" {
|
||||||
const modules = Modules(.{
|
const modules = Modules(.{
|
||||||
.physics = Module(.{
|
.physics = Module(.{
|
||||||
.components = .{
|
.components = .{
|
||||||
.id = u16,
|
.id = u32,
|
||||||
},
|
},
|
||||||
.globals = struct{
|
.globals = struct {
|
||||||
pointer: u8,
|
pointer: u8,
|
||||||
},
|
},
|
||||||
}),
|
}),
|
||||||
.geometry = Module(.{
|
.renderer = Module(.{
|
||||||
.components = .{
|
.components = .{
|
||||||
.id = u16,
|
.id = u16,
|
||||||
},
|
},
|
||||||
|
|
@ -77,35 +77,8 @@ test "example" {
|
||||||
const player2 = try world.entities.new();
|
const player2 = try world.entities.new();
|
||||||
const player3 = try world.entities.new();
|
const player3 = try world.entities.new();
|
||||||
try world.entities.setComponent(player1, .physics, .id, 1234);
|
try world.entities.setComponent(player1, .physics, .id, 1234);
|
||||||
try world.entities.setComponent(player1, .geometry, .id, 1234);
|
try world.entities.setComponent(player1, .renderer, .id, 1234);
|
||||||
|
|
||||||
try world.entities.setComponent(player2, .physics, .id, 1234);
|
try world.entities.setComponent(player2, .physics, .id, 1234);
|
||||||
try world.entities.setComponent(player3, .physics, .id, 1234);
|
try world.entities.setComponent(player3, .physics, .id, 1234);
|
||||||
|
|
||||||
// TODO: integrate systems with modules.
|
|
||||||
// const physics = (struct {
|
|
||||||
// pub fn physics(adapter: *Adapter(modules)) void {
|
|
||||||
// var iter = adapter.query(&.{"physics"});
|
|
||||||
// std.debug.print("\nphysics ran\n", .{});
|
|
||||||
// while (iter.next()) |row| {
|
|
||||||
// std.debug.print("found entity: {}\n", .{row.entity});
|
|
||||||
// defer row.unlock();
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
// }).physics;
|
|
||||||
// try world.register("physics", physics);
|
|
||||||
|
|
||||||
// const rendering = (struct {
|
|
||||||
// pub fn rendering(adapter: *Adapter(modules)) void {
|
|
||||||
// var iter = adapter.query(&.{"geometry"});
|
|
||||||
// std.debug.print("\nrendering ran\n", .{});
|
|
||||||
// while (iter.next()) |row| {
|
|
||||||
// std.debug.print("found entity: {}\n", .{row.entity});
|
|
||||||
// defer row.unlock();
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
// }).rendering;
|
|
||||||
// try world.register("rendering", rendering);
|
|
||||||
|
|
||||||
world.tick();
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -6,20 +6,6 @@ const StructField = std.builtin.Type.StructField;
|
||||||
|
|
||||||
const Entities = @import("entities.zig").Entities;
|
const Entities = @import("entities.zig").Entities;
|
||||||
|
|
||||||
pub fn Adapter(modules: anytype) type {
|
|
||||||
const all_components = NamespacedComponents(modules);
|
|
||||||
return struct {
|
|
||||||
world: *World(modules),
|
|
||||||
|
|
||||||
const Self = @This();
|
|
||||||
pub const Iterator = Entities(all_components).Iterator;
|
|
||||||
|
|
||||||
pub fn query(adapter: *Self, components: []const []const u8) Iterator {
|
|
||||||
return adapter.world.entities.query(components);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
/// An ECS module can provide components, systems, and global values.
|
/// An ECS module can provide components, systems, and global values.
|
||||||
pub fn Module(comptime Params: anytype) @TypeOf(Params) {
|
pub fn Module(comptime Params: anytype) @TypeOf(Params) {
|
||||||
// TODO: validate the type
|
// TODO: validate the type
|
||||||
|
|
@ -168,12 +154,10 @@ pub fn World(comptime modules: anytype) type {
|
||||||
const all_components = namespacedComponents(modules);
|
const all_components = namespacedComponents(modules);
|
||||||
return struct {
|
return struct {
|
||||||
allocator: Allocator,
|
allocator: Allocator,
|
||||||
systems: std.StringArrayHashMapUnmanaged(System) = .{},
|
|
||||||
entities: Entities(all_components),
|
entities: Entities(all_components),
|
||||||
globals: NamespacedGlobals(modules),
|
globals: NamespacedGlobals(modules),
|
||||||
|
|
||||||
const Self = @This();
|
const Self = @This();
|
||||||
pub const System = fn (adapter: *Adapter(modules)) void;
|
|
||||||
|
|
||||||
pub fn init(allocator: Allocator) !Self {
|
pub fn init(allocator: Allocator) !Self {
|
||||||
return Self{
|
return Self{
|
||||||
|
|
@ -184,7 +168,6 @@ pub fn World(comptime modules: anytype) type {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn deinit(world: *Self) void {
|
pub fn deinit(world: *Self) void {
|
||||||
world.systems.deinit(world.allocator);
|
|
||||||
world.entities.deinit();
|
world.entities.deinit();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -214,25 +197,5 @@ pub fn World(comptime modules: anytype) type {
|
||||||
@tagName(global_tag),
|
@tagName(global_tag),
|
||||||
) = value;
|
) = value;
|
||||||
}
|
}
|
||||||
|
|
||||||
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(modules){
|
|
||||||
.world = world,
|
|
||||||
};
|
|
||||||
system(&adapter);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue