fix: insertMany

This commit is contained in:
cha0s 2022-09-17 07:25:54 -05:00
parent 2ffad2d86f
commit 91b649a4b0
3 changed files with 27 additions and 8 deletions

View File

@ -61,6 +61,20 @@ export default class BaseComponent {
}
}
insertMany(entities) {
const creating = [];
for (let i = 0; i < entities.length; i++) {
const [entity, values] = entities[i];
if (!this.getUnsafe(entity)) {
creating.push([entity, values]);
}
else {
this.set(entity, values);
}
}
this.createMany(creating);
}
set(entity, values) {
const instance = this.getUnsafe(entity);
for (const i in values) {

View File

@ -292,19 +292,13 @@ export default class Ecs {
for (const i in components) {
const entities = components[i];
for (let j = 0; j < entities.length; ++j) {
let entity;
if (Array.isArray(entities[j])) {
[entity] = entities[j];
}
else {
entity = entities[j];
}
const [entity] = entities[j];
if (!this.$$entities[entity].includes(i)) {
this.$$entities[entity].push(i);
}
unique.add(entity);
}
this.Components[i].createMany(entities);
this.Components[i].insertMany(entities);
}
const uniqueArray = Array.from(unique.values());
for (let i = 0; i < this.$$systems.length; i++) {

View File

@ -20,6 +20,17 @@ it('can create entities with components', () => {
.to.deep.equal(JSON.stringify({Empty: {}, Position: {x: 32, y: 420, z: 0}}));
});
it('can insert components into entities', () => {
const ecs = new Ecs({Empty, Position});
const entity = ecs.create({Empty: {}});
ecs.insert(entity, {Position: {y: 420}});
expect(JSON.stringify(ecs.get(entity)))
.to.deep.equal(JSON.stringify({Empty: {}, Position: {x: 32, y: 420, z: 0}}));
ecs.insert(entity, {Position: {y: 69}});
expect(JSON.stringify(ecs.get(entity)))
.to.deep.equal(JSON.stringify({Empty: {}, Position: {x: 32, y: 69, z: 0}}));
});
it('can tick systems', () => {
const Momentum = {
x: 'int32',