78 lines
1.7 KiB
JavaScript
78 lines
1.7 KiB
JavaScript
import {Context} from '@avocado/behavior';
|
|
import {compose, TickingPromise} from '@avocado/core';
|
|
import {StateProperty, Trait} from '@avocado/entity';
|
|
|
|
const decorate = compose(
|
|
StateProperty('activeSlotIndex', {
|
|
track: true,
|
|
}),
|
|
);
|
|
|
|
export class Wielder extends decorate(Trait) {
|
|
|
|
static dependencies() {
|
|
return [
|
|
'receptacle',
|
|
];
|
|
}
|
|
|
|
static defaultState() {
|
|
return {
|
|
activeSlotIndex: 0,
|
|
};
|
|
}
|
|
|
|
static type() {
|
|
return 'wielder';
|
|
}
|
|
|
|
constructor(entity, params, state) {
|
|
super(entity, params, state);
|
|
this.actionsTickingPromise = undefined;
|
|
}
|
|
|
|
methods() {
|
|
return {
|
|
|
|
itemInActiveSlot: () => {
|
|
return this.entity.itemInSlot(this.entity.activeSlotIndex);
|
|
},
|
|
|
|
useItemInActiveSlot: () => {
|
|
if (-1 === this.entity.activeSlotIndex) {
|
|
return;
|
|
}
|
|
this.entity.useItemInSlot(this.entity.activeSlotIndex)
|
|
},
|
|
|
|
useItemInSlot: (slotIndex) => {
|
|
// Already using an item? TODO: cancel on active slot change?
|
|
if (this.actionsTickingPromise) {
|
|
return this.actionsTickingPromise;
|
|
}
|
|
const item = this.entity.itemInSlot(slotIndex);
|
|
if (!item) {
|
|
return;
|
|
}
|
|
// Set up context.
|
|
const context = new Context({
|
|
wielder: this.entity,
|
|
item,
|
|
});
|
|
// Defer until the item actions are finished.
|
|
this.actionsTickingPromise = item.itemActions.tickingPromise(
|
|
context
|
|
);
|
|
this.actionsTickingPromise.then(() => {
|
|
context.destroy();
|
|
this.actionsTickingPromise = undefined;
|
|
});
|
|
return this.actionsTickingPromise;
|
|
},
|
|
|
|
};
|
|
}
|
|
|
|
}
|
|
|