avocado-old/packages/graphics/traits/emitter.trait.js

106 lines
2.3 KiB
JavaScript
Raw Normal View History

2019-04-20 16:03:11 -05:00
import {compose} from '@avocado/core';
import {Trait} from '@avocado/entity';
import {Property} from '@avocado/mixins';
2019-04-25 02:28:54 -05:00
import {Ticker} from '@avocado/timing';
2019-04-20 16:03:11 -05:00
const decorate = compose(
)
export class Emitter extends decorate(Trait) {
initialize() {
this.emitters = {};
2019-04-25 02:28:54 -05:00
this.ticker = new Ticker(1 / 10);
this.ticker.on('tick', (elapsed) => {
2019-04-25 19:19:38 -05:00
this.updateFrequency();
2019-04-25 02:28:54 -05:00
for (const key in this.emitters) {
const emitter = this.emitters[key];
emitter.tick(elapsed);
}
});
2019-04-20 16:03:11 -05:00
}
2019-04-25 19:19:38 -05:00
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;
}
2019-04-20 16:03:11 -05:00
hooks() {
return {
afterDestructionTickers: () => {
return (elapsed) => {
for (const key in this.emitters) {
const emitter = this.emitters[key];
2019-04-25 02:28:54 -05:00
this.ticker.tick(elapsed);
2019-04-20 16:03:11 -05:00
if (!emitter.hasParticles()) {
2019-04-25 19:19:38 -05:00
emitter.destroy();
2019-04-20 16:03:11 -05:00
delete this.emitters[key];
}
}
return 0 === Object.keys(this.emitters).length;
};
},
}
}
methods() {
return {
addEmitter: (key, emitter) => {
this.emitters[key] = emitter;
2019-04-25 19:19:38 -05:00
this.constructor.addEmitter(emitter);
2019-04-20 16:03:11 -05:00
},
addEmitterRenderer: (key, renderer) => {
if (!this.emitters[key]) {
return;
}
this.emitters[key].addRenderer(renderer);
},
emitParticle: (key, ...args) => {
if (!this.emitters[key]) {
return;
}
this.emitters[key].emit(...args);
},
}
}
renderTick(elapsed) {
2019-04-25 02:28:54 -05:00
this.ticker.tick(elapsed);
2019-04-20 16:03:11 -05:00
}
}