diff options
author | Lorenzo Torres <torres@sideros.org> | 2025-03-28 15:34:36 +0100 |
---|---|---|
committer | Lorenzo Torres <torres@sideros.org> | 2025-03-28 15:34:36 +0100 |
commit | ff84d6ac5375587428f279c06ead111301a332ab (patch) | |
tree | a26f32397f66693494ab17187a212cb52729ff3f /src/main.zig | |
parent | 11f6bc2b04fae03a6e81735d4bcebe9505c5d76d (diff) |
the ECS is now using a more data oriented approach.
By defining archetypes using SOAs (Zig has this data structure in `std`,
called std.MultiArrayList), the engine can iterate faster over commonly
defined entities avoiding cache misses since each component is aligned
with other components of the same entity.
Diffstat (limited to 'src/main.zig')
-rw-r--r-- | src/main.zig | 17 |
1 files changed, 10 insertions, 7 deletions
diff --git a/src/main.zig b/src/main.zig index 4bbe084..5bcff43 100644 --- a/src/main.zig +++ b/src/main.zig @@ -1,5 +1,4 @@ const std = @import("std"); - const config = @import("config"); const math = @import("sideros").math; const mods = @import("mods"); @@ -7,11 +6,10 @@ const ecs = @import("ecs/ecs.zig"); pub const Renderer = @import("renderer"); fn testSystem2(pool: *ecs.Pool) void { - for (pool.getQuery(ecs.components.Position), 0..) |position, i| { - const entity = pool.getEntity(i, ecs.components.Position); - if (pool.getComponent(entity, ecs.components.Speed)) |speed| { - std.debug.print("entity{d}: {any},{any},{any} {any}\n", .{ i, position.x, position.y, position.z, speed.speed }); - } + const slice = pool.humans.slice(); + + for (slice.items(.position), slice.items(.speed)) |position, speed| { + std.debug.print("entity: {any} {any} {any}: {any}\n", .{ position.x, position.y, position.z, speed.speed }); } } @@ -53,7 +51,12 @@ pub fn main() !void { }; var pool = try ecs.Pool.init(allocator, resources); - defer pool.deinit(allocator); + defer pool.deinit(); + + _ = try pool.createEntity(ecs.entities.Human{ + .position = .{ .x = 0.0, .y = 1.0, .z = 0.0 }, + .speed = .{ .speed = 5.0 }, + }); try pool.addSystemGroup(&[_]ecs.System{ testSystem2, |