feat: only send mergeDiff for EntityCreatePacket

This commit is contained in:
cha0s 2019-05-17 05:37:29 -05:00
parent f6bbc7f6c3
commit ba34ef6e01
4 changed files with 44 additions and 14 deletions

View File

@ -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 {

View File

@ -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 = [];
}

View File

@ -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);
});
},
};

View File

@ -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)
}