fix: emitter throttling

This commit is contained in:
cha0s 2019-04-25 19:19:38 -05:00
parent 8d2a323d90
commit c4ad740406

View File

@ -12,6 +12,7 @@ export class Emitter extends decorate(Trait) {
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);
@ -19,6 +20,39 @@ export class Emitter extends decorate(Trait) {
});
}
static addEmitter(emitter) {
if (!this._emitters) {
this._emitters = [];
}
this._emitters.push(emitter);
}
static particleCount() {
let particleCount = 0;
if (!this._emitters) {
return particleCount;
}
for (let i = 0; i < this._emitters.length; i++) {
const emitter = this._emitters[i];
particleCount += emitter.particleCount;
}
return particleCount;
}
static removeEmitter(emitter) {
const index = this._emitters.indexOf(emitter);
if (-1 === index) {
return;
}
this._emitters.splice(index, 1);
}
updateFrequency() {
const particleCount = this.constructor.particleCount();
const updatesPerSecond = Math.max(15, (60 - (particleCount / 10)));
this.ticker.frequency = 1 / updatesPerSecond;
}
hooks() {
return {
@ -28,6 +62,7 @@ export class Emitter extends decorate(Trait) {
const emitter = this.emitters[key];
this.ticker.tick(elapsed);
if (!emitter.hasParticles()) {
emitter.destroy();
delete this.emitters[key];
}
}
@ -43,6 +78,7 @@ export class Emitter extends decorate(Trait) {
addEmitter: (key, emitter) => {
this.emitters[key] = emitter;
this.constructor.addEmitter(emitter);
},
addEmitterRenderer: (key, renderer) => {