import { PixiSceneManager } from './pixi-scene-manager'; import { GameConfig, GameDirector, GameRouter, GameScene, GameTransitionOpts, } from '@gedit/render-engine'; import { PixiGameScene, PixiGameWorld } from '../common'; import { getBackgroundConfig, fixPIXIBug, createVideoView, isAndroid, reMarsPlayer, } from './utils'; interface AssetsMap { [key: string]: PIXI.Texture | PIXI.sound.Sound | PIXI.VideoSource | null; } export interface LoadEventProps { onStart: (loader: PIXI.Loader) => void; // onError 无效; onError: (loader: PIXI.Loader, resource: AssetsMap) => void; onProgress: ( loader: PIXI.Loader & { progress: number }, progress: number ) => void; onComplete: (loader: PIXI.Loader, resource: AssetsMap) => void; } export class PixiGameWorldImpl implements PixiGameWorld { game: PIXI.Application; readonly sceneManager: PixiSceneManager; readonly loaderAssetsKey = new Set(); resources: { [key: string]: PIXI.Texture | PIXI.sound.Sound } = {}; loadingEvent: { onStart: LoadEventProps['onStart'][]; onError: LoadEventProps['onError'][]; onProgress: LoadEventProps['onProgress'][]; onComplete: LoadEventProps['onComplete'][]; } = { onStart: [], onError: [], onProgress: [], onComplete: [], }; videoView: HTMLDivElement | undefined; constructor(elementId: string, readonly config: GameConfig) { // PIXI.settings.PREFER_ENV = window.WebGL2RenderingContext ? 2 : 1; // delete PIXI.Renderer.__plugins.interaction; // PIXI.settings.ROUND_PIXELS = config.textPixel; fixPIXIBug(); reMarsPlayer(); // fix pixi video 在安卓支付宝下卡顿问题,需要把 video 放到 body 下; if (isAndroid) { this.videoView = createVideoView(); document.getElementById(elementId)?.appendChild(this.videoView); } this.game = new PIXI.Application(); this.init(elementId); /* if (!('events' in this.game.renderer)) { this.game.renderer.addSystem(PIXI.EventSystem as any, 'events'); } */ this.sceneManager = new PixiSceneManager(this); } async init(elementId: string): Promise { const { config } = this; const dpr = config.autoDPR ? window.devicePixelRatio : config.dpr || 2; // PIXI.settings.PREFER_ENV = window.WebGL2RenderingContext ? 2 : 1; const bg = getBackgroundConfig(config.backgroundColor); // ios 15.4 safari bug 遮罩下不支持平滑处理;关闭平滑 // https://github.com/pixijs/pixijs/issues/8183 const isSafari154 = navigator.userAgent.includes('Safari') && navigator.userAgent.includes('Version/15.4'); await this.game.init({ width: window.innerWidth, height: window.innerHeight, backgroundColor: bg.backgroundColor, backgroundAlpha: bg.backgroundAlpha ?? 1, antialias: isSafari154 ? false : !config.pixelArt, autoDensity: true, resolution: dpr, // transparent: bg.backgroundAlpha < 1, resizeTo: window.document.body, useBackBuffer: true, }); const parentNode = document.getElementById(elementId)!; parentNode.appendChild(this.game.renderer.canvas); } get stage(): PIXI.Container { return this.game.stage; } getSceneById(sceneId: string): PixiGameScene | undefined { return this.sceneManager.getSceneById(sceneId); } clearScene(): void { this.sceneManager.clearScene(); } clearResourceKeyCache(resourceKey?: string | undefined): void { if (!PIXI.Assets.loader) return; if (resourceKey) { PIXI.Assets.loader.unload(resourceKey); delete this.resources[resourceKey]; } else { PIXI.Assets.unload(Object.keys(this.resources)); this.resources = {}; } } } export class PixiGameDirectorImpl extends GameRouter implements GameDirector { constructor(protected world: PixiGameWorldImpl) { super(); } start(startSceneId: string): void { const scene = this.world.sceneManager.getSceneById(startSceneId); // 第一个场景进入时附值; if (!this.world.sceneManager.currentScene) { this.world.sceneManager.currentScene = scene; } scene?.load(); } goto( fromScene: GameScene, targetSceneId: string, opts?: GameTransitionOpts ): void { // sceneManage 的 goto 不负责加载,预先加载; this.start(targetSceneId); this.world.sceneManager.goto(targetSceneId, opts); } script(scriptId: string): void {} }