feat: emitter!

This commit is contained in:
cha0s 2019-04-20 16:03:11 -05:00
parent 707fa2ec4d
commit 27de793a48

View File

@ -0,0 +1,65 @@
import {compose} from '@avocado/core';
import {Trait} from '@avocado/entity';
import {Property} from '@avocado/mixins';
const decorate = compose(
)
export class Emitter extends decorate(Trait) {
initialize() {
this.emitters = {};
}
hooks() {
return {
afterDestructionTickers: () => {
return (elapsed) => {
for (const key in this.emitters) {
const emitter = this.emitters[key];
emitter.tick(elapsed);
if (!emitter.hasParticles()) {
delete this.emitters[key];
}
}
return 0 === Object.keys(this.emitters).length;
};
},
}
}
methods() {
return {
addEmitter: (key, emitter) => {
this.emitters[key] = emitter;
},
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);
},
}
}
tick(elapsed) {
for (const key in this.emitters) {
const emitter = this.emitters[key];
emitter.tick(elapsed);
}
}
}