import { GameObjectBaseType, GameObjectData, GameObjectBase, GameObject, } from '@gedit/render-engine'; import { PixiGameScene } from '../../common'; import { setGameObjectBaseData, getVisible } from '../utils'; import { SpineSkeletonResource } from '../../common/resource/spine'; export interface SpineSchema { animationName: string; animationLoop: boolean; autoPlay: boolean; } export interface SpineGameObjectData extends GameObjectData { [GameObjectBaseType.SPINE]: SpineSchema; } export class SpineGameObject extends GameObjectBase< SpineGameObjectData, PixiGameScene > { static type = GameObjectBaseType.SPINE; static needResource = true; rendererObject?: any; spineObject?: spine.Spine; track?: any; async create( resource: SpineSkeletonResource, children: GameObject[] ): Promise { this.rendererObject = new PIXI.Container(); if (!resource.uri || !resource.params.atlas.length) { super.create.apply(this, arguments); return; } this.spineObject = window.spine.Spine.from({ skeleton: resource.resourceKey, atlas: resource.params.atlas[0].resourceKey, }); this.rendererObject.addChild(this.spineObject); this.scene.container.addChild?.(this.rendererObject); this.init(); super.create.apply(this, arguments); } init(): void { if (!this.rendererObject || !this.spineObject) return; const spineObject = this.spineObject; const spineData = this.data[GameObjectBaseType.SPINE]; if (spineData.animationName) { this.track = spineObject.state.setAnimation( 0, spineData.animationName, spineData.animationLoop ); if (!spineData.autoPlay) { this.track.animationEnd = 0; } } } clearAnim(): void { const spineObject = this.spineObject; if (spineObject) { spineObject.state.clearTracks(); spineObject.skeleton.setToSetupPose(); this.track = null; } } dispose(): void { this.clearAnim(); this.rendererObject?.destroy(); super.dispose(); } update(data: SpineGameObjectData): void { if (!this.rendererObject || !this.spineObject) return; const spineObject = this.spineObject; const spineData = data[GameObjectBaseType.SPINE]; if ( spineData.animationName !== this.data[GameObjectBaseType.SPINE].animationName || spineData.animationLoop !== this.data[GameObjectBaseType.SPINE].animationLoop || spineData.autoPlay !== this.data[GameObjectBaseType.SPINE].autoPlay ) { if ( !spineData.animationName || (spineData.autoPlay && this.track && this.track.isComplete) ) { this.clearAnim(); } if ( spineData.animationName && (spineData.animationName !== this.data[GameObjectBaseType.SPINE].animationName || (spineData.autoPlay && !this.track) || (spineData.autoPlay && this.track && this.track.isComplete)) ) { this.track = spineObject.state.setAnimation( 0, spineData.animationName, spineData.animationLoop ); } if (!spineData.autoPlay && this.track) { this.track.animationEnd = 0; spineObject.state.update(0); } } // 等 spine 渲染后再设置 rendererObject,pivot 需要 bounds width height; setGameObjectBaseData(this, data, true); this.rendererObject.visible = getVisible(data, this); super.update(data); } getDisplaySize(): { width: number; height: number } { const spineObj = this.spineObject?.skeleton.data; return { width: spineObj?.width || 0, height: spineObj?.height || 0, }; } getRendererObject(): SpineGameObject | undefined { return this.rendererObject; } setDepth(depth: number): void { if (this.rendererObject) { this.rendererObject!.zIndex = depth; } } }