feat: add/remove system-like
This commit is contained in:
parent
ba533f6f4d
commit
f7e39c8820
|
@ -1,6 +1,7 @@
|
||||||
/* 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 Bundle from './bundle';
|
import Bundle from './bundle';
|
||||||
import Component from './component';
|
import Component from './component';
|
||||||
|
import System from './system';
|
||||||
|
|
||||||
export default class Ecs {
|
export default class Ecs {
|
||||||
|
|
||||||
|
@ -36,9 +37,9 @@ export default class Ecs {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
addSystem(System) {
|
addSystem(source) {
|
||||||
const ecs = this;
|
const ecs = this;
|
||||||
class WrappedSystem extends System {
|
class WrappedSystem extends System.normalize(source) {
|
||||||
|
|
||||||
constructor(Components) {
|
constructor(Components) {
|
||||||
super(Components);
|
super(Components);
|
||||||
|
@ -53,10 +54,15 @@ export default class Ecs {
|
||||||
return this.ecs.createMany(count, components);
|
return this.ecs.createMany(count, components);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// eslint-disable-next-line class-methods-use-this
|
||||||
|
get source() {
|
||||||
|
return source;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
const system = new WrappedSystem(this.Components);
|
const wrappedSystem = new WrappedSystem(this.Components);
|
||||||
this.$$systems.push(system);
|
wrappedSystem.reindex(Object.keys(this.$$entities));
|
||||||
return system;
|
this.$$systems.push(wrappedSystem);
|
||||||
}
|
}
|
||||||
|
|
||||||
create(components = {}) {
|
create(components = {}) {
|
||||||
|
@ -321,6 +327,13 @@ export default class Ecs {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
removeSystem(SystemLike) {
|
||||||
|
const index = this.$$systems.findIndex((system) => SystemLike === system.source);
|
||||||
|
if (-1 !== index) {
|
||||||
|
this.$$systems.splice(index, 1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
setClean() {
|
setClean() {
|
||||||
for (const i in this.Components) {
|
for (const i in this.Components) {
|
||||||
this.Components[i].setClean();
|
this.Components[i].setClean();
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
/* eslint-disable guard-for-in, no-restricted-syntax */
|
/* eslint-disable guard-for-in, max-classes-per-file, no-restricted-syntax */
|
||||||
|
|
||||||
import Query from './query';
|
import Query from './query';
|
||||||
|
|
||||||
|
@ -31,6 +31,18 @@ export default class System {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static normalize(SystemLike) {
|
||||||
|
if (SystemLike.prototype instanceof System) {
|
||||||
|
return SystemLike;
|
||||||
|
}
|
||||||
|
if ('function' === typeof SystemLike) {
|
||||||
|
class TickingSystem extends System {}
|
||||||
|
TickingSystem.prototype.tick = SystemLike;
|
||||||
|
return TickingSystem;
|
||||||
|
}
|
||||||
|
return undefined;
|
||||||
|
}
|
||||||
|
|
||||||
static queries() {
|
static queries() {
|
||||||
return {};
|
return {};
|
||||||
}
|
}
|
||||||
|
|
|
@ -13,6 +13,34 @@ const Position = {
|
||||||
z: 'int32',
|
z: 'int32',
|
||||||
};
|
};
|
||||||
|
|
||||||
|
it('can add and remove systems at runtime', () => {
|
||||||
|
const ecs = new Ecs();
|
||||||
|
let oneCount = 0;
|
||||||
|
let twoCount = 0;
|
||||||
|
const oneSystem = () => {
|
||||||
|
oneCount++;
|
||||||
|
};
|
||||||
|
ecs.addSystem(oneSystem);
|
||||||
|
ecs.tick();
|
||||||
|
expect(oneCount)
|
||||||
|
.to.equal(1);
|
||||||
|
const twoSystem = () => {
|
||||||
|
twoCount++;
|
||||||
|
};
|
||||||
|
ecs.addSystem(twoSystem);
|
||||||
|
ecs.tick();
|
||||||
|
expect(oneCount)
|
||||||
|
.to.equal(2);
|
||||||
|
expect(twoCount)
|
||||||
|
.to.equal(1);
|
||||||
|
ecs.removeSystem(oneSystem);
|
||||||
|
ecs.tick();
|
||||||
|
expect(oneCount)
|
||||||
|
.to.equal(2);
|
||||||
|
expect(twoCount)
|
||||||
|
.to.equal(2);
|
||||||
|
});
|
||||||
|
|
||||||
it('can create entities with components', () => {
|
it('can create entities with components', () => {
|
||||||
const ecs = new Ecs({Empty, Position});
|
const ecs = new Ecs({Empty, Position});
|
||||||
const entity = ecs.create({Empty: {}, Position: {y: 420}});
|
const entity = ecs.create({Empty: {}, Position: {y: 420}});
|
||||||
|
|
Loading…
Reference in New Issue
Block a user