import { GameObjectBaseType, GameObjectData, GameResource, } from '@gedit/render-engine'; import { deepClone } from '@gedit/utils'; import { PixiGameScene } from '../../common'; import { PixiGameObjectBase } from '../pixi-base-game-object'; import { setGameObjectBaseData, getVisible, easingsFunctions, parsePath, } from '../utils'; import { BehaviorsType, MovePathConfig, ParticlesSchema, SpawnTypeConfig, TextureTypeConfig, } from './particles-behaviors-type'; export * from './particles-behaviors-type'; export interface ParticlesGameObjectData extends GameObjectData { [GameObjectBaseType.PARTICLES]: ParticlesSchema; example: string; autoplay: boolean; } export class ParticlesGameObject extends PixiGameObjectBase< ParticlesGameObjectData, PixiGameScene, PIXI.Container > { static type = GameObjectBaseType.PARTICLES; static needResource = true; rendererObject: PIXI.Container; emitter?: PIXI.particles.Emitter; ticker?: PIXI.Ticker; elapsed: number; resource: GameResource; oldParticlesJSON?: string; get width(): number { return this.data.size?.width || 50; } set width(value: number) { } get height(): number { return this.data.size?.height || 50; } set height(value: number) { } create(resource: GameResource): void { // const scene = this.scene.scene; if (!this.data.particles.behaviors) { return console.warn('Warning: 粒子不兼容, 自动删除粒子图层。'); } this.rendererObject = new PIXI.Container(); this.scene.container.addChild?.(this.rendererObject); this.emitter = new PIXI.particles.Emitter( this.rendererObject as any, undefined as any ); this.ticker = PIXI.Ticker.shared; this.elapsed = Date.now(); this.ticker.add(this.onTicker.bind(this)); super.create.apply(this, arguments); } play(cb?: () => void): void { this.playParticle(this.data.particles); if (cb) { this.emitter?.playOnce(cb); } } replay(cb?: () => void): void { this.playParticle(this.data.particles, true); if (cb) { this.emitter?.playOnce(cb); } } dispose(): void { this.emitter?.destroy(); this.rendererObject?.destroy(); super.dispose(); } setDepth(depth: number): void { if (this.rendererObject) { this.rendererObject!.zIndex = depth; } } textureToResource(config: TextureTypeConfig): TextureTypeConfig { const newConfig: TextureTypeConfig = deepClone(config); if (newConfig.textures) { newConfig.textures = newConfig.textures .map(c => { if (this.resource.children) { return ( this.resource.children.find(d => d.id === c)?.resourceKey || c || '' ); } return c === this.resource.id ? this.resource.resourceKey : c || ''; }) .filter(c => c); } if (newConfig.anims) { newConfig.anims = newConfig.anims.map(c => ({ ...c, textures: c.textures.map(d => { if (this.resource.children) { return { ...d, texture: (!d.texture.match(/^http(s?):\/\//g) && this.resource.children.find(e => e.id === d.texture) ?.resourceKey) || d.texture || '', }; } return { ...d, texture: !d.texture.match(/^http(s?):\/\//g) && d.texture === this.resource.id ? this.resource.resourceKey : d.texture || '', }; }), })); } return newConfig; } playParticle(particles: ParticlesSchema, forcePlay?: boolean): void { const { pos, lifetime, frequency, emitterLifetime, maxParticles, behaviors, } = particles; const particlesJSON = JSON.stringify(behaviors); if ( forcePlay || !this.oldParticlesJSON || particlesJSON !== this.oldParticlesJSON ) { // texturesType 和 spawnType 需要转换 const behaviors = particles.behaviors.map(item => { let config; if (item.type === BehaviorsType.TEXTURE_TYPE) { config = item.config as TextureTypeConfig; const newConfig = this.textureToResource(config); return { type: config.type, config: newConfig, }; } if (item.type === BehaviorsType.SPAWN_TYPE) { config = item.config as SpawnTypeConfig; const type = config.type === BehaviorsType.SPAWN_POINT || config.type === BehaviorsType.SPAWN_BURST ? config.type : BehaviorsType.SPAWN_SHAPE; // 'rect' | 'polygonalChain' | 'torus' return { type, config, }; } // path 转成 x => number; if (item.type === BehaviorsType.MOVE_PATH) { config = item.config as MovePathConfig; let p: string | ((x: number) => number) = config.path; if (typeof p === 'string') { try { p = parsePath(p); } catch (e) { console.warn('PathParticle: error in parsing path expression', p); p = x => x; } } return { ...item, config: { ...config, path: p, }, }; } return item; }); // console.log(particles.ease); const newParticles = { ...particles, ease: particles.ease ? easingsFunctions[particles.ease] : easingsFunctions.linear, behaviors, }; this.emitter?.init(newParticles); this.oldParticlesJSON = particlesJSON; } if (!this.emitter) { return; } this.emitter.minLifetime = lifetime.min; this.emitter.maxLifetime = lifetime.max; this.emitter.frequency = frequency; this.emitter.emitterLifetime = emitterLifetime || 0; this.emitter.maxParticles = maxParticles || 0; if ( this.emitter?.spawnPos.x !== pos.x || this.emitter.spawnPos.y !== pos.y ) { this.emitter?.updateSpawnPos(pos.x, pos.y); } } protected initParticles(data: ParticlesGameObjectData): void { const { particles, autoplay } = data; if (autoplay) { this.playParticle(particles, this.data.autoplay !== autoplay); } else { this.emitter?.cleanup(); this.emitter?.init({ lifetime: { min: 0, max: 0, }, frequency: 0, emitterLifetime: 0.0001, maxParticles: 0, addAtBack: false, pos: { x: 0, y: 0, }, behaviors: [], }); } } update(data: ParticlesGameObjectData): void { // console.log(data); // console.log(this.resource); if (!this.emitter) { return; } const { particles, autoplay, ...otherData } = data; this.initParticles(data); // this.emitter?.resetPositionTracking(); if (this.rendererObject) { setGameObjectBaseData(this, otherData, true); this.rendererObject.visible = getVisible(data, this); } super.update(data); } onTicker(): void { const now = Date.now(); if (this.emitter) { this.emitter.update((now - this.elapsed) * 0.001); } this.elapsed = now; } getRendererObject(): PIXI.Container { return this.rendererObject; } getEmitter(): PIXI.particles.Emitter | undefined { return this.emitter; } }