tarinai/js/item_lifecycle_pipeline.js

52 lines
1.9 KiB
JavaScript
Raw Normal View History

2026-06-26 12:46:32 +09:00
"use strict";
// Layer: entity-runtime/items/pipeline
// Owns the ordered item lifecycle pipeline. Runtime calls this before falling
// back to the monolithic legacy Item.prototype.update body. Type-specific item
// methods still live on Item.prototype for compatibility while dispatch is moved
// into explicit lifecycle steps.
(function (global) {
const CONCRETE_STEP_NAMES = Object.freeze([
"TarinaiItemFrameLifecycleStep",
"TarinaiItemDecayLifecycleStep",
"TarinaiItemDynamicLifecycleStep",
"TarinaiItemGrowthLifecycleStep",
]);
function concreteSteps() {
return CONCRETE_STEP_NAMES.map(name => global[name]).filter(step => step && typeof step.update === "function");
}
function canRunConcretePipeline() {
return concreteSteps().length === CONCRETE_STEP_NAMES.length;
}
function runConcrete(item, dt, worldRef, context) {
for (const step of concreteSteps()) {
const result = step.update(item, dt, worldRef, context) || {};
if (result.done) return result.ok !== false;
}
return true;
}
function runLegacy(item, dt, worldRef, context) {
const step = global.TarinaiItemLegacyLifecycleStep;
if (step && typeof step.update === "function") return step.update(item, dt, worldRef, context).ok !== false;
const legacyUpdate = context.legacyUpdate;
if (typeof legacyUpdate !== "function") return false;
legacyUpdate.call(item, dt, worldRef);
return true;
}
function updateOne(item, dt, worldRef, options = {}) {
if (!item || item.dead) return false;
const context = { legacyUpdate: options.legacyUpdate };
return canRunConcretePipeline() ? runConcrete(item, dt, worldRef, context) : runLegacy(item, dt, worldRef, context);
}
global.TarinaiItemLifecyclePipeline = Object.freeze({
stepNames: CONCRETE_STEP_NAMES,
canRunConcretePipeline,
updateOne,
});
})(typeof window !== "undefined" ? window : globalThis);