feat: component add/remove

This commit is contained in:
cha0s 2022-09-17 09:09:59 -05:00
parent f7e39c8820
commit 689ce1e52b
2 changed files with 78 additions and 0 deletions

View File

@ -59,6 +59,22 @@ export default class Ecs {
return source; return source;
} }
insertComponents(entity, components) {
this.ecs.insert(entity, components);
}
insertManyComponents(components) {
this.ecs.insertMany(components);
}
removeComponents(entity, components) {
this.ecs.remove(entity, components);
}
removeManyComponents(entities) {
this.ecs.removeMany(entities);
}
} }
const wrappedSystem = new WrappedSystem(this.Components); const wrappedSystem = new WrappedSystem(this.Components);
wrappedSystem.reindex(Object.keys(this.$$entities)); wrappedSystem.reindex(Object.keys(this.$$entities));
@ -192,6 +208,10 @@ export default class Ecs {
} }
} }
destroy(entity) {
this.destroyMany([entity]);
}
destroyAll() { destroyAll() {
this.destroyMany(Object.keys(this.$$entities).map((entity) => BigInt(entity))); this.destroyMany(Object.keys(this.$$entities).map((entity) => BigInt(entity)));
} }
@ -327,6 +347,27 @@ export default class Ecs {
} }
} }
remove(entity, components) {
this.removeMany([[entity, components]]);
}
removeMany(entities) {
const removing = {};
for (let i = 0; i < entities.length; i++) {
const [entity, components] = entities[i];
for (let j = 0; j < components.length; j++) {
const component = components[j];
if (!removing[component]) {
removing[component] = [];
}
removing[component].push(entity);
}
}
for (const component in removing) {
this.Components[component].destroyMany(removing[component]);
}
}
removeSystem(SystemLike) { removeSystem(SystemLike) {
const index = this.$$systems.findIndex((system) => SystemLike === system.source); const index = this.$$systems.findIndex((system) => SystemLike === system.source);
if (-1 !== index) { if (-1 !== index) {

View File

@ -48,6 +48,14 @@ it('can create entities with components', () => {
.to.deep.equal(JSON.stringify({Empty: {}, Position: {x: 32, y: 420, z: 0}})); .to.deep.equal(JSON.stringify({Empty: {}, Position: {x: 32, y: 420, z: 0}}));
}); });
it("can remove entities' components", () => {
const ecs = new Ecs({Empty, Position});
const entity = ecs.create({Empty: {}, Position: {y: 420}});
ecs.remove(entity, ['Position']);
expect(JSON.stringify(ecs.get(entity)))
.to.deep.equal(JSON.stringify({Empty: {}}));
});
it('can get entities unsafely', () => { it('can get entities unsafely', () => {
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}});
@ -142,6 +150,35 @@ it('can schedule entities to be deleted when ticking systems', () => {
.to.be.undefined; .to.be.undefined;
}); });
it('can add components to and remove components from entities when ticking systems', () => {
const Foo = {bar: 'uint8'};
const ecs = new Ecs({Foo});
class AddComponent extends System {
tick() {
this.insertComponents(1n, {Foo: {}});
}
}
class RemoveComponent extends System {
tick() {
this.removeComponents(1n, ['Foo']);
}
}
ecs.addSystem(AddComponent);
ecs.createExact(1);
ecs.tick(1);
expect(ecs.get(1).Foo)
.to.not.be.undefined;
ecs.removeSystem(AddComponent);
ecs.addSystem(RemoveComponent);
ecs.tick(1);
expect(ecs.get(1).Foo)
.to.be.undefined;
});
it('can encode and decode an ecs', () => { it('can encode and decode an ecs', () => {
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}});