feat: Entity::mergeDiff

This commit is contained in:
cha0s 2019-05-17 04:33:52 -05:00
parent efb5c75f98
commit b591352d95

View File

@ -1,8 +1,13 @@
import * as I from 'immutable';
import D from 'debug';
import without from 'lodash.without';
import {compose, EventEmitter, fastApply} from '@avocado/core';
import {
compose,
EventEmitter,
fastApply,
merge,
mergeDiff,
} from '@avocado/core';
import {Resource} from '@avocado/resource';
import {Synchronized} from '@avocado/state';
@ -85,9 +90,25 @@ let numericUid = 1;
export class Entity extends decorate(Resource) {
constructor(json) {
static jsonWithDefaults(json) {
if ('undefined' === typeof json) {
return;
}
const pristine = JSON.parse(JSON.stringify(json));
for (const type in json.traits) {
if (!hasTrait(type)) {
continue;
}
const Trait = lookupTrait(type);
pristine.traits[type] = merge({}, Trait.defaultJSON(), json.traits[type]);
}
return pristine;
}
constructor(json, jsonext) {
super();
this._hooks = {};
this._json = Entity.jsonWithDefaults(json);
this._traits = {};
this._traitsFlat = [];
this._traitTickers = [];
@ -104,7 +125,7 @@ export class Entity extends decorate(Resource) {
this.visibleAabb = [0, 0, 0, 0];
// Fast path for instance.
if ('undefined' !== typeof json) {
this.fromJSON(json);
this.fromJSON(merge({}, json, jsonext));
}
}
@ -228,6 +249,10 @@ export class Entity extends decorate(Resource) {
return type in this._traits;
}
mergeDiff() {
return mergeDiff(this._json, this.toJSON());
}
packetsForUpdate(force = false) {
const packets = [];
if (force) {