refactor: get
This commit is contained in:
parent
91b649a4b0
commit
ee4de8ac60
|
@ -265,15 +265,30 @@ export default class Ecs {
|
||||||
view.setUint32(0, entitiesWritten, true);
|
view.setUint32(0, entitiesWritten, true);
|
||||||
}
|
}
|
||||||
|
|
||||||
get(entity, Components = Object.keys(this.Components)) {
|
get(entity) {
|
||||||
if (!this.$$entities[entity]) {
|
if (!this.$$entities[entity]) {
|
||||||
return undefined;
|
return undefined;
|
||||||
}
|
}
|
||||||
const result = {};
|
const result = {};
|
||||||
for (let i = 0; i < Components.length; i++) {
|
for (let i = 0; i < this.$$entities[entity].length; i++) {
|
||||||
const component = this.Components[Components[i]].get(entity);
|
const component = this.Components[this.$$entities[entity][i]].get(entity);
|
||||||
if ('undefined' !== typeof component) {
|
if ('undefined' !== typeof component) {
|
||||||
result[Components[i]] = component;
|
result[this.$$entities[entity][i]] = component;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
getUnsafe(entity) {
|
||||||
|
if (!this.$$entities[entity]) {
|
||||||
|
return undefined;
|
||||||
|
}
|
||||||
|
const result = {};
|
||||||
|
const components = this.$$entities[entity];
|
||||||
|
for (let i = 0; i < components.length; i++) {
|
||||||
|
const instance = this.Components[components[i]].getUnsafe(entity);
|
||||||
|
if ('undefined' !== typeof instance) {
|
||||||
|
result[components[i]] = instance;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return result;
|
return result;
|
||||||
|
|
|
@ -20,6 +20,13 @@ it('can create entities with components', () => {
|
||||||
.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}}));
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('can get entities unsafely', () => {
|
||||||
|
const ecs = new Ecs({Empty, Position});
|
||||||
|
const entity = ecs.create({Empty: {}, Position: {y: 420}});
|
||||||
|
expect(JSON.stringify(ecs.getUnsafe(entity)))
|
||||||
|
.to.deep.equal(JSON.stringify({Empty: {}, Position: {x: 32, y: 420, z: 0}}));
|
||||||
|
});
|
||||||
|
|
||||||
it('can insert components into entities', () => {
|
it('can insert components into entities', () => {
|
||||||
const ecs = new Ecs({Empty, Position});
|
const ecs = new Ecs({Empty, Position});
|
||||||
const entity = ecs.create({Empty: {}});
|
const entity = ecs.create({Empty: {}});
|
||||||
|
|
Loading…
Reference in New Issue
Block a user