tarinai/js/ui_ground.js

57 lines
2.2 KiB
JavaScript
Raw Normal View History

2026-06-25 14:51:58 +09:00
"use strict";
(function (global) {
function groundDef(worldRef = global.world) {
const id = worldRef?.groundType || "soil";
return global.TarinaiGround?.definition?.(id) || (typeof GROUND_TYPES !== "undefined" ? GROUND_TYPES[id] || GROUND_TYPES.soil : { id: "soil", label: "土", description: "いつものじめん" });
}
function groundTip(worldRef = global.world) {
const def = groundDef(worldRef);
if (global.TarinaiGround?.tooltipText) return global.TarinaiGround.tooltipText(def.id);
return `${def.label || "じめん"}\n${def.description || "クリックで切り替え"}`;
}
function updateGroundUI(worldRef = global.world) {
const uiRef = global.ui || {};
const def = groundDef(worldRef);
const label = def.label || "土";
if (uiRef.statGroundType) setTextIfChanged(uiRef.statGroundType, "groundType", label);
if (uiRef.groundTypeBtn) {
const desc = def.description || "クリックで切り替え";
uiRef.groundTypeBtn.dataset.tip = desc;
uiRef.groundTypeBtn.removeAttribute("title");
uiRef.groundTypeBtn.setAttribute("aria-label", desc ? `地面を切り替える。現在は${label}${desc}` : `地面を切り替える。現在は${label}`);
}
}
function bindGroundUI(worldRef = global.world) {
const btn = global.ui?.groundTypeBtn;
if (!btn) return;
if (btn.dataset.groundBound !== "1") {
btn.dataset.groundBound = "1";
btn.addEventListener("click", (event) => {
event.preventDefault();
event.stopPropagation();
const currentWorld = global.world || worldRef;
audio.uiClick?.();
currentWorld?.cycleGroundType?.();
if (global.uiCache) {
global.uiCache.lastStatsGroundType = "";
if (global.uiCache.stats) global.uiCache.stats.groundType = "";
}
updateGroundUI(currentWorld);
global.renderStats?.();
});
}
global.TarinaiTooltips?.bind?.(btn, () => groundTip(worldRef));
updateGroundUI(worldRef);
}
global.TarinaiGroundUI = Object.freeze({
bind: bindGroundUI,
update: updateGroundUI,
tip: groundTip,
});
global.bindGroundUI = bindGroundUI;
})(window);