fun: pick it up and put it in!

This commit is contained in:
cha0s 2019-10-07 06:29:57 -05:00
parent c14f167456
commit 7a8c787261

View File

@ -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 {