44 lines
1.1 KiB
JavaScript
44 lines
1.1 KiB
JavaScript
import Component from '@/ecs/component.js';
|
|
|
|
export default class Behaving extends Component {
|
|
instanceFromSchema() {
|
|
const {ecs} = this;
|
|
return class BehavingInstance extends super.instanceFromSchema() {
|
|
$$routineInstances = {};
|
|
tick(elapsed) {
|
|
const routine = this.$$routineInstances[this.currentRoutine];
|
|
if (routine) {
|
|
routine.context.entity = ecs.get(this.entity);
|
|
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])
|
|
.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',
|
|
},
|
|
},
|
|
};
|
|
}
|