fixed component queries
This commit is contained in:
parent
0c8cd85647
commit
191e148535
2 changed files with 42 additions and 9 deletions
|
|
@ -3,8 +3,8 @@ const Allocator = std.mem.Allocator;
|
|||
const components = @import("components.zig");
|
||||
const sparse = @import("sparse.zig");
|
||||
|
||||
const System = *const fn (*Pool) void;
|
||||
const SystemGroup = std.ArrayList(System);
|
||||
pub const System = *const fn (*Pool) void;
|
||||
pub const SystemGroup = []const System;
|
||||
|
||||
pub const Pool = struct {
|
||||
// Components
|
||||
|
|
@ -42,6 +42,10 @@ pub const Pool = struct {
|
|||
return pool;
|
||||
}
|
||||
|
||||
pub fn addSystemGroup(self: *@This(), group: SystemGroup) !void {
|
||||
try self.system_groups.append(group);
|
||||
}
|
||||
|
||||
pub fn deinit(self: *@This(), allocator: Allocator) void {
|
||||
self.position.deinit();
|
||||
self.speed.deinit();
|
||||
|
|
@ -54,16 +58,16 @@ pub const Pool = struct {
|
|||
}
|
||||
|
||||
pub fn tick(self: *@This()) void {
|
||||
for (self.system_groups) |group| {
|
||||
for (0..self.system_groups.items.len) |i| {
|
||||
self.thread_pool.spawnWg(&self.wait_group, struct {
|
||||
fn run(pool: *Pool) void {
|
||||
fn run(pool: *Pool, index: usize) void {
|
||||
const group = pool.system_groups.items[index];
|
||||
for (group) |system| {
|
||||
system(pool);
|
||||
}
|
||||
}
|
||||
}.run, .{self});
|
||||
}.run, .{ self, i });
|
||||
}
|
||||
self.wait_group.wait();
|
||||
}
|
||||
|
||||
pub fn createEntity(self: *@This()) !usize {
|
||||
|
|
@ -87,8 +91,8 @@ pub const Pool = struct {
|
|||
|
||||
pub fn addComponent(self: *@This(), entity: usize, component: anytype) !void {
|
||||
var set = switch (@TypeOf(component)) {
|
||||
components.Speed => self.speed,
|
||||
components.Position => self.position,
|
||||
components.Speed => &self.speed,
|
||||
components.Position => &self.position,
|
||||
else => unreachable,
|
||||
};
|
||||
|
||||
|
|
|
|||
31
src/main.zig
31
src/main.zig
|
|
@ -11,6 +11,25 @@ const wasm = @import("mods/wasm.zig");
|
|||
const components = @import("ecs/components.zig");
|
||||
const entities = @import("ecs/entities.zig");
|
||||
|
||||
fn testSystem2(pool: *entities.Pool) void {
|
||||
var i = @as(usize, 0);
|
||||
//std.debug.print("test: {d}\n", .{pool.position.components.items.len});
|
||||
for (pool.position.components.items) |position| {
|
||||
const entity = pool.position.dense.items[i];
|
||||
if (pool.speed.dense.items[pool.speed.sparse.items[entity]] == entity) {
|
||||
const speed = pool.speed.components.items[pool.speed.sparse.items[entity]];
|
||||
std.debug.print("entity{d}: {any},{any},{any} {any}\n", .{ i, position.x, position.y, position.z, speed.speed });
|
||||
}
|
||||
|
||||
i += 1;
|
||||
}
|
||||
}
|
||||
|
||||
fn testSystem(pool: *entities.Pool) void {
|
||||
_ = pool;
|
||||
std.debug.print("test\n", .{});
|
||||
}
|
||||
|
||||
pub fn main() !void {
|
||||
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
|
||||
const allocator = gpa.allocator();
|
||||
|
|
@ -31,8 +50,17 @@ pub fn main() !void {
|
|||
|
||||
var pool = try entities.Pool.init(allocator);
|
||||
defer pool.deinit(allocator);
|
||||
|
||||
//try pool.addSystemGroup(&[_]entities.System{
|
||||
// testSystem,
|
||||
//});
|
||||
try pool.addSystemGroup(&[_]entities.System{
|
||||
testSystem2,
|
||||
});
|
||||
|
||||
const entity = try pool.createEntity();
|
||||
try pool.addComponent(entity, components.Speed{ .speed = 0.0 });
|
||||
try pool.addComponent(entity, components.Position{ .x = 1.0, .y = 0.5, .z = 3.0 });
|
||||
try pool.addComponent(entity, components.Speed{ .speed = 5.0 });
|
||||
|
||||
// TODO(luccie-cmd): Renderer.create shouldn't return an error
|
||||
var r = try Renderer.create(allocator, w);
|
||||
|
|
@ -41,6 +69,7 @@ pub fn main() !void {
|
|||
while (!w.shouldClose()) {
|
||||
c.glfwPollEvents();
|
||||
try r.tick();
|
||||
pool.tick();
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue