silphius/app/ecs/components/interactive.js

40 lines
1.0 KiB
JavaScript

import Component from '@/ecs/component.js';
export default class Interactive extends Component {
instanceFromSchema() {
const {ecs} = this;
return class ControlledInstance extends super.instanceFromSchema() {
$$interact;
interact(initiator) {
const script = this.$$interact.clone();
script.context.initiator = initiator;
script.context.subject = ecs.get(this.entity);
const {Ticking} = script.context.subject;
Ticking.add(script.ticker());
}
get interacting() {
return !!this.$$interacting;
}
set interacting(interacting) {
this.$$interacting = interacting ? 1 : 0;
}
}
}
async load(instance) {
// heavy handed...
if ('undefined' !== typeof window) {
return;
}
instance.$$interact = await this.ecs.readScript(
instance.interactScript,
{
ecs: this.ecs,
},
);
}
static properties = {
interacting: {type: 'uint8'},
interactScript: {type: 'string'},
};
}