silphius/app/ecs-components/index.js

38 lines
1.1 KiB
JavaScript
Raw Normal View History

2024-06-14 15:18:55 -05:00
import Arbitrary from '@/ecs/arbitrary.js';
import Schema from '@/ecs/schema.js';
2024-06-22 22:33:34 -05:00
import gather from '@/util/gather.js';
2024-06-10 22:42:30 -05:00
2024-06-21 00:51:31 -05:00
const specificationsAndOrDecorators = gather(
2024-06-14 15:18:55 -05:00
import.meta.glob('./*.js', {eager: true, import: 'default'}),
);
const Components = {};
2024-06-21 00:51:31 -05:00
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 {
2024-06-23 03:04:48 -05:00
static componentName = componentName;
2024-06-21 00:51:31 -05:00
}
);
if (!Components[componentName]) {
throw new Error(`Component ${componentName} decorator returned nothing`);
}
2024-06-14 15:18:55 -05:00
}
else {
2024-06-15 19:38:49 -05:00
Components[componentName] = class Component extends Arbitrary {
2024-06-23 03:04:48 -05:00
static componentName = componentName;
2024-06-14 15:18:55 -05:00
static schema = new Schema({
type: 'object',
2024-06-21 00:51:31 -05:00
properties: specificationOrDecorator,
2024-06-14 15:18:55 -05:00
});
2024-06-21 00:51:31 -05:00
}
2024-06-14 15:18:55 -05:00
}
}
export default Components;