From 71027a8af6c742d32743ec097646bd3e518c4702 Mon Sep 17 00:00:00 2001 From: cha0s Date: Sun, 3 Nov 2019 13:27:51 -0600 Subject: [PATCH] feat: loot particles --- common/traits/lootable.trait.js | 45 +++++++++++++++++++++------------ server/fixtures/kitty.entity.js | 7 ----- 2 files changed, 29 insertions(+), 23 deletions(-) diff --git a/common/traits/lootable.trait.js b/common/traits/lootable.trait.js index 006f4a1..dd12fca 100644 --- a/common/traits/lootable.trait.js +++ b/common/traits/lootable.trait.js @@ -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; } } diff --git a/server/fixtures/kitty.entity.js b/server/fixtures/kitty.entity.js index 9e2034e..0fec8db 100644 --- a/server/fixtures/kitty.entity.js +++ b/server/fixtures/kitty.entity.js @@ -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: {}, }, };