// 入口:读档(含迁移 / 离线结算)→ 启动 UI。署名:FmodeCode import * as C from './config.js'; import * as S from './state.js'; import { App } from './ui.js'; function boot() { let loaded = null; try { loaded = S.save.load(localStorage); } catch (e) { loaded = null; } let state = loaded ? loaded.state : S.newState(); // 极早期存档 / 缺省字段补救:保证 UI 依赖的字段存在 if (state && typeof state === 'object') { const d = S.newState(); for (const k of Object.keys(d)) if (state[k] === undefined) state[k] = d[k]; for (const k of Object.keys(d.upgrades)) if (state.upgrades[k] === undefined) state.upgrades[k] = 0; if (!Array.isArray(state.planes) || state.planes.length < 4) state.planes = d.planes; if (!state.stats) state.stats = d.stats; if (!state.flags) state.flags = {}; if (!state.round) state.round = { refines: 0 }; if (!state.crisis) state.crisis = d.crisis; if (!state.magic) state.magic = d.magic; } let rift = loaded ? loaded.rift : null; // 里世界对象需带 active 标记以冻结主世界 if (rift && rift.run && rift.run.state !== 'result') { rift.run.active = true; } else rift = null; let offline = null; try { if (loaded && !(rift && rift.run)) { const gap = (Date.now() - loaded.savedAt) / 1000; offline = S.applyOffline(state, gap, null); } } catch (e) { offline = null; } const app = new App(state, rift); window.__wt = app; // 便于调试与自动化验收(只读) app.renderAll(); app._lastTickAt = Date.now(); if (offline && !offline.skipped && !(rift && rift.run && rift.run.state !== 'result')) app.pushDialog({ kind: 'offline', data: offline }); if (rift && rift.run && rift.run.state !== 'result') app.toast('你离开时正处于里世界:主世界时间已冻结,局内进度已保存。', 'warn'); app.log('世界树已苏醒:空洞的侵蚀仍在继续。', 'mile'); S.save.write(localStorage, state, rift); // 页面重新可见 / 获得焦点时补算离线 document.addEventListener('visibilitychange', () => { if (!document.hidden) { app._lastTickAt = app._lastTickAt || Date.now(); } }); window.addEventListener('focus', () => { const now = Date.now(); const gap = (now - (app._lastTickAt || now)) / 1000; app._lastTickAt = now; if (gap > 60 && !state.escaped && !(rift && rift.run)) { const o = S.applyOffline(state, gap, null); if (o && !o.skipped) { app.pushDialog({ kind: 'offline', data: o }); app.renderAll(); } } }); } if (document.readyState === 'loading') document.addEventListener('DOMContentLoaded', boot); else boot();