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 Schema from '@/ecs/schema.js';
import gather from '@/engine/gather.js'; import gather from '@/engine/gather.js';
const specificationsOrClasses = gather( const specificationsAndOrDecorators = gather(
import.meta.glob('./*.js', {eager: true, import: 'default'}), import.meta.glob('./*.js', {eager: true, import: 'default'}),
); );
const Components = {}; const Components = {};
for (const componentName in specificationsOrClasses) { for (const componentName in specificationsAndOrDecorators) {
const specificationOrClass = specificationsOrClasses[componentName]; // TODO: byKey, byId, ...
if (specificationOrClass instanceof Base) { if (Number.isInteger(+componentName)) {
Components[componentName] = specificationOrClass; 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 { else {
Components[componentName] = class Component extends Arbitrary { Components[componentName] = class Component extends Arbitrary {
static name = componentName; static name = componentName;
static schema = new Schema({ static schema = new Schema({
type: 'object', 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) { insertMany(entities) {
const creating = []; const creating = [];
for (let i = 0; i < entities.length; i++) { for (let i = 0; i < entities.length; i++) {