main.js 1.1 KB

1234567891011121314151617181920212223242526
  1. // 入口:读档 → 迁移 → 离线结算 → 启动单屏 UI。署名:FmodeCode
  2. import * as S from './game/state.js';
  3. import { Game } from './ui/game.js';
  4. function boot() {
  5. let state = null;
  6. try { state = S.save.load(localStorage); } catch (e) { state = null; }
  7. if (!state) state = S.newState();
  8. let offline = null;
  9. try {
  10. const gap = (Date.now() - (state.savedAt || Date.now())) / 1000;
  11. if (gap > 60 && state.story && state.story.prologue) offline = S.applyOffline(state, gap);
  12. } catch (e) { offline = null; }
  13. const game = new Game(state);
  14. window.__wt = game; // 供自动化测试读取(只读)
  15. game.start();
  16. if (offline) {
  17. const g = window.__wt;
  18. g.toast(`<b>根系在你离开时仍在生长</b><small>${S.formatGap ? S.formatGap(offline.sec) : Math.round(offline.sec / 60) + ' 分钟'} · 本源 +${Math.round(offline.res)}</small>`, 'green', 5000);
  19. g.handle(offline.events, 'offline');
  20. g.renderAll();
  21. }
  22. }
  23. if (document.readyState === 'loading') document.addEventListener('DOMContentLoaded', boot);
  24. else boot();