From 6429ae6e8080fcccd7c8d88bd642f6ec28f049c4 Mon Sep 17 00:00:00 2001 From: cha0s Date: Thu, 15 Sep 2022 14:09:10 -0500 Subject: [PATCH] refactor: createMany --- packages/ecs/src/ecs.js | 36 ++++++++++++++++++++++-------------- packages/ecs/test/ecs.js | 8 ++++---- 2 files changed, 26 insertions(+), 18 deletions(-) diff --git a/packages/ecs/src/ecs.js b/packages/ecs/src/ecs.js index 8dadfca..3ac0fbe 100644 --- a/packages/ecs/src/ecs.js +++ b/packages/ecs/src/ecs.js @@ -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); diff --git a/packages/ecs/test/ecs.js b/packages/ecs/test/ecs.js index 8d8fee7..363c559 100644 --- a/packages/ecs/test/ecs.js +++ b/packages/ecs/test/ecs.js @@ -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});