orbit/js/main.js

78 lines
2.8 KiB
JavaScript

import Phaser from './vendor/phaser.js';
import { config } from './config/Config.js';
import { ConfigLoader } from './config/ConfigLoader.js';
import { createGameConfig } from './config/GameConfig.js';
import { MenuScene } from './scenes/MenuScene.js';
import { GameScene } from './scenes/GameScene.js';
import { SurfaceScene } from './scenes/SurfaceScene.js';
import { installBuildDiag } from './dev/BuildDiag.js';
import { installNavDiag } from './dev/NavDiag.js';
/**
* Orbit — entry point.
*
* 1. Load config JSON (data/manifest.json → data/*.json)
* 2. Initialize the global config singleton
* 3. Build the Phaser game config and attach the scenes
* 4. Boot
*/
/**
* Wait for the web fonts (data/theme.json) to be ready before the first
* draw, so canvas text uses Ethnocentric/Centauri from frame one.
* Hard timeout so boot can never hang on a flaky asset — we just fall
* back to the stack's system fonts in that case.
*/
async function awaitFonts(timeoutMs = 2000) {
const fonts = globalThis.document?.fonts;
if (!fonts || typeof fonts.load !== 'function') return;
const families = ['header', 'body']
.map((k) => config.get(`theme.fonts.${k}.family`))
.filter((f) => typeof f === 'string' && f.length > 0);
if (families.length === 0) return;
const jobs = families.map((fam) => fonts.load(`16px "${fam}"`).catch(() => {}));
const timeout = new Promise((resolve) => setTimeout(resolve, timeoutMs));
await Promise.race([Promise.allSettled(jobs), timeout]);
}
async function boot() {
const data = await ConfigLoader.load();
config.init(data);
console.info('orbit — config loaded', Object.keys(data).join(', '));
// Read-only console diagnostics (orbitDiag / orbitDiagBrief). See
// js/dev/BuildDiag.js — it also self-logs a one-line report whenever
// the Build console opens.
installBuildDiag();
// The SYSTEM category's NAV chart (orbitNav / orbitNavBrief). See
// js/dev/NavDiag.js — which NAV points of the current system are still
// undiscovered, i.e. why the jumpgate tech may still be locked.
installNavDiag();
await awaitFonts();
console.info('orbit — fonts ready (or timed out)');
const gameConfig = createGameConfig();
gameConfig.scene = [MenuScene, GameScene, SurfaceScene];
const game = new Phaser.Game(gameConfig);
// Handy for console debugging.
window.game = game;
window.config = config;
console.info(
`%c${config.get('game.name', 'Orbit')} %c${config.get('game.version', '')}`,
'font-weight:bold;font-size:14px',
'color:#8fa0c9',
);
}
boot().catch((err) => {
console.error(err);
const pre = document.createElement('pre');
pre.style.cssText =
'color:#ff9b9b;font:14px/1.6 monospace;padding:2rem;white-space:pre-wrap;margin:0';
pre.textContent = `Failed to start Orbit:\n${(err && (err.stack || err.message)) || err}`;
document.body.appendChild(pre);
});