silphius/app/ecs-components/wielder.js

33 lines
1.0 KiB
JavaScript
Raw Normal View History

2024-06-26 21:08:09 -05:00
import Component from '@/ecs/component.js';
2024-06-25 05:46:03 -05:00
2024-06-26 21:08:09 -05:00
export default class Wielder extends Component {
instanceFromSchema() {
2024-06-27 11:06:58 -05:00
const {ecs} = this;
2024-07-04 15:17:49 -05:00
return class WielderInstance extends super.instanceFromSchema() {
2024-06-28 08:53:20 -05:00
activeItem() {
2024-06-27 11:06:58 -05:00
const {Inventory, Wielder} = ecs.get(this.entity);
return Inventory.item(Wielder.activeSlot + 1);
2024-06-26 21:08:09 -05:00
}
2024-06-28 08:53:20 -05:00
useActiveItem(state) {
2024-06-27 11:06:58 -05:00
const entity = ecs.get(this.entity);
const {Ticking} = entity;
2024-06-28 08:53:20 -05:00
const activeItem = this.activeItem();
2024-06-27 11:06:58 -05:00
if (activeItem) {
2024-06-28 12:12:38 -05:00
const {startInstance, stopInstance} = activeItem.scripts;
2024-07-04 09:24:49 -05:00
let script = state ? startInstance : stopInstance;
2024-06-28 12:12:38 -05:00
if (script) {
2024-07-04 09:24:49 -05:00
script = script.clone();
2024-06-28 12:12:38 -05:00
script.context.ecs = ecs;
script.context.item = activeItem;
script.context.wielder = entity;
Ticking.addTickingPromise(script.tickingPromise());
}
2024-06-27 11:06:58 -05:00
}
2024-06-25 06:20:09 -05:00
}
2024-06-25 05:46:03 -05:00
}
}
2024-06-26 21:08:09 -05:00
static properties = {
activeSlot: {type: 'uint16'},
};
2024-06-25 05:46:03 -05:00
}