refactor: decorated components

This commit is contained in:
cha0s 2024-06-21 00:51:31 -05:00
parent 9d6d6eb269
commit 493bf025f0
2 changed files with 18 additions and 11 deletions

View File

@ -3,24 +3,35 @@ import Base from '@/ecs/base.js';
import Schema from '@/ecs/schema.js';
import gather from '@/engine/gather.js';
const specificationsOrClasses = gather(
const specificationsAndOrDecorators = gather(
import.meta.glob('./*.js', {eager: true, import: 'default'}),
);
const Components = {};
for (const componentName in specificationsOrClasses) {
const specificationOrClass = specificationsOrClasses[componentName];
if (specificationOrClass instanceof Base) {
Components[componentName] = specificationOrClass;
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: specificationOrClass,
properties: specificationOrDecorator,
});
};
}
}
}

View File

@ -60,10 +60,6 @@ export default class Base {
}
}
static gathered(id, key) {
this.name = key;
}
insertMany(entities) {
const creating = [];
for (let i = 0; i < entities.length; i++) {