silphius/app/ecs-components/index.js
2024-06-22 22:33:54 -05:00

39 lines
1.1 KiB
JavaScript

import Arbitrary from '@/ecs/arbitrary.js';
import Base from '@/ecs/base.js';
import Schema from '@/ecs/schema.js';
import gather from '@/util/gather.js';
const specificationsAndOrDecorators = gather(
import.meta.glob('./*.js', {eager: true, import: 'default'}),
);
const Components = {};
for (const componentName in specificationsAndOrDecorators) {
// TODO: byKey, byId, ...
if (Number.isInteger(+componentName)) {
continue;
}
const specificationOrDecorator = specificationsAndOrDecorators[componentName];
if ('function' === typeof specificationOrDecorator) {
Components[componentName] = specificationOrDecorator(
class Decorated extends Arbitrary {
static name = componentName;
}
);
if (!Components[componentName]) {
throw new Error(`Component ${componentName} decorator returned nothing`);
}
}
else {
Components[componentName] = class Component extends Arbitrary {
static name = componentName;
static schema = new Schema({
type: 'object',
properties: specificationOrDecorator,
});
}
}
}
export default Components;