refactor: createMany
This commit is contained in:
parent
ba938bc39d
commit
6429ae6e80
|
@ -41,7 +41,7 @@ export default class Ecs {
|
||||||
}
|
}
|
||||||
|
|
||||||
create(components = {}) {
|
create(components = {}) {
|
||||||
const [entity] = this.createMany(1, components);
|
const [entity] = this.createMany([components]);
|
||||||
return entity;
|
return entity;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -49,23 +49,31 @@ export default class Ecs {
|
||||||
this.createManyExact([entity, components]);
|
this.createManyExact([entity, components]);
|
||||||
}
|
}
|
||||||
|
|
||||||
createMany(count, components) {
|
createMany(componentsList) {
|
||||||
const componentKeys = Object.keys(components);
|
|
||||||
const entities = [];
|
const entities = [];
|
||||||
// eslint-disable-next-line no-param-reassign
|
const creating = {};
|
||||||
while (this.$$pool.length > 0 && count--) {
|
for (let i = 0; i < componentsList.length; i++) {
|
||||||
const entity = this.$$pool.pop();
|
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);
|
entities.push(entity);
|
||||||
this.$$entities[entity] = componentKeys.slice(0);
|
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
|
for (const i in creating) {
|
||||||
while (count-- > 0) {
|
this.Components[i].createMany(creating[i]);
|
||||||
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 (let i = 0; i < this.$$systems.length; i++) {
|
for (let i = 0; i < this.$$systems.length; i++) {
|
||||||
this.$$systems[i].reindex(entities);
|
this.$$systems[i].reindex(entities);
|
||||||
|
|
|
@ -1,10 +1,10 @@
|
||||||
|
/* eslint-disable react/prefer-stateless-function */
|
||||||
import {expect} from 'chai';
|
import {expect} from 'chai';
|
||||||
|
|
||||||
import Component from '../src/component';
|
import Component from '../src/component';
|
||||||
import Ecs from '../src/ecs';
|
import Ecs from '../src/ecs';
|
||||||
import System from '../src/system';
|
import System from '../src/system';
|
||||||
|
|
||||||
// eslint-disable-next-line
|
|
||||||
class Empty extends Component {}
|
class Empty extends Component {}
|
||||||
|
|
||||||
class Position extends Component {
|
class Position extends Component {
|
||||||
|
@ -19,7 +19,7 @@ class Position extends Component {
|
||||||
|
|
||||||
it('can create entities with components', () => {
|
it('can create entities with components', () => {
|
||||||
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}});
|
||||||
expect(JSON.stringify(ecs.get(entity)))
|
expect(JSON.stringify(ecs.get(entity)))
|
||||||
.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}}));
|
||||||
});
|
});
|
||||||
|
@ -56,7 +56,7 @@ it('can tick systems', () => {
|
||||||
|
|
||||||
}
|
}
|
||||||
ecs.addSystem(Physics);
|
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);
|
const position = JSON.stringify(ecs.get(entity).Position);
|
||||||
ecs.tick(1);
|
ecs.tick(1);
|
||||||
expect(JSON.stringify(ecs.get(entity).Position))
|
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', () => {
|
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}});
|
||||||
const view = new DataView(new ArrayBuffer(1024));
|
const view = new DataView(new ArrayBuffer(1024));
|
||||||
ecs.encode([[entity, true]], view);
|
ecs.encode([[entity, true]], view);
|
||||||
const newEcs = new Ecs({Empty, Position});
|
const newEcs = new Ecs({Empty, Position});
|
||||||
|
|
Loading…
Reference in New Issue
Block a user