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); 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 */ /* eslint-disable guard-for-in, max-classes-per-file, no-continue, no-restricted-syntax */
import Component from './component';
export default class Ecs { export default class Ecs {
@ -16,7 +17,7 @@ export default class Ecs {
constructor(Components) { constructor(Components) {
for (const i in 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 */ /* eslint-disable react/prefer-stateless-function */
import {expect} from 'chai'; import {expect} from 'chai';
import Component from '../src/component';
import Ecs from '../src/ecs'; import Ecs from '../src/ecs';
import System from '../src/system'; import System from '../src/system';
class Empty extends Component {} const Empty = {};
class Position extends Component { const Position = {
x: {type: 'int32', defaultValue: 32},
static schema = { y: 'int32',
x: {type: 'int32', defaultValue: 32}, z: 'int32',
y: 'int32', };
z: 'int32',
};
}
it('can create entities with components', () => { it('can create entities with components', () => {
const ecs = new Ecs({Empty, Position}); const ecs = new Ecs({Empty, Position});
@ -25,15 +20,11 @@ it('can create entities with components', () => {
}); });
it('can tick systems', () => { it('can tick systems', () => {
class Momentum extends Component { const Momentum = {
x: 'int32',
static schema = { y: 'int32',
x: 'int32', z: 'int32',
y: 'int32', };
z: 'int32',
};
}
const ecs = new Ecs({Momentum, Position}); const ecs = new Ecs({Momentum, Position});
class Physics extends System { class Physics extends System {