import { GameObjectBaseType, GameResource, GameObjectData, GameObjectBase, } from '@gedit/render-engine'; import { deepClone } from '@gedit/utils'; import { PixiGameScene } from '../../common'; import { setGameObjectBaseData, getVisible } from '../utils'; export interface MarsSchema { interactive: boolean; // 是否交互 fps?: number; // 帧率 pixelRatio?: number; // 像素比 // transparentBackground?: boolean; // 透明背景 speed?: number; // 速度 reverse?: boolean; // 反向播放 autoplay?: boolean; // 自动播放 } export interface MarsGameObjectData extends GameObjectData { [GameObjectBaseType.MARS]: MarsSchema; } const marsPlayerMap = new Map(); const marsElementMap = new Map(); export class MarsGameObject extends GameObjectBase< MarsGameObjectData, PixiGameScene > { static type = GameObjectBaseType.MARS; static needResource = true; rendererObject?: PIXI.Sprite; marsComposition?: ge.Composition; mars?: ge.Player; animationData: MarsData; marsCanvas: HTMLCanvasElement = document.createElement('canvas'); create(resource: GameResource): void { const animationData = (this.animationData = deepClone( this.scene.getTexture(resource.resourceKey) ) as MarsData); animationData.elementId = this.id; const n = marsElementMap.get(this.id) || 0; marsElementMap.set(this.id, n + 1); const { compositions = [], compositionId } = animationData || {}; const composition = compositions.find((c: any) => c.id === compositionId); const [w, h] = composition?.previewSize || (composition as any)?.meta?.previewSize || []; this.marsCanvas.width = w; this.marsCanvas.height = h; // document.body.appendChild(this.marsCanvas); // this.marsCanvas.style.position = 'absolute'; // this.marsCanvas.style.top = '0px'; // this.marsCanvas.style.left = '0px'; this.rendererObject = new PIXI.Sprite(PIXI.Texture.from(this.marsCanvas)); this.scene.container.addChild?.(this.rendererObject); this.createMars(); super.create.apply(this, arguments); } createMars( data: MarsGameObjectData = this.data, animationData: MarsData = this.animationData ): void { const { compositions = [], compositionId } = animationData || {}; const composition = compositions.find((c: any) => c.id === compositionId); if (!composition) { return; } const key = `mars_${this.isSnapshot ? '_isSnapshot' : ''}`; this.mars = marsPlayerMap.get(key); const [w, h] = composition?.previewSize || (composition as any)?.meta?.previewSize || []; if (!this.mars) { const canvas = document.createElement('canvas'); // document.body.appendChild(canvas); // canvas.style.position = 'absolute'; // canvas.style.top = '0px'; // canvas.style.left = '0px'; canvas.width = w; canvas.height = h; this.mars = new window.ge.Player({ canvas, }); marsPlayerMap.set(key, this.mars); } this.mars.animElemMap = this.mars.animElemMap || new Map(); const { speed, reverse } = data[GameObjectBaseType.MARS]; const s = reverse ? -1 : speed ?? 1; this.mars.animElemMap.set(this.id, { context: this.marsCanvas.getContext('2d')!, fps: data[GameObjectBaseType.MARS].fps || 60, speed: s, pixelRatio: data[GameObjectBaseType.MARS].pixelRatio || 1, width: w, height: h, }); const player = this.mars; player .loadScene(animationData, { autoplay: data[GameObjectBaseType.MARS].autoplay, // speed: data[GameObjectBaseType.MARS].reverse // ? -1 // : data[GameObjectBaseType.MARS].speed, }) .then(e => { this.marsComposition = e; if (!data[GameObjectBaseType.MARS].autoplay) { this.gotoAndStop(0); } }); player.on('update', () => { this.rendererObject?.texture?.source?.update(); }); } dispose(): void { const num = (marsElementMap.get(this.id) || 0) - 1; if (num <= 0) { this.mars?.animElemMap.delete(this.id); marsElementMap.delete(this.id); } else { marsElementMap.set(this.id, num); } if (this.mars && !marsElementMap.size) { this.mars.dispose(true); marsPlayerMap.delete(`mars_${this.isSnapshot ? '_isSnapshot' : ''}`); } this.rendererObject?.destroy(); super.dispose(); } setDepth(depth: number): void { if (this.rendererObject) { this.rendererObject!.zIndex = depth; } } gotoAndPlay(frame: number): void { this.marsComposition?.gotoAndPlay(frame); } gotoAndStop(frame: number): void { this.marsComposition?.gotoAndStop(frame); } play(): void { this.marsComposition?.play(); } pause(): void { this.marsComposition?.pause(); } update(data: MarsGameObjectData): void { if (this.rendererObject && this.mars) { const { autoplay, fps, speed, pixelRatio, interactive, reverse } = data[ GameObjectBaseType.MARS ]; const oldData = this.data[GameObjectBaseType.MARS]; const animData = this.mars.animElemMap.get(this.id); if ( reverse !== oldData.reverse || (speed !== oldData.speed && typeof speed === 'number') ) { const s = reverse ? -1 : speed ?? 1; if (animData) { animData.speed = s; } // this.mars?.setSpeed(s); } if (autoplay !== oldData.autoplay) { if (autoplay) { this.gotoAndPlay(0); } else { this.gotoAndStop(0); } } if (fps !== oldData.fps && typeof fps === 'number') { if (animData) { animData.fps = fps; } } if (interactive !== oldData.interactive && this.mars) { this.mars.interactive = interactive; } if (pixelRatio !== oldData.pixelRatio && typeof pixelRatio === 'number') { if (animData) { animData.pixelRatio = pixelRatio; } } setGameObjectBaseData(this, data, true); this.rendererObject.visible = getVisible(data, this); } super.update(data); } getRendererObject(): PIXI.Sprite | undefined { return this.rendererObject; } getDisplaySize(): { width: number; height: number } { const { compositions = [], compositionId } = this.animationData || {}; const composition = compositions.find((c: any) => c.id === compositionId); const [w, h] = composition?.previewSize || (composition as any)?.meta?.previewSize || []; return { width: w || 0, height: h || 0, }; } }