Improved documentation

This commit is contained in:
abux 2026-08-03 15:39:11 +01:00
parent eba8ba3bf2
commit e6c46e2371
2 changed files with 105 additions and 28 deletions

View file

@ -1,9 +1,17 @@
const std = @import("std"); //! ----------------------------------------------------
//! ----------------------------------------------------
const std = @import("std");
pub const IDType = u64; pub const IDType = u64;
//
// FIELDS
//
id: IDType, id: IDType,
/// ----------------------------------------------------
/// ----------------------------------------------------
pub fn format( pub fn format(
self: @This(), self: @This(),
writer: *std.Io.Writer, writer: *std.Io.Writer,

View file

@ -1,4 +1,5 @@
//! ---------------------------------------------------- //! ----------------------------------------------------
//! `🗲` **Archetype ECS** `🗲`
//! ---------------------------------------------------- //! ----------------------------------------------------
// //
@ -45,10 +46,12 @@ pub fn init(alloc: std.mem.Allocator) Self {
/// ---------------------------------------------------- /// ----------------------------------------------------
/// ---------------------------------------------------- /// ----------------------------------------------------
pub fn deinit(self: *Self) void { pub fn deinit(self: *Self) void {
// --- ARCHETYPES --- // --- FREE ARCHETYPES ---
for (self.archetypes.items) |*arch| { for (self.archetypes.items) |*arch| {
arch.deinit(); arch.deinit();
} }
// --- # ---
self.entity_map.deinit(); self.entity_map.deinit();
self.archetypes.deinit(self.alloc); self.archetypes.deinit(self.alloc);
self.signature_map.deinit(); self.signature_map.deinit();
@ -73,6 +76,18 @@ pub fn spawn(
} }
/// ---------------------------------------------------- /// ----------------------------------------------------
/// # Example
///
/// ```zig
/// const player = try world.spawnEntity(.{
/// Player{},
/// });
///
/// try world.addComponents(player, .{
/// Transform{},
/// Velocity{},
/// });
/// ```
/// ---------------------------------------------------- /// ----------------------------------------------------
pub fn addComponents( pub fn addComponents(
self: *Self, self: *Self,
@ -198,6 +213,16 @@ pub fn addComponents(
} }
/// ---------------------------------------------------- /// ----------------------------------------------------
/// # Example
///
/// ```zig
/// const player = try world.spawnEntity(.{
/// Player{},
/// Inventory{},
/// });
///
/// if (world.getComponent(Inventory, player)) |inv| {...}
/// ```
/// ---------------------------------------------------- /// ----------------------------------------------------
pub fn getComponent( pub fn getComponent(
self: *Self, self: *Self,
@ -210,6 +235,25 @@ pub fn getComponent(
} }
/// ---------------------------------------------------- /// ----------------------------------------------------
/// # Example
///
/// ```zig
/// const player = try world.spawnEntity(.{
/// Player{},
/// Inventory{},
/// Transform{},
/// Velocity{},
/// });
///
/// const cmp = try world.getComponents(struct{
/// inv: *Inventory,
/// velocity: *Velocity,
/// camera_target: ?*const CameraTarget,
/// }, player);
///
/// cmp.velocity.value[0] += 1;
/// if (cmp.camera_target) |ct| {...}
/// ```
/// ---------------------------------------------------- /// ----------------------------------------------------
pub fn getComponents( pub fn getComponents(
self: *Self, self: *Self,
@ -247,6 +291,23 @@ pub fn getComponents(
} }
/// ---------------------------------------------------- /// ----------------------------------------------------
/// # Example
///
/// ```zig
/// const player = try world.spawnEntity(.{
/// Player{},
/// Inventory{},
/// });
///
/// if (world.hasComponent(Inventory), player)
/// std.debug.print("[PLAYER] Has Inventory Component\n", .{});
/// ```
///
/// # Output
///
/// ```text
/// [PLAYER] Has Inventory Component
/// ```
/// ---------------------------------------------------- /// ----------------------------------------------------
pub fn hasComponent( pub fn hasComponent(
self: *Self, self: *Self,
@ -370,6 +431,35 @@ pub fn spawnEntity(
return entity; return entity;
} }
/// ----------------------------------------------------
/// # Filters
///
/// ```zig
/// .with = &.{Velocity},
/// .without = &.{ Exclude, Vehicle, Dead },
/// ```
///
/// # Example
///
/// ```zig
/// var query = world.query(struct {
/// player: ?*Player,
/// pos: *Position,
/// vel: *Velocity,
/// }, .{});
/// while (query.next()) |entity| {
/// if (entity.player) |player| {}
/// }
/// ```
/// ----------------------------------------------------
pub fn query(
self: *Self,
comptime Result: type,
comptime Filters: QueryFilters,
) Query(Result, Filters) {
return .init(self);
}
/// ---------------------------------------------------- /// ----------------------------------------------------
/// ---------------------------------------------------- /// ----------------------------------------------------
fn createEntity(self: *Self) Entity { fn createEntity(self: *Self) Entity {
@ -384,8 +474,9 @@ fn createArchetype(
self: *Self, self: *Self,
comptime Components: type, comptime Components: type,
) !usize { ) !usize {
const fields = const fields = std.meta.fields(
std.meta.fields(Components); Components,
);
var ids = try self.alloc.alloc( var ids = try self.alloc.alloc(
u64, u64,
@ -419,28 +510,6 @@ fn createArchetype(
return self.archetypes.items.len - 1; return self.archetypes.items.len - 1;
} }
/// ----------------------------------------------------
/// # Example
///
/// ```zig
/// var query = world.query(struct {
/// player: ?*Player,
/// pos: *Position,
/// vel: *Velocity,
/// }, .{});
/// while (query.next()) |entity| {
/// if (entity.player) |player| {}
/// }
/// ```
/// ----------------------------------------------------
pub fn query(
self: *Self,
comptime Result: type,
comptime Filters: QueryFilters,
) Query(Result, Filters) {
return .init(self);
}
// //
// TESTING // TESTING
// //
@ -488,7 +557,7 @@ test "basic" {
e.transform.pos[1] += e.velocity.value[1]; e.transform.pos[1] += e.velocity.value[1];
// --- DEBUG PLAYER --- // --- DEBUG PLAYER ---
if (e.player) |player| { if (e.player) |player_info| {
std.debug.print( std.debug.print(
\\({f}) PLAYER \\({f}) PLAYER
\\| Name: {s} \\| Name: {s}
@ -497,7 +566,7 @@ test "basic" {
\\ \\
, .{ , .{
it.entity(), it.entity(),
player.name, player_info.name,
e.transform.pos, e.transform.pos,
e.transform.scale, e.transform.scale,
}); });