import Phaser from "phaser"; import { BootScene } from "./scenes/BootScene"; import { PreloaderScene } from "./scenes/PreloaderScene"; import { GameScene } from "./scenes/GameScene"; /** * 创建并启动 Phaser 游戏实例。 * * 设计分辨率 = viewport × 2(HiDPI 适配),Scale.FIT 自动铺满容器。 * 场景按 Boot → Preloader → Game 顺序启动。 */ export const StartGame = (parent: string): Phaser.Game => { const viewportWidth = window.innerWidth || 375; const viewportHeight = window.innerHeight || 667; const DESIGN_SCALE = 2; const designWidth = Math.round(viewportWidth * DESIGN_SCALE); const designHeight = Math.round(viewportHeight * DESIGN_SCALE); const config: Phaser.Types.Core.GameConfig = { type: Phaser.WEBGL, parent, width: designWidth, height: designHeight, backgroundColor: 0xffffff, scene: [BootScene, PreloaderScene, GameScene], scale: { mode: Phaser.Scale.FIT, autoCenter: Phaser.Scale.CENTER_BOTH, }, input: { activePointers: 2, }, }; const game = new Phaser.Game(config); if (typeof __DEV__ !== "undefined" && __DEV__) { (window as any).__PHASER_GAME__ = game; (window as any).PhaserRef = Phaser; console.log("[Game] Started", { viewport: { w: viewportWidth, h: viewportHeight, dpr: window.devicePixelRatio }, design: { w: designWidth, h: designHeight }, }); } return game; };