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 ---
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();

View file

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

View file

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

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 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
];

View file

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

View file

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