From 493bf025f0ca61ffb6e7087adc819db1279dfeb5 Mon Sep 17 00:00:00 2001 From: cha0s Date: Fri, 21 Jun 2024 00:51:31 -0500 Subject: [PATCH] refactor: decorated components --- app/ecs-components/index.js | 25 ++++++++++++++++++------- app/ecs/base.js | 4 ---- 2 files changed, 18 insertions(+), 11 deletions(-) diff --git a/app/ecs-components/index.js b/app/ecs-components/index.js index 362857a..4b8e3fe 100644 --- a/app/ecs-components/index.js +++ b/app/ecs-components/index.js @@ -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, }); - }; + } } } diff --git a/app/ecs/base.js b/app/ecs/base.js index f3140d8..02d79b1 100644 --- a/app/ecs/base.js +++ b/app/ecs/base.js @@ -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++) {