refactor: allEntities

This commit is contained in:
cha0s 2021-01-18 08:12:07 -06:00
parent afa9c45d78
commit 077f0036c8
2 changed files with 22 additions and 21 deletions

View File

@ -41,17 +41,6 @@ export default (latus) => class Layers extends decorate(JsonResource) {
this.emit('layerAdded', layer);
}
allEntities() {
const allEntities = [];
for (let i = 0; i < this.layers.length; i++) {
const layerEntities = Object.values(this.layers[i].entities);
for (let j = 0; j < layerEntities.length; j++) {
allEntities.push(layerEntities[j]);
}
}
return allEntities;
}
cleanPackets() {
for (let i = 0; i < this.layers.length; i++) {
this.layers[i].cleanPackets();
@ -66,6 +55,18 @@ export default (latus) => class Layers extends decorate(JsonResource) {
}
}
get entities() {
const entities = {};
for (let i = 0; i < this.layers.length; i++) {
const layerEntities = Object.entries(this.layers[i].entities);
for (let j = 0; j < layerEntities.length; j++) {
const [uuid, entity] = layerEntities[j];
entities[uuid] = entity;
}
}
return entities;
}
static async extendJson(json) {
const {fromResourceType: {Layer}} = resource(latus);
return Promise.all(json.map((layer) => Layer.load(layer)));

View File

@ -51,10 +51,6 @@ export default (latus) => class Room extends decorate(JsonResource) {
this.layers.addEntityToLayer(entity, layerIndex);
}
allEntities() {
return this.layers.allEntities();
}
cleanPackets() {
super.cleanPackets();
this.layers.cleanPackets();
@ -68,6 +64,10 @@ export default (latus) => class Room extends decorate(JsonResource) {
this.off('worldChanged', this.onWorldChanged);
}
get entities() {
return this.layers.entities();
}
static async extendJson(json) {
const extended = await super.extendJson(json);
const {fromResourceType: {Layers}} = resource(latus);
@ -147,9 +147,9 @@ export default (latus) => class Room extends decorate(JsonResource) {
setLayers(layers) {
if (this.layers) {
const allEntities = this.layers.allEntities();
for (let i = 0; i < allEntities.length; i++) {
this.onEntityRemovedFromRoom(allEntities[i]);
const entities = Object.values(this.layers.entities);
for (let i = 0; i < entities.length; i++) {
this.onEntityRemovedFromRoom(entities[i]);
}
this.layers.off('layerAdded', this.onLayerAdded);
this.layers.off('layerRemoved', this.onLayerRemoved);
@ -167,9 +167,9 @@ export default (latus) => class Room extends decorate(JsonResource) {
this.layers.on('entityAdded', this.onEntityAddedToRoom, this);
this.layers.on('layerRemoved', this.onLayerRemoved, this);
this.layers.on('layerAdded', this.onLayerAdded, this);
const allEntities = this.layers.allEntities();
for (let i = 0; i < allEntities.length; i++) {
this.onEntityAddedToRoom(allEntities[i]);
const entities = Object.values(this.layers.entities);
for (let i = 0; i < entities.length; i++) {
this.onEntityAddedToRoom(entities[i]);
}
}