fix: insertMany
This commit is contained in:
parent
2ffad2d86f
commit
91b649a4b0
|
@ -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) {
|
||||
|
|
|
@ -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++) {
|
||||
|
|
|
@ -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',
|
||||
|
|
Loading…
Reference in New Issue
Block a user