feat: component shorthand

This commit is contained in:
cha0s 2022-09-15 14:27:57 -05:00
parent 6429ae6e80
commit 22568bd999
3 changed files with 24 additions and 21 deletions

View File

@ -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;
};
};

View File

@ -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]));
}
}

View File

@ -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 = {
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 = {
const Momentum = {
x: 'int32',
y: 'int32',
z: 'int32',
};
}
const ecs = new Ecs({Momentum, Position});
class Physics extends System {