feat: particle broadcasting

This commit is contained in:
cha0s 2021-02-05 16:33:08 -06:00
parent 784fa2cc75
commit 71b9175289
3 changed files with 71 additions and 18 deletions

View File

@ -17,5 +17,8 @@ export default {
'@avocado/resource/resources.decorate': decorateWithLatus(
require.context('./resources/decorators', false, /\.js$/),
),
'@latus/socket/packets': gatherWithLatus(
require.context('./packets', false, /\.js$/),
),
},
};

View File

@ -0,0 +1,11 @@
import {compose} from '@latus/core';
import {
Packer,
Packet,
} from '@latus/socket';
const decorate = compose(
Packer(),
);
export default () => decorate(class EmitParticlesPacket extends Packet {});

View File

@ -16,6 +16,8 @@ export default (latus) => class Emitter extends decorate(Trait) {
#emitter = new Proton.Emitter();
#emitting = [];
#onParticleDead;
#onParticleUpdate;
@ -36,6 +38,18 @@ export default (latus) => class Emitter extends decorate(Trait) {
this.#emitter.addEventListener('PARTICLE_UPDATE', this.#onParticleUpdate);
}
acceptPacket(packet) {
if ('EmitParticles' === packet.constructor.type) {
for (let i = 0; i < packet.data.length; i++) {
this.entity.emitParticleJson(packet.data[i]);
}
}
}
cleanPackets() {
this.#emitting = [];
}
static defaultParams() {
return {
particles: {},
@ -82,24 +96,23 @@ export default (latus) => class Emitter extends decorate(Trait) {
},
],
},
jsonForParticle: {
type: 'object',
label: 'Create particle.',
args: [
{
label: 'key',
type: 'string',
},
],
},
};
}
async emitParticleJson(json) {
const {Entity} = latus.get('%resources');
return this.entity.emitParticleEntity(
await Entity.load(
merge(
{
traits: {
Positioned: {},
Visible: {},
},
},
json,
),
),
);
return this.entity.emitParticleEntity(await Entity.load(json));
}
gatherParticles() {
@ -148,6 +161,9 @@ export default (latus) => class Emitter extends decorate(Trait) {
return {
emitParticleEntity: (entity) => {
if (!entity.is('Emitted')) {
return undefined;
}
const particle = entity.particle();
const position = particle.position
? particle.position
@ -204,14 +220,29 @@ export default (latus) => class Emitter extends decorate(Trait) {
emitParticleJson: (json) => {
let {
count = 1,
// eslint-disable-next-line prefer-const
} = json;
const {
transmit = true,
rate = 0,
} = json;
const augmentedJson = merge(
{
traits: {
Positioned: {},
Visible: {},
},
},
json,
);
if (transmit && 'client' !== process.env.SIDE) {
this.#emitting.push(augmentedJson);
this.markAsDirty();
}
const stream = K.stream((emitter) => {
if (0 === rate) {
const promises = [];
for (let i = 0; i < count; ++i) {
promises.push(this.emitParticleJson(json).then((particle) => {
promises.push(this.emitParticleJson(augmentedJson).then((particle) => {
emitter.emit(particle);
}));
}
@ -219,7 +250,7 @@ export default (latus) => class Emitter extends decorate(Trait) {
}
else {
const ticker = new Ticker(rate);
this.emitParticleJson(json).then((particle) => {
this.emitParticleJson(augmentedJson).then((particle) => {
emitter.emit(particle);
});
count -= 1;
@ -232,7 +263,7 @@ export default (latus) => class Emitter extends decorate(Trait) {
emitter.end();
};
ticker.on('tick', async () => {
const particle = await this.emitParticleJson(json);
const particle = await this.emitParticleJson(augmentedJson);
emitter.emit(particle);
if (0 >= --count) {
removeEmission();
@ -250,7 +281,7 @@ export default (latus) => class Emitter extends decorate(Trait) {
return stream;
},
emitParticle: (key, json = {}) => {
emitParticle: (key, json) => {
const particleJson = this.#particles[key];
if (!particleJson) {
return undefined;
@ -258,6 +289,8 @@ export default (latus) => class Emitter extends decorate(Trait) {
return this.entity.emitParticleJson(merge(particleJson, json));
},
jsonForParticle: (key) => this.#particles[key] || {},
};
}
@ -281,6 +314,12 @@ export default (latus) => class Emitter extends decorate(Trait) {
/* eslint-enable no-param-reassign */
}
packets() {
return this.#emitting.length > 0
? [['EmitParticles', this.#emitting]]
: [];
}
tick(elapsed) {
this.#emitter.update(elapsed);
for (let i = 0; i < this.#emissions.length; i++) {