Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Emitter pool was created whether you used it or not #3138

Merged
merged 1 commit into from
Jul 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading