Files
heros_web/src/main.ts

95 lines
3.1 KiB
TypeScript

import Phaser from 'phaser';
import { ambienceTracks, effectTracks, musicTracks } from './game/audio/audioAssets';
import { soundDirector } from './game/audio/SoundDirector';
import { BootScene } from './game/scenes/BootScene';
import { startLazySceneFromGame } from './game/scenes/lazyScenes';
import { TitleScene } from './game/scenes/TitleScene';
import './styles/global.css';
declare global {
interface Window {
__HEROS_GAME__?: Phaser.Game;
__HEROS_DEBUG__?: HerosDebugApi;
}
}
type DebugBattleScene = Phaser.Scene & {
getDebugState?: () => unknown;
toggleDebugOverlay?: () => void;
debugForceBattleOutcome?: (outcome: 'victory' | 'defeat') => void;
};
type DebugCampScene = Phaser.Scene & {
getDebugState?: () => unknown;
};
type HerosDebugApi = {
game: Phaser.Game;
activeScenes: () => string[];
audio: () => ReturnType<typeof soundDirector.getDebugState>;
battle: () => unknown;
camp: () => unknown;
goToBattle: (battleId?: string) => Promise<void>;
goToCamp: () => Promise<void>;
forceBattleOutcome: (outcome: 'victory' | 'defeat') => void;
scene: (key: string) => Phaser.Scene | undefined;
toggleBattleOverlay: () => void;
};
const searchParams = new URLSearchParams(window.location.search);
const rendererType =
searchParams.get('renderer') === 'canvas'
? Phaser.CANVAS
: searchParams.get('renderer') === 'webgl'
? Phaser.WEBGL
: Phaser.AUTO;
const config: Phaser.Types.Core.GameConfig = {
type: rendererType,
parent: 'game',
width: 1920,
height: 1080,
backgroundColor: '#10131a',
scale: {
mode: Phaser.Scale.FIT,
autoCenter: Phaser.Scale.CENTER_BOTH
},
scene: [BootScene, TitleScene]
};
soundDirector.registerMusicTracks(musicTracks);
soundDirector.registerAmbienceTracks(ambienceTracks);
soundDirector.registerEffectTracks(effectTracks);
const game = new Phaser.Game(config);
const debugEnabled = import.meta.env.DEV || searchParams.has('debug');
if (debugEnabled) {
window.__HEROS_GAME__ = game;
window.__HEROS_DEBUG__ = createDebugApi(game);
console.info('[Heros Debug] Use window.__HEROS_DEBUG__.battle() or press F9 in battle.');
}
function createDebugApi(game: Phaser.Game): HerosDebugApi {
const scene = (key: string) => game.scene.getScene(key);
const battleScene = () => scene('BattleScene') as DebugBattleScene | undefined;
const campScene = () => scene('CampScene') as DebugCampScene | undefined;
return {
game,
activeScenes: () => game.scene.getScenes(true).map((activeScene) => activeScene.scene.key),
audio: () => soundDirector.getDebugState(),
battle: () => battleScene()?.getDebugState?.() ?? { active: false, reason: 'BattleScene is not active yet.' },
camp: () => campScene()?.getDebugState?.() ?? { active: false, reason: 'CampScene is not active yet.' },
goToBattle: (battleId) => startLazySceneFromGame(game, 'BattleScene', battleId ? { battleId } : undefined),
goToCamp: () => startLazySceneFromGame(game, 'CampScene'),
forceBattleOutcome: (outcome) => {
battleScene()?.debugForceBattleOutcome?.(outcome);
},
scene,
toggleBattleOverlay: () => {
battleScene()?.toggleDebugOverlay?.();
}
};
}