module: rename Entities -> Database

Signed-off-by: Stephen Gutekanst <stephen@hexops.com>
This commit is contained in:
Stephen Gutekanst 2024-05-07 16:35:27 -07:00 committed by Stephen Gutekanst
parent 2b6aea7d91
commit 11ebce62a3
4 changed files with 29 additions and 29 deletions

View file

@ -53,8 +53,8 @@ component_names: *StringTable,
/// A hash composed of all Column.name's, effectively acting as the unique name of this table. /// A hash composed of all Column.name's, effectively acting as the unique name of this table.
hash: u64, hash: u64,
/// An index to Entities.archetypes, used in the event of a *bucket* hash collision (not a collision /// An index to Database.archetypes, used in the event of a *bucket* hash collision (not a collision
/// of the .hash field) - see Entities.archetypeOrPut for details. /// of the .hash field) - see Database.archetypeOrPut for details.
next: ?u32 = null, next: ?u32 = null,
pub fn deinit(storage: *Archetype, gpa: Allocator) void { pub fn deinit(storage: *Archetype, gpa: Allocator) void {

View file

@ -14,7 +14,7 @@ const ModuleName = @import("module.zig").ModuleName;
const ComponentNameM = @import("module.zig").ComponentNameM; const ComponentNameM = @import("module.zig").ComponentNameM;
const ComponentName = @import("module.zig").ComponentName; const ComponentName = @import("module.zig").ComponentName;
/// An entity ID uniquely identifies an entity globally within an Entities set. /// An entity ID uniquely identifies an entity globally within a Database.
pub const EntityID = u64; pub const EntityID = u64;
fn byTypeId(context: void, lhs: Archetype.Column, rhs: Archetype.Column) bool { fn byTypeId(context: void, lhs: Archetype.Column, rhs: Archetype.Column) bool {
@ -25,7 +25,7 @@ fn byTypeId(context: void, lhs: Archetype.Column, rhs: Archetype.Column) bool {
/// A database of entities. For example, all player, monster, etc. entities in a game world. /// A database of entities. For example, all player, monster, etc. entities in a game world.
/// ///
/// ``` /// ```
/// const world = Entities.init(allocator); // all entities in our world. /// const world = Database.init(allocator); // all entities in our world.
/// defer world.deinit(); /// defer world.deinit();
/// ///
/// const player1 = world.new(); // our first "player" entity /// const player1 = world.new(); // our first "player" entity
@ -67,13 +67,13 @@ fn byTypeId(context: void, lhs: Archetype.Column, rhs: Archetype.Column) bool {
/// deleted are merely marked as "unused" and recycled /// deleted are merely marked as "unused" and recycled
/// ///
/// Database equivalents: /// Database equivalents:
/// * Entities is a database of tables, where each table represents a single archetype. /// * Database is a database of tables, where each table represents a single archetype.
/// * Archetype is a table, whose rows are entities and columns are components. /// * Archetype is a table, whose rows are entities and columns are components.
/// * EntityID is a mere 32-bit array index, pointing to a 16-bit archetype table index and 32-bit /// * EntityID is a mere 32-bit array index, pointing to a 16-bit archetype table index and 32-bit
/// row index, enabling entities to "move" from one archetype table to another seamlessly and /// row index, enabling entities to "move" from one archetype table to another seamlessly and
/// making lookup by entity ID a few cheap array indexing operations. /// making lookup by entity ID a few cheap array indexing operations.
/// * ComponentStorage(T) is a column of data within a table for a single type of component `T`. /// * ComponentStorage(T) is a column of data within a table for a single type of component `T`.
pub fn Entities(comptime modules: anytype) type { pub fn Database(comptime modules: anytype) type {
const component_types_by_name = ComponentTypesByName(modules){}; const component_types_by_name = ComponentTypesByName(modules){};
return struct { return struct {
allocator: Allocator, allocator: Allocator,
@ -104,7 +104,7 @@ pub fn Entities(comptime modules: anytype) type {
/// of that table. That is, the entity's component values are stored at: /// of that table. That is, the entity's component values are stored at:
/// ///
/// ``` /// ```
/// Entities.archetypes.items[ptr.archetype_index].rows[ptr.row_index] /// Database.archetypes.items[ptr.archetype_index].rows[ptr.row_index]
/// ``` /// ```
/// ///
pub const Pointer = struct { pub const Pointer = struct {
@ -271,9 +271,9 @@ pub fn Entities(comptime modules: anytype) type {
/// Given a component name, returns its ID. A new ID is created if needed. /// Given a component name, returns its ID. A new ID is created if needed.
/// ///
/// The set of components used is expected to be static for the lifetime of the Entities, /// The set of components used is expected to be static for the lifetime of the Database,
/// and as such this function allocates component names but there is no way to release that /// and as such this function allocates component names but there is no way to release that
/// memory until Entities.deinit() is called. /// memory until Database.deinit() is called.
pub fn componentNameString(entities: *Self, name_str: []const u8) StringTable.Index { pub fn componentNameString(entities: *Self, name_str: []const u8) StringTable.Index {
return entities.component_names.indexOrPut(entities.allocator, name_str) catch @panic("TODO: implement stateful OOM"); return entities.component_names.indexOrPut(entities.allocator, name_str) catch @panic("TODO: implement stateful OOM");
} }
@ -871,15 +871,15 @@ pub fn Entities(comptime modules: anytype) type {
// TODO: move this type somewhere else // TODO: move this type somewhere else
pub fn ArchetypeIterator(comptime modules: anytype) type { pub fn ArchetypeIterator(comptime modules: anytype) type {
const EntitiesT = Entities(modules); const DatabaseT = Database(modules);
return struct { return struct {
entities: *EntitiesT, entities: *DatabaseT,
query: EntitiesT.QueryDeprecated, query: DatabaseT.QueryDeprecated,
index: usize, index: usize,
const Self = @This(); const Self = @This();
pub fn init(entities: *EntitiesT, query: EntitiesT.QueryDeprecated) Self { pub fn init(entities: *DatabaseT, query: DatabaseT.QueryDeprecated) Self {
return Self{ return Self{
.entities = entities, .entities = entities,
.query = query, .query = query,
@ -935,7 +935,7 @@ test {
const modules = merge(.{ const modules = merge(.{
builtin_modules, builtin_modules,
}); });
std.testing.refAllDeclsRecursive(Entities(modules)); std.testing.refAllDeclsRecursive(Database(modules));
} }
// TODO: require "one big registration of components" even when using dynamic API? Would alleviate // TODO: require "one big registration of components" even when using dynamic API? Would alleviate
@ -954,7 +954,7 @@ test "dynamic" {
const Rotation = struct { degrees: f32 }; const Rotation = struct { degrees: f32 };
// Create a world. // Create a world.
var world = try Entities(merge(.{builtin_modules})).init(allocator); var world = try Database(merge(.{builtin_modules})).init(allocator);
defer world.deinit(); defer world.deinit();
// Create an entity and add dynamic components. // Create an entity and add dynamic components.
@ -1002,7 +1002,7 @@ test "example" {
//------------------------------------------------------------------------- //-------------------------------------------------------------------------
// Create a world. // Create a world.
var world = try Entities(modules).init(allocator); var world = try Database(modules).init(allocator);
defer world.deinit(); defer world.deinit();
//------------------------------------------------------------------------- //-------------------------------------------------------------------------
@ -1112,7 +1112,7 @@ test "example" {
test "empty_world" { test "empty_world" {
const allocator = testing.allocator; const allocator = testing.allocator;
//------------------------------------------------------------------------- //-------------------------------------------------------------------------
var world = try Entities(merge(.{builtin_modules})).init(allocator); var world = try Database(merge(.{builtin_modules})).init(allocator);
// Create a world. // Create a world.
defer world.deinit(); defer world.deinit();
} }
@ -1141,7 +1141,7 @@ test "many entities" {
}); });
// Create many entities // Create many entities
var world = try Entities(modules).init(allocator); var world = try Database(modules).init(allocator);
defer world.deinit(); defer world.deinit();
for (0..8192) |_| { for (0..8192) |_| {
const player = try world.new(); const player = try world.new();

View file

@ -3,7 +3,7 @@ const mach = @import("../main.zig");
const testing = std.testing; const testing = std.testing;
pub const EntityID = @import("entities.zig").EntityID; pub const EntityID = @import("entities.zig").EntityID;
pub const Entities = @import("entities.zig").Entities; pub const Database = @import("entities.zig").Database;
pub const Archetype = @import("Archetype.zig"); pub const Archetype = @import("Archetype.zig");
pub const ModSet = @import("module.zig").ModSet; pub const ModSet = @import("module.zig").ModSet;
pub const Modules = @import("module.zig").Modules; pub const Modules = @import("module.zig").Modules;

View file

@ -63,7 +63,7 @@ const builtin = @import("builtin");
const std = @import("std"); const std = @import("std");
const testing = @import("../testing.zig"); const testing = @import("../testing.zig");
const Entities = @import("entities.zig").Entities; const Database = @import("entities.zig").Database;
const EntityID = @import("entities.zig").EntityID; const EntityID = @import("entities.zig").EntityID;
const is_debug = @import("Archetype.zig").is_debug; const is_debug = @import("Archetype.zig").is_debug;
const builtin_modules = @import("main.zig").builtin_modules; const builtin_modules = @import("main.zig").builtin_modules;
@ -201,12 +201,12 @@ pub fn Modules(comptime modules: anytype) type {
events: EventQueue, events: EventQueue,
mod: ModsByName(modules), mod: ModsByName(modules),
// TODO: pass mods directly instead of ComponentTypesByName? // TODO: pass mods directly instead of ComponentTypesByName?
entities: Entities(modules), entities: Database(modules),
debug_trace: bool, debug_trace: bool,
pub fn init(m: *@This(), allocator: std.mem.Allocator) !void { pub fn init(m: *@This(), allocator: std.mem.Allocator) !void {
// TODO: switch Entities to stack allocation like Modules is // TODO: switch Database to stack allocation like Modules is
var entities = try Entities(modules).init(allocator); var entities = try Database(modules).init(allocator);
errdefer entities.deinit(); errdefer entities.deinit();
const debug_trace_str = std.process.getEnvVarOwned( const debug_trace_str = std.process.getEnvVarOwned(
@ -579,13 +579,13 @@ pub fn ModSet(comptime modules: anytype) type {
// The .entity module is a special builtin module, with its own unique API. // The .entity module is a special builtin module, with its own unique API.
return struct { return struct {
/// Private/internal fields /// Private/internal fields
__entities: *Entities(modules), __entities: *Database(modules),
__is_initialized: bool, __is_initialized: bool,
__state: void, __state: void,
pub const IsInjectedArgument = void; pub const IsInjectedArgument = void;
pub inline fn read(comptime component_name: ComponentNameM(M)) Entities(modules).ComponentQuery { pub inline fn read(comptime component_name: ComponentNameM(M)) Database(modules).ComponentQuery {
return .{ .read = .{ return .{ .read = .{
.module = M.name, .module = M.name,
.component = comptime stringToEnum(ComponentName(modules), @tagName(component_name)).?, .component = comptime stringToEnum(ComponentName(modules), @tagName(component_name)).?,
@ -634,7 +634,7 @@ pub fn ModSet(comptime modules: anytype) type {
/// ///
/// Whether you use `Foo.Mod.read()` or `Foo.Mod.write()` determines whether the /// Whether you use `Foo.Mod.read()` or `Foo.Mod.write()` determines whether the
/// slice in each iterator value will be mutable or not. /// slice in each iterator value will be mutable or not.
pub inline fn query(m: *@This(), comptime q: anytype) !Entities(modules).QueryResult(q) { pub inline fn query(m: *@This(), comptime q: anytype) !Database(modules).QueryResult(q) {
return m.__entities.query(q); return m.__entities.query(q);
} }
}; };
@ -642,20 +642,20 @@ pub fn ModSet(comptime modules: anytype) type {
return struct { return struct {
/// Private/internal fields /// Private/internal fields
__entities: *Entities(modules), __entities: *Database(modules),
__is_initialized: bool, __is_initialized: bool,
__state: M, __state: M,
pub const IsInjectedArgument = void; pub const IsInjectedArgument = void;
pub inline fn read(comptime component_name: ComponentNameM(M)) Entities(modules).ComponentQuery { pub inline fn read(comptime component_name: ComponentNameM(M)) Database(modules).ComponentQuery {
return .{ .read = .{ return .{ .read = .{
.module = M.name, .module = M.name,
.component = comptime stringToEnum(ComponentName(modules), @tagName(component_name)).?, .component = comptime stringToEnum(ComponentName(modules), @tagName(component_name)).?,
} }; } };
} }
pub inline fn write(comptime component_name: ComponentNameM(M)) Entities(modules).ComponentQuery { pub inline fn write(comptime component_name: ComponentNameM(M)) Database(modules).ComponentQuery {
return .{ .write = .{ return .{ .write = .{
.module = M.name, .module = M.name,
.component = comptime stringToEnum(ComponentName(modules), @tagName(component_name)).?, .component = comptime stringToEnum(ComponentName(modules), @tagName(component_name)).?,