silphius/app/ecs/components/behaving.js

47 lines
1.1 KiB
JavaScript
Raw Normal View History

2024-07-21 11:14:51 -05:00
import Component from '@/ecs/component.js';
export default class Behaving extends Component {
instanceFromSchema() {
return class BehavingInstance extends super.instanceFromSchema() {
$$routineInstances = {};
tick(elapsed) {
const routine = this.$$routineInstances[this.currentRoutine];
if (routine) {
routine.tick(elapsed);
}
}
};
}
async load(instance) {
// heavy handed...
if ('undefined' !== typeof window) {
return;
}
const promises = [];
for (const key in instance.routines) {
promises.push(
this.ecs.readScript(
instance.routines[key],
{
entity: this.ecs.get(instance.entity),
},
)
.then((script) => {
instance.$$routineInstances[key] = script;
}),
);
}
await Promise.all(promises);
}
static properties = {
currentRoutine: {defaultValue: 'initial', type: 'string'},
isBehaving: {defaultValue: 1, type: 'uint8'},
routines: {
type: 'map',
value: {
type: 'string',
},
},
};
}