feat: loot particles

This commit is contained in:
cha0s 2019-11-03 13:27:51 -06:00
parent a3c68fa56e
commit 71027a8af6
2 changed files with 29 additions and 23 deletions

View File

@ -1,6 +1,6 @@
import {compose, Property} from '@avocado/core';
import {StateProperty, Trait, Entity} from '@avocado/entity';
import {Rectangle, Vector} from '@avocado/math';
import {randomNumber, Rectangle, Vector} from '@avocado/math';
const decorate = compose(
StateProperty('lootable', {
@ -41,28 +41,41 @@ export class Lootable extends decorate(Trait) {
return jsons;
}
listeners() {
const listeners = {};
hooks() {
const hooks = {};
if (AVOCADO_SERVER) {
listeners['died'] = () => {
hooks['died'] = () => {
const jsons = this.calculateLoot();
const position = this.entity.position;
const promises = [];
for (let i = 0; i < jsons.length; i++) {
const offset = Vector.sub(
[
Math.floor(Math.random() * 16),
Math.floor(Math.random() * 16)
],
[8, 8]
);
this.entity.spawnRawAt(
Vector.add(position, offset),
jsons[i]
);
const json = jsons[i];
if (!json.traits) {
json.traits = {};
}
json.traits.emitted = {
params: {
force: [0, 16],
velocity: [
randomNumber(-0.5, 0.5),
randomNumber(-2.5, -1.5)
],
position,
transient: false,
ttl: 0.25,
},
};
const promise = this.entity.emitParticleJson(
json
).then((particle) => {
this.entity.list.addEntity(particle);
});
promises.push(promise);
}
return Promise.all(promises);
};
}
return listeners;
return hooks;
}
}

View File

@ -119,12 +119,6 @@ export function kittyJSON() {
lootable: {
params: {
table: [
{
perc: 20,
json: {
uri: '/rock.entity.json',
},
},
{
perc: 70,
json: {
@ -151,7 +145,6 @@ export function kittyJSON() {
},
},
},
spawner: {},
vulnerable: {},
},
};