From 22568bd999db530f0f16a94fd657454b81c2aa4b Mon Sep 17 00:00:00 2001 From: cha0s Date: Thu, 15 Sep 2022 14:27:57 -0500 Subject: [PATCH] feat: component shorthand --- packages/ecs/src/component.js | 11 +++++++++++ packages/ecs/src/ecs.js | 3 ++- packages/ecs/test/ecs.js | 31 +++++++++++-------------------- 3 files changed, 24 insertions(+), 21 deletions(-) diff --git a/packages/ecs/src/component.js b/packages/ecs/src/component.js index 17f4e8a..7537139 100644 --- a/packages/ecs/src/component.js +++ b/packages/ecs/src/component.js @@ -18,3 +18,14 @@ export default function ComponentRouter() { } return new RealClass(schema); } + +ComponentRouter.normalize = (ComponentLike) => { + if (ComponentLike.prototype instanceof ComponentRouter) { + return ComponentLike; + } + return class AdhocComponent extends ComponentRouter { + + static schema = ComponentLike; + + }; +}; diff --git a/packages/ecs/src/ecs.js b/packages/ecs/src/ecs.js index 3ac0fbe..82a8fdc 100644 --- a/packages/ecs/src/ecs.js +++ b/packages/ecs/src/ecs.js @@ -1,4 +1,5 @@ /* eslint-disable guard-for-in, max-classes-per-file, no-continue, no-restricted-syntax */ +import Component from './component'; export default class Ecs { @@ -16,7 +17,7 @@ export default class Ecs { constructor(Components) { for (const i in Components) { - this.Components[i] = this.trackDirtyEntities(Components[i]); + this.Components[i] = this.trackDirtyEntities(Component.normalize(Components[i])); } } diff --git a/packages/ecs/test/ecs.js b/packages/ecs/test/ecs.js index 363c559..88ec4fc 100644 --- a/packages/ecs/test/ecs.js +++ b/packages/ecs/test/ecs.js @@ -1,21 +1,16 @@ /* eslint-disable react/prefer-stateless-function */ import {expect} from 'chai'; -import Component from '../src/component'; import Ecs from '../src/ecs'; import System from '../src/system'; -class Empty extends Component {} +const Empty = {}; -class Position extends Component { - - static schema = { - x: {type: 'int32', defaultValue: 32}, - y: 'int32', - z: 'int32', - }; - -} +const Position = { + x: {type: 'int32', defaultValue: 32}, + y: 'int32', + z: 'int32', +}; it('can create entities with components', () => { const ecs = new Ecs({Empty, Position}); @@ -25,15 +20,11 @@ it('can create entities with components', () => { }); it('can tick systems', () => { - class Momentum extends Component { - - static schema = { - x: 'int32', - y: 'int32', - z: 'int32', - }; - - } + const Momentum = { + x: 'int32', + y: 'int32', + z: 'int32', + }; const ecs = new Ecs({Momentum, Position}); class Physics extends System {