tarinai/js/system_order.js

45 lines
2.3 KiB
JavaScript

"use strict";
// Layer: simulation/system-order
// Names the update order in one place. Prefer concrete simulation systems,
// with the phase facade kept as a stable named entry point.
(function (global) {
function phase(name) {
return global.TarinaiSimulationSystems.phases?.[name] || null;
}
const SYSTEM_ORDER = Object.freeze([
{ id: "clock", run(worldRef, ctx) { phase("updateClock")?.(worldRef, ctx.dt); } },
{ id: "frame_signals", run(worldRef, ctx) { phase("updateFrameSignals")?.(worldRef, ctx.dt); } },
{ id: "environment", run(worldRef, ctx) { phase("updateEnvironment")?.(worldRef, ctx.dt); } },
{ id: "spatial_prepare", run(worldRef) { phase("prepareSpatialFrame")?.(worldRef); } },
{ id: "items_and_ants", run(worldRef, ctx) { ctx.mobile = phase("updateItemsAndAnts")?.(worldRef, ctx.dt) || null; } },
{ id: "effects", run(worldRef, ctx) { phase("updateEffects")?.(worldRef, ctx.dt); } },
{ id: "creatures", run(worldRef, ctx) { phase("updateCreatures")?.(worldRef, ctx.dt); } },
{ id: "maintenance", run(worldRef, ctx) { phase("runMaintenance")?.(worldRef, ctx.dt, ctx.mobile?.itemCountBefore ?? (worldRef.items || []).length); } },
{ id: "phase_events", run(worldRef) { phase("emitPhaseChange")?.(worldRef); } },
{ id: "ambient_tarinai", run(worldRef, ctx) { phase("spawnAmbientTarinai")?.(worldRef, ctx.dt); } },
{ id: "ambient_grass", run(worldRef, ctx) { phase("spawnAmbientGrass")?.(worldRef, ctx.dt); } },
{ id: "rain_water", run(worldRef, ctx) { phase("spawnRainWater")?.(worldRef, ctx.dt); } },
{ id: "diagnostics", run(worldRef) { phase("finalizeDiagnostics")?.(worldRef); } },
]);
function update(worldRef, dt) {
const normalize = phase("normalizeDelta");
if (!normalize) throw new Error("Tarinai simulation normalize phase is not available");
const frameDt = normalize(worldRef, dt);
if (frameDt === null) return;
const ctx = { dt: frameDt, mobile: null };
const profiler = global.TarinaiPerf;
for (const system of SYSTEM_ORDER) {
const end = profiler.begin(`update.phase.${system.id}`) || null;
try {
system.run(worldRef, ctx);
} finally {
if (end) end();
}
}
}
global.TarinaiSystemOrder = Object.freeze({ update });
})(typeof window !== "undefined" ? window : globalThis);