silphius/app/ecs/components/wielder.js
2024-07-25 10:47:52 -05:00

34 lines
1.0 KiB
JavaScript

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