Added proper Entity type | Shortcuts to [Plugin, Entity] in app.zig

This commit is contained in:
abux 2026-08-01 18:54:24 +01:00
parent 9cbd0804a6
commit b9ce1f27e9
7 changed files with 37 additions and 17 deletions

View file

@ -4,11 +4,16 @@
// --- LIBS --- // --- LIBS ---
const std = @import("std"); const std = @import("std");
const root = @import("root"); const root = @import("root");
pub const event = @import("event/root.zig"); pub const event = @import("event/root.zig");
pub const plugin = @import("plugin/root.zig"); pub const plugin = @import("plugin/root.zig");
pub const resource = @import("resource/root.zig"); pub const resource = @import("resource/root.zig");
pub const scheduler = @import("scheduler/root.zig"); pub const scheduler = @import("scheduler/root.zig");
pub const ecs = @import("world/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 template = @import("template/root.zig");
const Self = @This(); const Self = @This();

View file

@ -188,7 +188,7 @@ pub fn test_ecs(
// --- ENTITIES DEBUG --- // --- ENTITIES DEBUG ---
std.debug.print("\x1b[36m", .{}); std.debug.print("\x1b[36m", .{});
std.debug.print( std.debug.print(
\\| ID: {} \\| ID: {f}
\\| Components: \\| Components:
\\ | Player: {?f} \\ | Player: {?f}
\\ | Material: {f} \\ | Material: {f}

View file

@ -2,6 +2,7 @@
const std = @import("std"); const std = @import("std");
const Signature = @import("signature.zig"); const Signature = @import("signature.zig");
const Column = @import("column.zig"); const Column = @import("column.zig");
const Entity = @import("entity.zig");
const TypeID = @import("../template/type_id.zig").of; const TypeID = @import("../template/type_id.zig").of;
const Self = @This(); const Self = @This();
@ -11,7 +12,7 @@ const Self = @This();
signature: Signature, signature: Signature,
type_map: std.AutoHashMap(u64, usize), type_map: std.AutoHashMap(u64, usize),
entities: std.ArrayList(u64), entities: std.ArrayList(Entity),
columns: std.ArrayList(Column), columns: std.ArrayList(Column),
alloc: std.mem.Allocator, alloc: std.mem.Allocator,
@ -78,7 +79,7 @@ pub fn addColumns(
/// ---------------------------------------------------- /// ----------------------------------------------------
pub fn add( pub fn add(
self: *Self, self: *Self,
entity: u64, entity: Entity,
components: anytype, components: anytype,
) !void { ) !void {
const fields = std.meta.fields(@TypeOf(components)); const fields = std.meta.fields(@TypeOf(components));
@ -126,7 +127,7 @@ pub fn get(
pub fn remove( pub fn remove(
self: *Self, self: *Self,
row: usize, row: usize,
) ?u64 { ) ?Entity {
const last_entity = self.entities.items[self.entities.items.len - 1]; const last_entity = self.entities.items[self.entities.items.len - 1];
_ = self.entities.swapRemove(row); _ = self.entities.swapRemove(row);

12
src/world/entity.zig Normal file
View file

@ -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});
}

View file

@ -1,6 +1,7 @@
const std = @import("std"); const std = @import("std");
const World = @import("world.zig"); const World = @import("world.zig");
const Archetype = @import("archetype.zig"); const Archetype = @import("archetype.zig");
const Entity = @import("entity.zig");
const QueryFilters = @import("query_filters.zig"); const QueryFilters = @import("query_filters.zig");
const TypeID = @import("../template/type_id.zig").of; 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[ return self.current_arch.?.entities.items[
self.current_row self.current_row
]; ];

View file

@ -1,2 +1,3 @@
pub const World = @import("world.zig"); pub const World = @import("world.zig");
pub const Entity = @import("entity.zig");
pub const Archetype = @import("archetype.zig"); pub const Archetype = @import("archetype.zig");

View file

@ -2,6 +2,7 @@
const std = @import("std"); const std = @import("std");
const Archetype = @import("archetype.zig"); const Archetype = @import("archetype.zig");
const Signature = @import("signature.zig"); const Signature = @import("signature.zig");
const Entity = @import("entity.zig");
const EntityRecord = @import("entity_record.zig"); const EntityRecord = @import("entity_record.zig");
const QueryFilters = @import("query_filters.zig"); const QueryFilters = @import("query_filters.zig");
const Query = @import("query.zig").Query; const Query = @import("query.zig").Query;
@ -13,9 +14,9 @@ const Self = @This();
// //
archetypes: std.ArrayList(Archetype), archetypes: std.ArrayList(Archetype),
entity_map: std.AutoHashMap(u64, EntityRecord), entity_map: std.AutoHashMap(Entity, EntityRecord),
signature_map: std.AutoHashMap(u64, usize), signature_map: std.AutoHashMap(u64, usize),
next_entity: u64, next_entity: Entity.IDType,
alloc: std.mem.Allocator, alloc: std.mem.Allocator,
/// ---------------------------------------------------- /// ----------------------------------------------------
@ -55,7 +56,7 @@ pub fn spawn(
/// ---------------------------------------------------- /// ----------------------------------------------------
pub fn addComponents( pub fn addComponents(
self: *Self, self: *Self,
entity: u64, entity: Entity,
components: anytype, components: anytype,
) !void { ) !void {
const record = self.entity_map.get(entity) orelse return; const record = self.entity_map.get(entity) orelse return;
@ -181,7 +182,7 @@ pub fn addComponents(
pub fn getComponent( pub fn getComponent(
self: *Self, self: *Self,
comptime T: type, comptime T: type,
entity: u64, entity: Entity,
) ?*T { ) ?*T {
const record = self.entity_map.get(entity) orelse return null; const record = self.entity_map.get(entity) orelse return null;
const arch = &self.archetypes.items[record.archetype_idx]; const arch = &self.archetypes.items[record.archetype_idx];
@ -193,7 +194,7 @@ pub fn getComponent(
pub fn getComponents( pub fn getComponents(
self: *Self, self: *Self,
comptime Result: type, comptime Result: type,
entity: u64, entity: Entity,
) error{ComponentNotRegistered}!Result { ) error{ComponentNotRegistered}!Result {
// --- RESULT --- // --- RESULT ---
var result: Result = undefined; var result: Result = undefined;
@ -230,7 +231,7 @@ pub fn getComponents(
pub fn hasComponent( pub fn hasComponent(
self: *Self, self: *Self,
comptime T: type, comptime T: type,
entity: u64, entity: Entity,
) bool { ) bool {
return self.getComponent(T, entity) != null; return self.getComponent(T, entity) != null;
} }
@ -239,7 +240,7 @@ pub fn hasComponent(
/// ---------------------------------------------------- /// ----------------------------------------------------
pub fn despawn( pub fn despawn(
self: *Self, self: *Self,
entity: u64, entity: Entity,
) void { ) void {
const record = self.entity_map.get(entity) orelse return; const record = self.entity_map.get(entity) orelse return;
if (self.archetypes.items[record.archetype_idx].remove(record.row)) |moved| { if (self.archetypes.items[record.archetype_idx].remove(record.row)) |moved| {
@ -253,7 +254,7 @@ pub fn despawn(
pub fn spawnEntity( pub fn spawnEntity(
self: *Self, self: *Self,
components: anytype, components: anytype,
) !u64 { ) !Entity {
const Components = @TypeOf(components); const Components = @TypeOf(components);
const fields = std.meta.fields(Components); const fields = std.meta.fields(Components);
@ -308,8 +309,7 @@ pub fn spawnEntity(
}; };
// --- NEW ENTITY --- // --- NEW ENTITY ---
const entity = self.next_entity; const entity = self.createEntity();
self.next_entity += 1;
// --- ADD ROW --- // --- ADD ROW ---
var arch = &self.archetypes.items[archetype_idx]; var arch = &self.archetypes.items[archetype_idx];
@ -332,8 +332,8 @@ pub fn spawnEntity(
/// ---------------------------------------------------- /// ----------------------------------------------------
/// ---------------------------------------------------- /// ----------------------------------------------------
fn createEntity(self: *Self) u64 { fn createEntity(self: *Self) Entity {
const entity = self.next_entity; const entity = Entity{ .id = self.next_entity };
self.next_entity += 1; self.next_entity += 1;
return entity; return entity;
} }