From 009e69ffd6f310240b00be585141ba27cb450483 Mon Sep 17 00:00:00 2001 From: cha0s Date: Thu, 16 May 2019 23:01:57 -0500 Subject: [PATCH] feat: loaded entities for list --- TODO.md | 2 +- packages/entity/list/index.js | 16 ++++++++++++++++ packages/topdown/layer.js | 4 +--- 3 files changed, 18 insertions(+), 4 deletions(-) diff --git a/TODO.md b/TODO.md index 7d1c9db..9d89029 100644 --- a/TODO.md +++ b/TODO.md @@ -17,7 +17,7 @@ - ✔ EventEmitter::emit is too hot - ✔ Entity packets - ✔ Decouple input normalization from action registry -- ❌ entityList.fromJSON() +- ✔ entityList.fromJSON() - ❌ Socket WebWorker can't connect in Firefox - ✔ Don't run emitter destruction tickers on server - ✔ Investigate unrolling equalsClose diff --git a/packages/entity/list/index.js b/packages/entity/list/index.js index f89b76e..00a4d69 100644 --- a/packages/entity/list/index.js +++ b/packages/entity/list/index.js @@ -1,3 +1,5 @@ +import merge from 'lodash.merge'; + import {compose, EventEmitter} from '@avocado/core'; import {QuadTree, Rectangle, Vector} from '@avocado/math'; import {Synchronized} from '@avocado/state'; @@ -75,6 +77,20 @@ export class EntityList extends decorate(class {}) { } } + fromJSON(json) { + for (let i = 0; i < json.length; i++) { + const entityJSON = json[i]; + if ('undefined' !== entityJSON.uri) { + Entity.read(entityJSON.uri).then((readJSON) => { + this.addEntity(new Entity(merge(readJSON, entityJSON))); + }); + } + else { + this.addEntity(new Entity(json[i])); + } + } + } + packetsForUpdate(force = false) { const packets = []; if (!force) { diff --git a/packages/topdown/layer.js b/packages/topdown/layer.js index f6924bf..a690d0d 100644 --- a/packages/topdown/layer.js +++ b/packages/topdown/layer.js @@ -104,9 +104,7 @@ export class Layer extends decorate(class {}) { fromJSON(json) { if (json.entities) { - for (let i = 0; i < json.entities.length; i++) { - this.entityList.addEntity(new Entity(json.entities[i])); - } + this.entityList.fromJSON(json.entities); } if (json.tiles) { this.tiles.fromJSON(json.tiles)