205 lines
6.2 KiB
TypeScript
205 lines
6.2 KiB
TypeScript
/**
|
|
* world.ts — the top-level simulation container.
|
|
*
|
|
* Owns entities, systems, time state, and the optional camera. Provides
|
|
* safe add/remove during iteration via deferred queues flushed at the end
|
|
* of each update.
|
|
*
|
|
* The update loop:
|
|
* 1. Compute scaled dt (respects pause and timeScale).
|
|
* 2. Update camera.
|
|
* 3. Run each non-render system in order.
|
|
* 4. Flush pending adds and removes.
|
|
*
|
|
* The render loop:
|
|
* 1. Run each renderOnly system in order with the last scaled dt.
|
|
*
|
|
* Additions and removals are queued, not applied immediately. A system
|
|
* that adds an entity during update will not see it until the next frame.
|
|
*/
|
|
|
|
import { Camera, type CameraConfig } from "./camera.ts";
|
|
import { createEntity, type Entity, type EntityOptions, type SlotKey } from "./entity.ts";
|
|
import type { System } from "./system.ts";
|
|
|
|
export interface WorldConfig {
|
|
width: number;
|
|
height: number;
|
|
/** Clamp on raw frame dt, seconds. Default 1/20. */
|
|
maxDelta?: number;
|
|
camera?: CameraConfig;
|
|
}
|
|
|
|
export interface WorldStats {
|
|
frameCount: number;
|
|
lastRawDt: number;
|
|
lastScaledDt: number;
|
|
}
|
|
|
|
export class World {
|
|
width: number;
|
|
height: number;
|
|
timeScale = 1;
|
|
paused = false;
|
|
/** Scaled seconds elapsed since creation. */
|
|
time = 0;
|
|
|
|
readonly camera: Camera;
|
|
readonly maxDelta: number;
|
|
readonly stats: WorldStats = {
|
|
frameCount: 0,
|
|
lastRawDt: 0,
|
|
lastScaledDt: 0,
|
|
};
|
|
|
|
private readonly _entities: Entity[] = [];
|
|
private readonly _systems: System[] = [];
|
|
private readonly _pendingAdds: Entity[] = [];
|
|
private readonly _pendingRemoves: Entity[] = [];
|
|
private lastScaledDt = 0;
|
|
|
|
constructor(cfg: WorldConfig) {
|
|
this.width = cfg.width;
|
|
this.height = cfg.height;
|
|
this.maxDelta = cfg.maxDelta ?? 1 / 20;
|
|
this.camera = new Camera(cfg.camera);
|
|
}
|
|
|
|
/* -------------------------------------------------------------- */
|
|
/* Accessors */
|
|
/* -------------------------------------------------------------- */
|
|
|
|
get entities(): readonly Entity[] {
|
|
return this._entities;
|
|
}
|
|
|
|
get systems(): readonly System[] {
|
|
return this._systems;
|
|
}
|
|
|
|
/** Entities with no parent. Renderers iterate these and recurse. */
|
|
get roots(): readonly Entity[] {
|
|
const result: Entity[] = [];
|
|
for (const e of this._entities) {
|
|
if (!e.destroyed && e.parent === null) result.push(e);
|
|
}
|
|
return result;
|
|
}
|
|
|
|
/* -------------------------------------------------------------- */
|
|
/* Entities */
|
|
/* -------------------------------------------------------------- */
|
|
|
|
/** Create an entity and queue it for addition. */
|
|
createEntity(opts: EntityOptions = {}): Entity {
|
|
const e = createEntity(opts);
|
|
this._pendingAdds.push(e);
|
|
return e;
|
|
}
|
|
|
|
/** Queue an already-created entity for addition. */
|
|
addEntity(entity: Entity): Entity {
|
|
this._pendingAdds.push(entity);
|
|
return entity;
|
|
}
|
|
|
|
/**
|
|
* Mark an entity as destroyed and queue it for removal.
|
|
* Cascades to children. Idempotent.
|
|
*/
|
|
removeEntity(entity: Entity): void {
|
|
if (entity.destroyed) return;
|
|
entity.destroyed = true;
|
|
|
|
// Detach from parent
|
|
if (entity.parent !== null) {
|
|
const i = entity.parent.children.indexOf(entity);
|
|
if (i >= 0) entity.parent.children.splice(i, 1);
|
|
entity.parent = null;
|
|
}
|
|
|
|
// Cascade to children (copy the array first — removeEntity mutates it)
|
|
for (const child of [...entity.children]) {
|
|
this.removeEntity(child);
|
|
}
|
|
|
|
this._pendingRemoves.push(entity);
|
|
}
|
|
|
|
/** All live entities that have the given slot populated. */
|
|
entitiesWith<K extends SlotKey>(slot: K): Entity[] {
|
|
const result: Entity[] = [];
|
|
for (const e of this._entities) {
|
|
if (e.destroyed) continue;
|
|
if (e[slot] !== undefined) result.push(e);
|
|
}
|
|
return result;
|
|
}
|
|
|
|
/* -------------------------------------------------------------- */
|
|
/* Systems */
|
|
/* -------------------------------------------------------------- */
|
|
|
|
addSystem(system: System): System {
|
|
this._systems.push(system);
|
|
this._systems.sort((a, b) => (a.order ?? 0) - (b.order ?? 0));
|
|
system.onAdd?.(this);
|
|
return system;
|
|
}
|
|
|
|
removeSystem(system: System): void {
|
|
const i = this._systems.indexOf(system);
|
|
if (i < 0) return;
|
|
this._systems.splice(i, 1);
|
|
system.onRemove?.(this);
|
|
}
|
|
|
|
/* -------------------------------------------------------------- */
|
|
/* Main loop */
|
|
/* -------------------------------------------------------------- */
|
|
|
|
update(rawDt: number): void {
|
|
this.stats.frameCount++;
|
|
this.stats.lastRawDt = rawDt;
|
|
|
|
const clamped = Math.min(rawDt, this.maxDelta);
|
|
const scaledDt = this.paused ? 0 : clamped * this.timeScale;
|
|
|
|
this.stats.lastScaledDt = scaledDt;
|
|
this.lastScaledDt = scaledDt;
|
|
this.time += scaledDt;
|
|
|
|
this.camera.update(scaledDt);
|
|
|
|
for (const sys of this._systems) {
|
|
if (sys.renderOnly === true) continue;
|
|
sys.update(this, scaledDt);
|
|
}
|
|
|
|
this.flush();
|
|
}
|
|
|
|
render(): void {
|
|
for (const sys of this._systems) {
|
|
if (sys.renderOnly !== true) continue;
|
|
sys.update(this, this.lastScaledDt);
|
|
}
|
|
}
|
|
|
|
/* -------------------------------------------------------------- */
|
|
|
|
private flush(): void {
|
|
if (this._pendingAdds.length > 0) {
|
|
for (const e of this._pendingAdds) this._entities.push(e);
|
|
this._pendingAdds.length = 0;
|
|
}
|
|
if (this._pendingRemoves.length > 0) {
|
|
for (const e of this._pendingRemoves) {
|
|
const i = this._entities.indexOf(e);
|
|
if (i >= 0) this._entities.splice(i, 1);
|
|
}
|
|
this._pendingRemoves.length = 0;
|
|
}
|
|
}
|
|
}
|