feat: emitter!
This commit is contained in:
parent
707fa2ec4d
commit
27de793a48
65
packages/graphics/traits/emitter.trait.js
Normal file
65
packages/graphics/traits/emitter.trait.js
Normal 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);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user