refactor: createMany

This commit is contained in:
cha0s 2022-09-15 14:09:10 -05:00
parent ba938bc39d
commit 6429ae6e80
2 changed files with 26 additions and 18 deletions

View File

@ -41,7 +41,7 @@ export default class Ecs {
}
create(components = {}) {
const [entity] = this.createMany(1, components);
const [entity] = this.createMany([components]);
return entity;
}
@ -49,23 +49,31 @@ export default class Ecs {
this.createManyExact([entity, components]);
}
createMany(count, components) {
const componentKeys = Object.keys(components);
createMany(componentsList) {
const entities = [];
// eslint-disable-next-line no-param-reassign
while (this.$$pool.length > 0 && count--) {
const entity = this.$$pool.pop();
const creating = {};
for (let i = 0; i < componentsList.length; i++) {
const components = componentsList[i];
const componentKeys = Object.keys(components);
let entity;
if (this.$$pool.length > 0) {
entity = this.$$pool.pop();
}
else {
entity = BigInt(this.$$caret++);
}
entities.push(entity);
this.$$entities[entity] = componentKeys.slice(0);
for (let j = 0; j < componentKeys.length; j++) {
const component = componentKeys[j];
if (!creating[component]) {
creating[component] = [];
}
creating[component].push([entity, components[component]]);
}
}
// eslint-disable-next-line no-param-reassign
while (count-- > 0) {
const entity = BigInt(this.$$caret++);
entities.push(entity);
this.$$entities[entity] = componentKeys.slice(0);
}
for (const i in components) {
this.Components[i].createMany(entities.map((entity) => [entity, components[i](entity)]));
for (const i in creating) {
this.Components[i].createMany(creating[i]);
}
for (let i = 0; i < this.$$systems.length; i++) {
this.$$systems[i].reindex(entities);

View File

@ -1,10 +1,10 @@
/* eslint-disable react/prefer-stateless-function */
import {expect} from 'chai';
import Component from '../src/component';
import Ecs from '../src/ecs';
import System from '../src/system';
// eslint-disable-next-line
class Empty extends Component {}
class Position extends Component {
@ -19,7 +19,7 @@ class Position extends Component {
it('can create entities with components', () => {
const ecs = new Ecs({Empty, Position});
const entity = ecs.create({Empty: () => {}, Position: () => ({y: 420})});
const entity = ecs.create({Empty: {}, Position: {y: 420}});
expect(JSON.stringify(ecs.get(entity)))
.to.deep.equal(JSON.stringify({Empty: {}, Position: {x: 32, y: 420, z: 0}}));
});
@ -56,7 +56,7 @@ it('can tick systems', () => {
}
ecs.addSystem(Physics);
const entity = ecs.create({Momentum: () => ({}), Position: () => ({y: 420})});
const entity = ecs.create({Momentum: {}, Position: {y: 420}});
const position = JSON.stringify(ecs.get(entity).Position);
ecs.tick(1);
expect(JSON.stringify(ecs.get(entity).Position))
@ -106,7 +106,7 @@ it('can schedule entities to be deleted when ticking systems', () => {
it('can encode and decode an ecs', () => {
const ecs = new Ecs({Empty, Position});
const entity = ecs.create({Empty: () => {}, Position: () => ({y: 420})});
const entity = ecs.create({Empty: {}, Position: {y: 420}});
const view = new DataView(new ArrayBuffer(1024));
ecs.encode([[entity, true]], view);
const newEcs = new Ecs({Empty, Position});