From fa4edd127c51c83c7a1785a2da37cb28348fd822 Mon Sep 17 00:00:00 2001 From: Erik Onarheim Date: Tue, 23 Jul 2024 08:47:46 -0500 Subject: [PATCH] fix emitter pool --- src/engine/EntityComponentSystem/Entity.ts | 17 +++++++++++------ src/engine/Particles/ParticleEmitter.ts | 19 ++++++++----------- src/engine/Particles/Particles.ts | 4 +++- 3 files changed, 22 insertions(+), 18 deletions(-) diff --git a/src/engine/EntityComponentSystem/Entity.ts b/src/engine/EntityComponentSystem/Entity.ts index d5b8debb0..f20229d12 100644 --- a/src/engine/EntityComponentSystem/Entity.ts +++ b/src/engine/EntityComponentSystem/Entity.ts @@ -69,6 +69,7 @@ export const EntityEvents = { export interface EntityOptions { name?: string; components?: TComponents[]; + silenceWarnings?: boolean; } /** @@ -119,13 +120,15 @@ export class Entity implements OnIniti constructor(componentsOrOptions?: TKnownComponents[] | EntityOptions, name?: string) { let componentsToAdd!: TKnownComponents[]; let nameToAdd: string | undefined; + let silence = false; if (Array.isArray(componentsOrOptions)) { componentsToAdd = componentsOrOptions; nameToAdd = name; } else if (componentsOrOptions && typeof componentsOrOptions === 'object') { - const { components, name } = componentsOrOptions; + const { components, name, silenceWarnings } = componentsOrOptions; componentsToAdd = components ?? []; nameToAdd = name; + silence = !!silenceWarnings; } if (nameToAdd) { this.name = nameToAdd; @@ -137,11 +140,13 @@ export class Entity implements OnIniti } if (process.env.NODE_ENV === 'development') { - setTimeout(() => { - if (!this.scene && !this.isInitialized) { - Logger.getInstance().warn(`Entity "${this.name || this.id}" was not added to a scene.`); - } - }, 5000); + if (!silence) { + setTimeout(() => { + if (!this.scene && !this.isInitialized) { + Logger.getInstance().warn(`Entity "${this.name || this.id}" was not added to a scene.`); + } + }, 5000); + } } } diff --git a/src/engine/Particles/ParticleEmitter.ts b/src/engine/Particles/ParticleEmitter.ts index 8f9f08343..c4aafd162 100644 --- a/src/engine/Particles/ParticleEmitter.ts +++ b/src/engine/Particles/ParticleEmitter.ts @@ -8,15 +8,6 @@ import { EmitterType } from '../EmitterType'; import { Particle, ParticleTransform, ParticleEmitterArgs, ParticleConfig } from './Particles'; import { RentalPool } from '../Util/RentalPool'; -/** - * Used internally by Excalibur to manage all particles in the engine - */ -export const ParticlePool = new RentalPool( - () => new Particle({}), - (p) => p, - 2000 -); - /** * Using a particle emitter is a great way to create interesting effects * in your game, like smoke, fire, water, explosions, etc. `ParticleEmitter` @@ -25,6 +16,12 @@ export const ParticlePool = new RentalPool( export class ParticleEmitter extends Actor { private _particlesToEmit: number = 0; + private _particlePool = new RentalPool( + () => new Particle({}), + (p) => p, + 500 + ); + public numParticles: number = 0; /** @@ -134,7 +131,7 @@ export class ParticleEmitter extends Actor { ranY = radius * Math.sin(angle); } - const p = ParticlePool.rent(); + const p = this._particlePool.rent(); p.configure({ life: this.particle.life, opacity: this.particle.opacity, @@ -176,7 +173,7 @@ export class ParticleEmitter extends Actor { for (let i = 0; i < this.deadParticles.length; i++) { if (this?.scene?.world) { this.scene.world.remove(this.deadParticles[i], false); - ParticlePool.return(this.deadParticles[i]); + this._particlePool.return(this.deadParticles[i]); } } this.deadParticles.length = 0; diff --git a/src/engine/Particles/Particles.ts b/src/engine/Particles/Particles.ts index 357179e72..5c9bacf37 100644 --- a/src/engine/Particles/Particles.ts +++ b/src/engine/Particles/Particles.ts @@ -60,8 +60,10 @@ export class Particle extends Entity { public graphics: GraphicsComponent; public particleTransform = ParticleTransform.Global; + public name = `Particle#${this.id}`; + constructor(options: ParticleConfig) { - super(); + super({ silenceWarnings: true }); this.addComponent((this.transform = new TransformComponent())); this.addComponent((this.motion = new MotionComponent())); this.addComponent((this.graphics = new GraphicsComponent()));