diff --git a/packages/topdown/src/resources/layers.js b/packages/topdown/src/resources/layers.js index f2033ae..28f0d16 100644 --- a/packages/topdown/src/resources/layers.js +++ b/packages/topdown/src/resources/layers.js @@ -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))); diff --git a/packages/topdown/src/resources/room.js b/packages/topdown/src/resources/room.js index 0094d39..647d318 100644 --- a/packages/topdown/src/resources/room.js +++ b/packages/topdown/src/resources/room.js @@ -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]); } }