Skip to content

Commit

Permalink
fix: Emitter pool was created whether you used it or not (#3138)
Browse files Browse the repository at this point in the history
  • Loading branch information
eonarheim authored Jul 23, 2024
1 parent 9f6b1db commit c7b2eb8
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 18 deletions.
17 changes: 11 additions & 6 deletions src/engine/EntityComponentSystem/Entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ export const EntityEvents = {
export interface EntityOptions<TComponents extends Component> {
name?: string;
components?: TComponents[];
silenceWarnings?: boolean;
}

/**
Expand Down Expand Up @@ -119,13 +120,15 @@ export class Entity<TKnownComponents extends Component = any> implements OnIniti
constructor(componentsOrOptions?: TKnownComponents[] | EntityOptions<TKnownComponents>, 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;
Expand All @@ -137,11 +140,13 @@ export class Entity<TKnownComponents extends Component = any> 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);
}
}
}

Expand Down
19 changes: 8 additions & 11 deletions src/engine/Particles/ParticleEmitter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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`
Expand All @@ -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;

/**
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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;
Expand Down
4 changes: 3 additions & 1 deletion src/engine/Particles/Particles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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()));
Expand Down

0 comments on commit c7b2eb8

Please sign in to comment.