tarinai/js/main.js

160 lines
5.4 KiB
JavaScript
Raw Normal View History

2026-06-18 18:59:56 +09:00
"use strict";
2026-06-25 17:43:24 +09:00
function ensureWorldBootstrap() {
let currentWorld = globalThis.world;
if (currentWorld?.update) return currentWorld;
try {
currentWorld = new World();
if (typeof window !== "undefined") window.world = currentWorld;
if (typeof globalThis !== "undefined") globalThis.world = currentWorld;
return currentWorld;
} catch (error) {
throw error;
}
}
let world = ensureWorldBootstrap();
2026-06-18 18:59:56 +09:00
let last = nowSec();
let statsTimer = 0;
2026-07-18 13:06:02 +09:00
let statsRefreshPending = false;
2026-06-18 18:59:56 +09:00
window.__tarinaiFps = 0;
2026-06-20 23:27:39 +09:00
2026-07-18 13:06:02 +09:00
function scheduleStatsRefresh() {
if (statsRefreshPending) return;
if (typeof colonyStatsUiVisible === "function" && !colonyStatsUiVisible()) return;
statsRefreshPending = true;
const run = () => {
statsRefreshPending = false;
if (!document.hidden) renderStats();
};
if (typeof window.requestIdleCallback === "function") window.requestIdleCallback(run, { timeout: 900 });
else window.setTimeout(run, 0);
}
2026-06-29 16:21:58 +09:00
const LOADING_SPRITES = SPRITES
.filter(asset => asset && !asset.actionOnly)
.slice(0, 11)
.map(asset => asset.path);
2026-06-21 14:09:35 +09:00
let loadingSpriteTimer = 0;
function shuffledLoadingSprites() {
const list = [...LOADING_SPRITES];
for (let i = list.length - 1; i > 0; i--) {
const j = Math.floor(Math.random() * (i + 1));
[list[i], list[j]] = [list[j], list[i]];
}
return list.slice(0, 4);
}
function renderLoadingSprites() {
const strip = document.getElementById("loadingSpriteStrip");
if (!strip) return;
strip.innerHTML = "";
const frag = document.createDocumentFragment();
for (const src of shuffledLoadingSprites()) {
const card = document.createElement("div");
card.className = "loading-sprite-card";
const img = document.createElement("img");
img.src = src;
img.alt = "";
img.loading = "eager";
card.appendChild(img);
frag.appendChild(card);
}
strip.appendChild(frag);
}
function initLoadingShowcase() {
renderLoadingSprites();
if (loadingSpriteTimer) clearInterval(loadingSpriteTimer);
loadingSpriteTimer = window.setInterval(renderLoadingSprites, 1200);
}
2026-06-20 23:27:39 +09:00
function setLoadingProgress(text = "", ratio = 0) {
const screen = document.getElementById("loadingScreen");
if (!screen) return;
const pct = clamp(Number(ratio) || 0, 0, 1);
const label = document.getElementById("loadingText");
const bar = document.getElementById("loadingBar");
const percent = document.getElementById("loadingPercent");
if (label && text) label.textContent = text;
if (bar) bar.style.width = `${Math.round(pct * 100)}%`;
if (percent) percent.textContent = `${Math.round(pct * 100)}%`;
}
function hideLoadingScreen() {
2026-06-21 14:09:35 +09:00
if (loadingSpriteTimer) { clearInterval(loadingSpriteTimer); loadingSpriteTimer = 0; }
2026-07-10 16:40:06 +09:00
document.getElementById("loadingSpriteStrip")?.replaceChildren();
2026-06-20 23:27:39 +09:00
setLoadingProgress("\u8d77\u52d5\u5b8c\u4e86", 1);
const screen = document.getElementById("loadingScreen");
if (!screen) return;
window.setTimeout(() => screen.classList.add("hidden"), 120);
}
2026-06-18 18:59:56 +09:00
function loop() {
const t = nowSec();
const rawDt = t - last;
const dt = clamp(rawDt, 0, 0.05);
last = t;
2026-07-10 16:40:06 +09:00
window.TarinaiPerf.observeFrameDelta(rawDt);
2026-06-29 16:21:58 +09:00
window.TarinaiPerf.beginFrame();
const endUpdate = window.TarinaiPerf.begin("update.total");
2026-06-18 18:59:56 +09:00
world.update(dt);
2026-07-20 14:36:59 +09:00
if (typeof tickColonyHistorySampling === "function") tickColonyHistorySampling();
2026-06-25 14:51:58 +09:00
if (endUpdate) endUpdate();
2026-06-29 16:21:58 +09:00
const endRender = window.TarinaiPerf.begin("render.total");
2026-06-18 18:59:56 +09:00
render();
2026-06-25 14:51:58 +09:00
if (endRender) endRender();
2026-07-01 22:59:49 +09:00
window.TarinaiPerf.endFrame();
2026-06-29 16:21:58 +09:00
if (window.TarinaiPerf.consumeResizeRequest() && typeof resizeCanvas === "function") resizeCanvas();
2026-06-18 18:59:56 +09:00
statsTimer += dt;
2026-06-24 21:20:53 +09:00
const statsInterval = 2.5;
2026-06-19 02:27:19 +09:00
if (statsTimer > statsInterval) {
2026-07-18 13:06:02 +09:00
scheduleStatsRefresh();
2026-06-18 18:59:56 +09:00
statsTimer = 0;
}
requestAnimationFrame(loop);
}
2026-06-21 14:09:35 +09:00
initLoadingShowcase();
2026-06-20 23:27:39 +09:00
setLoadingProgress("\u521d\u671f\u753b\u50cf\u3092\u8aad\u307f\u8fbc\u307f\u4e2d", 0.08);
2026-06-29 16:21:58 +09:00
loadImages((done, total) => {
2026-06-20 23:27:39 +09:00
const p = total > 0 ? done / total : 1;
setLoadingProgress(`\u521d\u671f\u753b\u50cf\u3092\u8aad\u307f\u8fbc\u307f\u4e2d ${done}/${total}`, 0.08 + p * 0.46);
}).then(() => {
setLoadingProgress("UI\u3092\u6e96\u5099\u4e2d", 0.62);
2026-06-18 18:59:56 +09:00
bindUI();
2026-06-20 23:27:39 +09:00
setLoadingProgress("\u30d5\u30a3\u30fc\u30eb\u30c9\u3092\u69cb\u7bc9\u4e2d", 0.72);
2026-06-19 14:00:48 +09:00
applyFieldLayout("garden");
world.reset(null, "garden");
2026-07-05 18:01:36 +09:00
window.TarinaiGroundUI?.bind?.(world);
2026-06-18 18:59:56 +09:00
selectTool("observe");
2026-06-20 23:27:39 +09:00
setLoadingProgress("\u8868\u793a\u3092\u521d\u671f\u5316\u4e2d", 0.84);
2026-06-18 18:59:56 +09:00
renderLog(world.logs);
renderArchive();
render();
renderStats();
syncTopButtons();
2026-06-29 16:21:58 +09:00
startBackgroundImageLoading();
2026-06-20 23:27:39 +09:00
setLoadingProgress("\u30b7\u30df\u30e5\u30ec\u30fc\u30b7\u30e7\u30f3\u3092\u958b\u59cb\u4e2d", 0.94);
2026-06-18 18:59:56 +09:00
loop();
2026-06-20 23:27:39 +09:00
requestAnimationFrame(hideLoadingScreen);
2026-06-18 18:59:56 +09:00
});
2026-06-21 14:09:35 +09:00
if ("serviceWorker" in navigator) {
window.addEventListener("load", () => {
2026-07-01 14:41:05 +09:00
const swUrl = window.TARINAI_APP?.withVersion?.("./service-worker.js") || "./service-worker.js";
2026-06-21 14:09:35 +09:00
navigator.serviceWorker.register(swUrl, { updateViaCache: "none" }).catch(() => undefined);
});
}
2026-07-20 14:36:59 +09:00
// Expose the active build on the root element so stale-cache reports can be verified immediately.
(function markActiveBuild() {
const apply = () => {
if (!document?.documentElement) return;
document.documentElement.dataset.tarinaiBuild = String(window.TARINAI_APP?.build || window.TARINAI_VERSION || "unknown");
};
if (document.readyState === "loading") document.addEventListener("DOMContentLoaded", apply, { once: true });
else apply();
})();