silphius/app/ecs-components/wielder.js
2024-06-28 12:12:38 -05:00

33 lines
1020 B
JavaScript

import Component from '@/ecs/component.js';
export default class Wielder extends Component {
instanceFromSchema() {
const {ecs} = this;
const Instance = super.instanceFromSchema();
return class WielderInstance extends Instance {
activeItem() {
const {Inventory, Wielder} = ecs.get(this.entity);
return Inventory.item(Wielder.activeSlot + 1);
}
useActiveItem(state) {
const entity = ecs.get(this.entity);
const {Ticking} = entity;
const activeItem = this.activeItem();
if (activeItem) {
const {startInstance, stopInstance} = activeItem.scripts;
const script = state ? startInstance : stopInstance;
if (script) {
script.context.ecs = ecs;
script.context.item = activeItem;
script.context.wielder = entity;
Ticking.addTickingPromise(script.tickingPromise());
}
}
}
}
}
static properties = {
activeSlot: {type: 'uint16'},
};
}