From b9ce1f27e931d8b16a9d2033a53802f527a8fa08 Mon Sep 17 00:00:00 2001 From: abux Date: Sat, 1 Aug 2026 18:54:24 +0100 Subject: [PATCH] Added proper Entity type | Shortcuts to [Plugin, Entity] in app.zig --- src/app.zig | 5 +++++ src/example/simple/system.zig | 2 +- src/world/archetype.zig | 7 ++++--- src/world/entity.zig | 12 ++++++++++++ src/world/query.zig | 3 ++- src/world/root.zig | 1 + src/world/world.zig | 24 ++++++++++++------------ 7 files changed, 37 insertions(+), 17 deletions(-) create mode 100644 src/world/entity.zig diff --git a/src/app.zig b/src/app.zig index f08344b..c10e2e1 100644 --- a/src/app.zig +++ b/src/app.zig @@ -4,11 +4,16 @@ // --- LIBS --- const std = @import("std"); const root = @import("root"); + pub const event = @import("event/root.zig"); pub const plugin = @import("plugin/root.zig"); pub const resource = @import("resource/root.zig"); pub const scheduler = @import("scheduler/root.zig"); pub const ecs = @import("world/root.zig"); + +pub const Entity = ecs.Entity; +pub const Plugin = plugin.Plugin; + const template = @import("template/root.zig"); const Self = @This(); diff --git a/src/example/simple/system.zig b/src/example/simple/system.zig index d923f95..1e968db 100644 --- a/src/example/simple/system.zig +++ b/src/example/simple/system.zig @@ -188,7 +188,7 @@ pub fn test_ecs( // --- ENTITIES DEBUG --- std.debug.print("\x1b[36m", .{}); std.debug.print( - \\| ID: {} + \\| ID: {f} \\| Components: \\ | Player: {?f} \\ | Material: {f} diff --git a/src/world/archetype.zig b/src/world/archetype.zig index c8fceb5..f180657 100644 --- a/src/world/archetype.zig +++ b/src/world/archetype.zig @@ -2,6 +2,7 @@ const std = @import("std"); const Signature = @import("signature.zig"); const Column = @import("column.zig"); +const Entity = @import("entity.zig"); const TypeID = @import("../template/type_id.zig").of; const Self = @This(); @@ -11,7 +12,7 @@ const Self = @This(); signature: Signature, type_map: std.AutoHashMap(u64, usize), -entities: std.ArrayList(u64), +entities: std.ArrayList(Entity), columns: std.ArrayList(Column), alloc: std.mem.Allocator, @@ -78,7 +79,7 @@ pub fn addColumns( /// ---------------------------------------------------- pub fn add( self: *Self, - entity: u64, + entity: Entity, components: anytype, ) !void { const fields = std.meta.fields(@TypeOf(components)); @@ -126,7 +127,7 @@ pub fn get( pub fn remove( self: *Self, row: usize, -) ?u64 { +) ?Entity { const last_entity = self.entities.items[self.entities.items.len - 1]; _ = self.entities.swapRemove(row); diff --git a/src/world/entity.zig b/src/world/entity.zig new file mode 100644 index 0000000..a5696f1 --- /dev/null +++ b/src/world/entity.zig @@ -0,0 +1,12 @@ +const std = @import("std"); + +pub const IDType = u64; + +id: IDType, + +pub fn format( + self: @This(), + writer: *std.Io.Writer, +) !void { + try writer.print("{d}", .{self.id}); +} diff --git a/src/world/query.zig b/src/world/query.zig index 2b9d407..13d9bfb 100644 --- a/src/world/query.zig +++ b/src/world/query.zig @@ -1,6 +1,7 @@ const std = @import("std"); const World = @import("world.zig"); const Archetype = @import("archetype.zig"); +const Entity = @import("entity.zig"); const QueryFilters = @import("query_filters.zig"); const TypeID = @import("../template/type_id.zig").of; @@ -131,7 +132,7 @@ pub fn Query( /// ---------------------------------------------------- /// ---------------------------------------------------- - pub fn entity(self: *const Self) u64 { + pub fn entity(self: *const Self) Entity { return self.current_arch.?.entities.items[ self.current_row ]; diff --git a/src/world/root.zig b/src/world/root.zig index e6a9653..41dc228 100644 --- a/src/world/root.zig +++ b/src/world/root.zig @@ -1,2 +1,3 @@ pub const World = @import("world.zig"); +pub const Entity = @import("entity.zig"); pub const Archetype = @import("archetype.zig"); diff --git a/src/world/world.zig b/src/world/world.zig index d139d59..cfd22f3 100644 --- a/src/world/world.zig +++ b/src/world/world.zig @@ -2,6 +2,7 @@ const std = @import("std"); const Archetype = @import("archetype.zig"); const Signature = @import("signature.zig"); +const Entity = @import("entity.zig"); const EntityRecord = @import("entity_record.zig"); const QueryFilters = @import("query_filters.zig"); const Query = @import("query.zig").Query; @@ -13,9 +14,9 @@ const Self = @This(); // archetypes: std.ArrayList(Archetype), -entity_map: std.AutoHashMap(u64, EntityRecord), +entity_map: std.AutoHashMap(Entity, EntityRecord), signature_map: std.AutoHashMap(u64, usize), -next_entity: u64, +next_entity: Entity.IDType, alloc: std.mem.Allocator, /// ---------------------------------------------------- @@ -55,7 +56,7 @@ pub fn spawn( /// ---------------------------------------------------- pub fn addComponents( self: *Self, - entity: u64, + entity: Entity, components: anytype, ) !void { const record = self.entity_map.get(entity) orelse return; @@ -181,7 +182,7 @@ pub fn addComponents( pub fn getComponent( self: *Self, comptime T: type, - entity: u64, + entity: Entity, ) ?*T { const record = self.entity_map.get(entity) orelse return null; const arch = &self.archetypes.items[record.archetype_idx]; @@ -193,7 +194,7 @@ pub fn getComponent( pub fn getComponents( self: *Self, comptime Result: type, - entity: u64, + entity: Entity, ) error{ComponentNotRegistered}!Result { // --- RESULT --- var result: Result = undefined; @@ -230,7 +231,7 @@ pub fn getComponents( pub fn hasComponent( self: *Self, comptime T: type, - entity: u64, + entity: Entity, ) bool { return self.getComponent(T, entity) != null; } @@ -239,7 +240,7 @@ pub fn hasComponent( /// ---------------------------------------------------- pub fn despawn( self: *Self, - entity: u64, + entity: Entity, ) void { const record = self.entity_map.get(entity) orelse return; if (self.archetypes.items[record.archetype_idx].remove(record.row)) |moved| { @@ -253,7 +254,7 @@ pub fn despawn( pub fn spawnEntity( self: *Self, components: anytype, -) !u64 { +) !Entity { const Components = @TypeOf(components); const fields = std.meta.fields(Components); @@ -308,8 +309,7 @@ pub fn spawnEntity( }; // --- NEW ENTITY --- - const entity = self.next_entity; - self.next_entity += 1; + const entity = self.createEntity(); // --- ADD ROW --- var arch = &self.archetypes.items[archetype_idx]; @@ -332,8 +332,8 @@ pub fn spawnEntity( /// ---------------------------------------------------- /// ---------------------------------------------------- -fn createEntity(self: *Self) u64 { - const entity = self.next_entity; +fn createEntity(self: *Self) Entity { + const entity = Entity{ .id = self.next_entity }; self.next_entity += 1; return entity; }