refactor: particle emission stream

This commit is contained in:
cha0s 2019-12-12 19:55:30 -06:00
parent 12307024fc
commit d94bafd2f5

View File

@ -1,3 +1,5 @@
import K from 'kefir';
import {compose, merge} from '@avocado/core';
import {Entity, StateProperty, Trait} from '@avocado/entity';
import {Vector} from '@avocado/math';
@ -83,9 +85,10 @@ export class Emitter extends decorate(Trait) {
return hooks;
}
emitParticleJson(json) {
return Entity.loadOrInstance(json).then((particle) => {
emitParticleJson(json, emitter) {
Entity.loadOrInstance(json).then((particle) => {
this.entity.emitParticleEntity(particle);
emitter.emit(particle);
});
}
@ -154,39 +157,45 @@ export class Emitter extends decorate(Trait) {
}
// Prime.
this.onParticleUpdate(protonParticle);
return entity;
},
emitParticleJson: (json) => {
let {
count = 1,
rate = 0,
} = json;
if (0 === rate) {
for (let i = 0; i < count; ++i) {
this.emitParticleJson(json);
return K.stream((emitter) => {
let {
count = 1,
rate = 0,
} = json;
if (0 === rate) {
for (let i = 0; i < count; ++i) {
this.emitParticleJson(json, emitter);
}
}
}
else {
const ticker = new Ticker(rate);
this.emitParticleJson(json);
count -= 1;
if (count > 0) {
const removeEmission = () => {
const index = this.emissions.indexOf(ticker);
if (-1 !== index) {
this.emissions.splice(index, 1);
}
};
this.entity.on('destroy', removeEmission);
ticker.on('tick', () => {
this.emitParticleJson(json);
if (0 >= --count) {
removeEmission();
}
});
this.emissions.push(ticker);
else {
const ticker = new Ticker(rate);
this.emitParticleJson(json, emitter);
count -= 1;
if (count > 0) {
const removeEmission = () => {
const index = this.emissions.indexOf(ticker);
if (-1 !== index) {
this.emissions.splice(index, 1);
}
emitter.end();
};
this.entity.on('destroy', removeEmission);
ticker.on('tick', () => {
this.emitParticleJson(json, emitter);
if (0 >= --count) {
removeEmission();
}
});
this.emissions.push(ticker);
return;
}
}
}
emitter.end();
});
},
emitParticle: (key, json = {}) => {
@ -195,7 +204,7 @@ export class Emitter extends decorate(Trait) {
return;
}
const mergedJson = merge({}, particleJson, json);
this.entity.emitParticleJson(mergedJson);
return this.entity.emitParticleJson(mergedJson);
},
};