From 7a8c7872615f3ed6968762035e07ae019faf2f77 Mon Sep 17 00:00:00 2001 From: cha0s Date: Mon, 7 Oct 2019 06:29:57 -0500 Subject: [PATCH] fun: pick it up and put it in! --- common/traits/receptacle.trait.js | 50 ++++++++++++++++++++++++++++++- 1 file changed, 49 insertions(+), 1 deletion(-) diff --git a/common/traits/receptacle.trait.js b/common/traits/receptacle.trait.js index 6a6cd77..87f8e49 100644 --- a/common/traits/receptacle.trait.js +++ b/common/traits/receptacle.trait.js @@ -119,6 +119,15 @@ export class Receptacle extends decorate(Trait) { item.on('qtyChanged', listener); } + firstFreeSlotIndex() { + for (let i = 0; i < this.entity.slotCount; ++i) { + if (!this.slotItems[i]) { + return i; + } + } + return -1; + } + isSlotIndexInRange(slotIndex) { return slotIndex >= 0 && slotIndex < this.entity.slotCount; } @@ -132,8 +141,19 @@ export class Receptacle extends decorate(Trait) { return -1; } - // TODO mergeItem(item) { + const slotIndex = this.queryItem(item); + if (-1 === slotIndex) { + const freeSlotIndex = this.firstFreeSlotIndex(); + if (-1 !== freeSlotIndex) { + this.entity.addItemToSlot(item, freeSlotIndex); + } + } + else { + const slotItem = this.entity.itemInSlot(slotIndex); + slotItem.decrementQuantity(-item.qty); + item.destroy(); + } } packets(informed) { @@ -142,6 +162,22 @@ export class Receptacle extends decorate(Trait) { } } + queryItem(item) { + for (let i = 0; i < this.entity.slotCount; ++i) { + // Anything? + if (!this.slotItems[i]) { + continue; + } + // URI? + if (item.uri !== this.slotItems[i].uri) { + continue; + } + // TODO: Qty, Specifics? + return i; + } + return -1; + } + removeListenersForItem(item) { item.off('qtyChanged', this.qtyListeners.get(item)); this.qtyListeners.delete(item); @@ -155,6 +191,18 @@ export class Receptacle extends decorate(Trait) { return json; } + listeners() { + const listeners = {}; + if (AVOCADO_SERVER) { + listeners.collisionStart = (other) => { + if (other.is('item')) { + this.entity.addItemToSlot(other); + } + }; + } + return listeners; + } + methods() { return {