diff --git a/packages/entity/index.js b/packages/entity/index.js index 4c4e3b1..999e5e7 100644 --- a/packages/entity/index.js +++ b/packages/entity/index.js @@ -105,6 +105,17 @@ export class Entity extends decorate(Resource) { return pristine; } + static loadOrInstance(json) { + if (json.uri) { + return Entity.read(json.uri).then((entityJSON) => { + return new Entity(entityJSON, json); + }); + } + else { + return Promise.resolve(new Entity(json)); + } + } + constructor(json, jsonext) { super(); this._hooks = {}; @@ -259,7 +270,7 @@ export class Entity extends decorate(Resource) { packetsForUpdate(force = false) { const packets = []; if (force) { - const packet = new EntityCreatePacket(this.toJSON(), this); + const packet = new EntityCreatePacket(this.mergeDiff(), this); packets.push(packet); } else { diff --git a/packages/entity/list/index.js b/packages/entity/list/index.js index 50ffc10..546144b 100644 --- a/packages/entity/list/index.js +++ b/packages/entity/list/index.js @@ -34,9 +34,10 @@ export class EntityList extends decorate(class {}) { acceptPacket(packet) { if (packet instanceof EntityCreatePacket) { if (!this.findEntity(packet.data.uuid)) { - const entity = new Entity(packet.data); - entity.instanceUuid = packet.data.uuid; - this.addEntity(entity); + Entity.loadOrInstance(packet.data).then((entity) => { + entity.instanceUuid = packet.data.uuid; + this.addEntity(entity); + }); } } } @@ -94,7 +95,7 @@ export class EntityList extends decorate(class {}) { if (!force) { for (let i = 0; i < this._entitiesJustAdded.length; i++) { const entity = this._entitiesJustAdded[i]; - packets.push(new EntityCreatePacket(entity.toJSON(), entity)); + packets.push(new EntityCreatePacket(entity.mergeDiff(), entity)); } this._entitiesJustAdded = []; } diff --git a/packages/entity/traits/spawner.trait.js b/packages/entity/traits/spawner.trait.js index aa444ad..b105c39 100644 --- a/packages/entity/traits/spawner.trait.js +++ b/packages/entity/traits/spawner.trait.js @@ -1,4 +1,4 @@ -import {compose} from '@avocado/core'; +import {compose, merge} from '@avocado/core'; import {Vector} from '@avocado/math'; import {StateProperty, Trait} from '../trait'; @@ -70,14 +70,15 @@ export class Spawner extends decorate(Trait) { if (!spawnJSON) { return; } - const spawn = new Entity(spawnJSON, json); - this.children.push(spawn); - spawn.once('destroy', () => { - const index = this.children.indexOf(spawn); - this.children.splice(index, 1); - }) const list = this.entity.list; - list.addEntity(spawn); + Entity.loadOrInstance(merge(spawnJSON, json)).then((spawn) => { + this.children.push(spawn); + spawn.once('destroy', () => { + const index = this.children.indexOf(spawn); + this.children.splice(index, 1); + }) + list.addEntity(spawn); + }); }, }; diff --git a/packages/topdown/room.js b/packages/topdown/room.js index b3e71ed..da3626f 100644 --- a/packages/topdown/room.js +++ b/packages/topdown/room.js @@ -32,6 +32,7 @@ export class Room extends decorate(class {}) { super(); this.bounds = []; this.layers = new Layers(); + this.queuedEntityPackets = {}; // Listeners. this.layers.on('entityAdded', this.onEntityAddedToRoom, this); this.layers.on('entityRemoved', this.onEntityRemovedFromRoom, this); @@ -41,10 +42,13 @@ export class Room extends decorate(class {}) { } acceptPacket(packet) { + if (packet instanceof EntityCreatePacket) { + this.queuedEntityPackets[packet.data.uuid] = []; + this.layers.acceptPacket(packet); + } if ( packet instanceof LayerCreatePacket || packet instanceof TileUpdatePacket - || packet instanceof EntityCreatePacket ) { this.layers.acceptPacket(packet); } @@ -60,6 +64,12 @@ export class Room extends decorate(class {}) { if (entity) { entity.acceptPacket(packet); } + else { + const queuedPackets = this.queuedEntityPackets[packet.data.uuid]; + if (queuedPackets) { + queuedPackets.push(packet); + } + } } if (packet instanceof RoomSizeUpdatePacket) { const x = packet.data & 0xFFFF; @@ -103,6 +113,13 @@ export class Room extends decorate(class {}) { onEntityAddedToRoom(entity) { entity.room = this; + if (AVOCADO_CLIENT) { + const queuedPackets = this.queuedEntityPackets[entity.instanceUuid]; + for (let i = 0; i < queuedPackets.length; i++) { + entity.acceptPacket(queuedPackets[i]); + } + this.queuedEntityPackets[entity.instanceUuid] = undefined; + } this.emit('entityAdded', entity) }