fix: only tick emitter on client and wait to remove tick handler until after destroy finishes

This commit is contained in:
cha0s 2019-05-03 01:22:15 -05:00
parent 1f065d58e3
commit 0710ff875d

View File

@ -9,14 +9,10 @@ export class Emitter extends decorate(Trait) {
initialize() {
this.emitters = {};
this.ticker = new Ticker(1 / 10);
this.ticker.on('tick', (elapsed) => {
this.updateFrequency();
for (const key in this.emitters) {
const emitter = this.emitters[key];
emitter.tick(elapsed);
}
});
if (AVOCADO_CLIENT) {
this.ticker = new Ticker(1 / 10);
this.ticker.on('tick', this.onTick, this);
}
}
static addEmitter(emitter) {
@ -46,6 +42,14 @@ export class Emitter extends decorate(Trait) {
this._emitters.splice(index, 1);
}
onTick(elapsed) {
this.updateFrequency();
for (const key in this.emitters) {
const emitter = this.emitters[key];
emitter.tick(elapsed);
}
}
updateFrequency() {
const particleCount = this.constructor.particleCount();
const updatesPerSecond = Math.max(15, (60 - (particleCount / 10)));
@ -61,6 +65,7 @@ export class Emitter extends decorate(Trait) {
const emitter = this.emitters[key];
this.ticker.tick(elapsed);
if (!emitter.hasParticles()) {
this.ticker.off('tick', this.onTick);
emitter.destroy();
this.constructor.removeEmitter(emitter);
delete this.emitters[key];
@ -105,7 +110,9 @@ export class Emitter extends decorate(Trait) {
}
tick(elapsed) {
this.ticker.tick(elapsed);
if (AVOCADO_CLIENT) {
this.ticker.tick(elapsed);
}
}
}