tarinai/js/tarinai_item_interaction_system.js
2026-06-26 19:04:32 +09:00

39 lines
1.3 KiB
JavaScript

"use strict";
// Tarinai item interaction system.
// Orchestrates food/medicine consumption and non-eating item contact effects.
// The candidate loop intentionally preserves the established order: for each nearby
// candidate, try eating first; if nothing was consumed, apply contact effects
// for that same candidate before advancing to the next one.
(function (global) {
function interactWithItems(dt = 0) {
const ctx = global.TarinaiItemInteractionContext?.create?.(this, dt);
if (!ctx) return false;
for (const item of ctx.foodCandidates || []) {
const consumed = Boolean(global.TarinaiFoodInteractionSystem?.updateCandidate?.(this, ctx, item, dt));
if (consumed) {
global.TarinaiItemInteractionContext?.finalize?.(this);
return true;
}
global.TarinaiContactItemSystem?.updateCandidate?.(this, ctx, item, dt);
}
global.TarinaiItemInteractionContext?.finalize?.(this);
return true;
}
function updateOne(tarinai, dt = 0) {
if (!tarinai || tarinai.dead) return false;
return interactWithItems.call(tarinai, dt);
}
function updateWithThis(dt = 0) {
return updateOne(this, dt);
}
global.TarinaiItemInteractionSystem = Object.freeze({
interactWithItems,
updateOne,
updateWithThis,
});
})(typeof window !== "undefined" ? window : globalThis);