tarinai/js/tarinai_action_state.js
2026-06-23 11:27:55 +09:00

91 lines
4.2 KiB
JavaScript

"use strict";
(function (global) {
const Tarinai = global.Tarinai;
if (!Tarinai) throw new Error("Tarinai is not available for mixin: tarinai_action_state.js");
Object.defineProperties(Tarinai.prototype, Object.getOwnPropertyDescriptors({
setActionState(state, opts = {}) {
if (this.dead) return false;
this.state = state || "idle";
if ("target" in opts) this.target = opts.target;
if (opts.reason != null) this.thought = String(opts.reason || "");
if (opts.clearTarget) this.target = null;
if (opts.sleeping != null) this.sleeping = Boolean(opts.sleeping);
if (opts.wake) {
this.sleeping = false;
if (this.insideNestBoxId && this.leaveNestBox) this.leaveNestBox(this.world?.nestBoxById?.(this.insideNestBoxId));
this.insideNestBoxId = "";
}
return true;
},
beginEating(item = null, reason = "") {
return this.setActionState("eat", {
target: item || null,
reason: reason || (item?.type && typeof toolLabel === "function" ? `${toolLabel(item.type)}を食べている` : "食べている"),
sleeping: false,
});
},
currentLodgedPin() {
return typeof lodgedPinFor === "function" ? lodgedPinFor(this, this.world) : null;
},
currentLodgedPinBehavior() {
const pin = this.currentLodgedPin ? this.currentLodgedPin() : null;
return typeof pinBehaviorFor === "function" ? pinBehaviorFor(pin?.type) : null;
},
hasLodgedPinEffect(effectKey = "") {
return Boolean(this.currentLodgedPinBehavior?.()?.[effectKey]);
},
goIdle(reason = "") {
return this.setActionState("idle", { target: null, reason: reason || this.thought || "待っている", sleeping: false });
},
startSleeping(target = null, reason = "") {
return this.setActionState("sleep", { target, reason: reason || (target?.type === "nest_box" ? "巣箱の中で眠っている" : "眠っている"), sleeping: true });
},
seekTarget(state, target = null, reason = "") {
return this.setActionState(state, { target, reason, sleeping: false });
},
enterPanic(opts = {}) {
if (this.dead) return false;
const profile = this.personalityProfile ? this.personalityProfile() : { fear: 1 };
const fearMul = opts.fearMultiplier ?? profile.fear ?? 1;
const threat = opts.threat ?? opts.target ?? null;
let target = null;
if ("target" in opts) target = opts.target;
else if (opts.destination) target = opts.destination;
else if (this.panicDestination) target = this.panicDestination(threat, !!opts.forceDestination);
this.setActionState("panic", {
target,
reason: opts.reason || this.thought || "パニックになっている",
wake: opts.wake !== false,
sleeping: false,
});
if (Number.isFinite(opts.fearTimer)) this.fearTimer = Math.max(this.fearTimer || 0, opts.fearTimer * fearMul);
else this.fearTimer = Math.max(this.fearTimer || 0, (opts.fear ?? 0.55) * fearMul);
if (Number.isFinite(opts.surpriseTimer)) this.surpriseTimer = Math.max(this.surpriseTimer || 0, opts.surpriseTimer);
if (Number.isFinite(opts.hurtTimer)) this.hurtTimer = Math.max(this.hurtTimer || 0, opts.hurtTimer);
if (Number.isFinite(opts.awakeLockTimer)) this.awakeLockTimer = Math.max(this.awakeLockTimer || 0, opts.awakeLockTimer);
this.panicTimer = Math.max(this.panicTimer || 0, Number.isFinite(opts.duration) ? opts.duration : 3.0);
if (Number.isFinite(opts.fallTimer)) {
this.fallTimer = Math.max(this.fallTimer || 0, opts.fallTimer);
this.fallMax = Math.max(this.fallMax || 0, this.fallTimer);
}
if (Number.isFinite(opts.stress) && opts.stress > 0) {
if (this.addStress) this.addStress(opts.stress, { threshold: opts.stressThreshold ?? 8, duration: opts.stressDuration ?? 4.2 });
else this.stress = clamp((this.stress || 0) + opts.stress, 0, 130);
}
if (opts.bubble) this.bubble?.(opts.bubble, opts.bubbleCooldown ?? 0.9, opts.bubbleColor || "rgba(84,66,86,0.80)");
this.lastPanicCause = opts.cause || opts.reason || this.thought || "panic";
this.lastPanicAt = this.world?.time || 0;
return true;
},
}));
})(typeof window !== "undefined" ? window : globalThis);