chore: acceptStateChange -> patchState
This commit is contained in:
parent
e7c6cb8556
commit
8f3a8d9513
|
@ -53,10 +53,6 @@ class Entity extends decorate(Resource) {
|
|||
this.traits_PRIVATE = new Traits(createProxy(this));
|
||||
}
|
||||
|
||||
acceptStateChange(change) {
|
||||
this.traits_PRIVATE.acceptStateChange(change);
|
||||
}
|
||||
|
||||
addTrait(type, trait) {
|
||||
this.traits_PRIVATE.addTrait(type, trait);
|
||||
}
|
||||
|
@ -97,6 +93,10 @@ class Entity extends decorate(Resource) {
|
|||
return this.traits_PRIVATE.invokeHookFlat(hook, ...args);
|
||||
}
|
||||
|
||||
patchState(patch) {
|
||||
this.traits_PRIVATE.patchState(patch);
|
||||
}
|
||||
|
||||
removeAllTraits() {
|
||||
const types = this.traits_PRIVATE.allTypes();
|
||||
this.removeTraits(types);
|
||||
|
|
|
@ -28,31 +28,6 @@ export class EntityList extends decorate(class {}) {
|
|||
}
|
||||
}
|
||||
|
||||
acceptStateChange(change) {
|
||||
for (const uuid in change) {
|
||||
const localUuid = this.uuidMap_PRIVATE[uuid];
|
||||
const entity = this.entities_PRIVATE[localUuid];
|
||||
if (entity) {
|
||||
if (false === change[uuid]) {
|
||||
// Entity removed.
|
||||
this.removeEntity(entity);
|
||||
}
|
||||
else {
|
||||
entity.acceptStateChange(change[uuid]);
|
||||
this.state_PRIVATE = this.state_PRIVATE.set(localUuid, entity.state);
|
||||
}
|
||||
}
|
||||
else {
|
||||
// New entity. Create with change as traits.
|
||||
const newEntity = create().fromJSON({
|
||||
traits: change[uuid],
|
||||
});
|
||||
this.uuidMap_PRIVATE[uuid] = newEntity.instanceUuid;
|
||||
this.addEntity(newEntity);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
addEntity(entity) {
|
||||
const uuid = entity.instanceUuid;
|
||||
this.entities_PRIVATE[uuid] = entity;
|
||||
|
@ -80,6 +55,31 @@ export class EntityList extends decorate(class {}) {
|
|||
}
|
||||
}
|
||||
|
||||
patchState(patch) {
|
||||
for (const uuid in patch) {
|
||||
const localUuid = this.uuidMap_PRIVATE[uuid];
|
||||
const entity = this.entities_PRIVATE[localUuid];
|
||||
if (entity) {
|
||||
if (false === patch[uuid]) {
|
||||
// Entity removed.
|
||||
this.removeEntity(entity);
|
||||
}
|
||||
else {
|
||||
entity.patchState(patch[uuid]);
|
||||
this.state_PRIVATE = this.state_PRIVATE.set(localUuid, entity.state);
|
||||
}
|
||||
}
|
||||
else {
|
||||
// New entity. Create with patch as traits.
|
||||
const newEntity = create().fromJSON({
|
||||
traits: patch[uuid],
|
||||
});
|
||||
this.uuidMap_PRIVATE[uuid] = newEntity.instanceUuid;
|
||||
this.addEntity(newEntity);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
get quadTree() {
|
||||
return this._quadTree;
|
||||
}
|
||||
|
|
|
@ -13,23 +13,6 @@ export class Trait {
|
|||
this.state = I.fromJS(ctor.defaultState()).merge(I.fromJS(state));
|
||||
}
|
||||
|
||||
acceptStateChange(change) {
|
||||
if (!change.state) {
|
||||
return;
|
||||
}
|
||||
const undefinedProperties = {};
|
||||
for (const key in change.state) {
|
||||
const value = change.state[key];
|
||||
if (key in this.entity) {
|
||||
this.entity[key] = value;
|
||||
}
|
||||
else {
|
||||
undefinedProperties[key] = value;
|
||||
}
|
||||
}
|
||||
this.state = this.state.merge(undefinedProperties);
|
||||
}
|
||||
|
||||
destroy() {}
|
||||
|
||||
hooks() {
|
||||
|
@ -54,6 +37,23 @@ export class Trait {
|
|||
return {};
|
||||
}
|
||||
|
||||
patchState(patch) {
|
||||
if (!patch.state) {
|
||||
return;
|
||||
}
|
||||
const undefinedProperties = {};
|
||||
for (const key in patch.state) {
|
||||
const value = patch.state[key];
|
||||
if (key in this.entity) {
|
||||
this.entity[key] = value;
|
||||
}
|
||||
else {
|
||||
undefinedProperties[key] = value;
|
||||
}
|
||||
}
|
||||
this.state = this.state.merge(undefinedProperties);
|
||||
}
|
||||
|
||||
toJSON() {
|
||||
return {
|
||||
params: this.params.toJS(),
|
||||
|
|
|
@ -38,26 +38,6 @@ export class Traits {
|
|||
});
|
||||
}
|
||||
|
||||
acceptStateChange(change) {
|
||||
for (const type in change) {
|
||||
let instance = this.traits_PRIVATE[type];
|
||||
// New trait requested?
|
||||
if (!this.traits_PRIVATE[type]) {
|
||||
// Doesn't exist?
|
||||
if (!hasTrait(type)) {
|
||||
continue;
|
||||
}
|
||||
this.addTrait(type, change[type]);
|
||||
instance = this.traits_PRIVATE[type];
|
||||
}
|
||||
else {
|
||||
// Accept state.
|
||||
instance.acceptStateChange(change[type]);
|
||||
}
|
||||
this._setInstanceState(type, instance);
|
||||
}
|
||||
}
|
||||
|
||||
allInstances() {
|
||||
return this.traits_PRIVATE;
|
||||
}
|
||||
|
@ -203,6 +183,26 @@ export class Traits {
|
|||
return results;
|
||||
}
|
||||
|
||||
patchState(patch) {
|
||||
for (const type in patch) {
|
||||
let instance = this.traits_PRIVATE[type];
|
||||
// New trait requested?
|
||||
if (!this.traits_PRIVATE[type]) {
|
||||
// Doesn't exist?
|
||||
if (!hasTrait(type)) {
|
||||
continue;
|
||||
}
|
||||
this.addTrait(type, patch[type]);
|
||||
instance = this.traits_PRIVATE[type];
|
||||
}
|
||||
else {
|
||||
// Accept state.
|
||||
instance.patchState(patch[type]);
|
||||
}
|
||||
this._setInstanceState(type, instance);
|
||||
}
|
||||
}
|
||||
|
||||
removeAllTraits() {
|
||||
const types = this.allTypes();
|
||||
this.removeTraits(types);
|
||||
|
|
|
@ -9,16 +9,6 @@ export class StateSynchronizer {
|
|||
this.tick();
|
||||
}
|
||||
|
||||
acceptStateChange(change) {
|
||||
for (const key in change) {
|
||||
const stateful = this._statefuls[key];
|
||||
if (!stateful) {
|
||||
continue;
|
||||
}
|
||||
stateful.acceptStateChange(change[key]);
|
||||
}
|
||||
}
|
||||
|
||||
diff(previousState) {
|
||||
// Take a pure JS diff.
|
||||
const steps = immutablediff(previousState, this._state).toJS();
|
||||
|
@ -55,6 +45,16 @@ export class StateSynchronizer {
|
|||
return -1 !== ['add', 'replace'].indexOf(step.op);
|
||||
}
|
||||
|
||||
patchState(patch) {
|
||||
for (const key in patch) {
|
||||
const stateful = this._statefuls[key];
|
||||
if (!stateful) {
|
||||
continue;
|
||||
}
|
||||
stateful.patchState(patch[key]);
|
||||
}
|
||||
}
|
||||
|
||||
get state() {
|
||||
return this._state;
|
||||
}
|
||||
|
|
|
@ -29,18 +29,6 @@ export class Layer extends decorate(class {}) {
|
|||
this.tiles.on('dataChanged', this.onTileDataChanged);
|
||||
}
|
||||
|
||||
acceptStateChange(change) {
|
||||
if (change.entityList) {
|
||||
this.entityList.acceptStateChange(change.entityList);
|
||||
}
|
||||
if (change.tilesetUri) {
|
||||
this.tilesetUri = change.tilesetUri;
|
||||
}
|
||||
if (change.tiles) {
|
||||
this.tiles.acceptStateChange(change.tiles);
|
||||
}
|
||||
}
|
||||
|
||||
addEntity(entity) {
|
||||
this.entityList.addEntity(entity);
|
||||
}
|
||||
|
@ -92,6 +80,18 @@ export class Layer extends decorate(class {}) {
|
|||
this.entityList.removeEntity(entity);
|
||||
}
|
||||
|
||||
patchState(patch) {
|
||||
if (patch.entityList) {
|
||||
this.entityList.patchState(patch.entityList);
|
||||
}
|
||||
if (patch.tilesetUri) {
|
||||
this.tilesetUri = patch.tilesetUri;
|
||||
}
|
||||
if (patch.tiles) {
|
||||
this.tiles.patchState(patch.tiles);
|
||||
}
|
||||
}
|
||||
|
||||
setTileAt(x, y, tile) {
|
||||
this.tiles.setTileAt(x, y, tile);
|
||||
}
|
||||
|
|
|
@ -26,16 +26,6 @@ export class Layers extends decorate(class {}) {
|
|||
}
|
||||
}
|
||||
|
||||
acceptStateChange(change) {
|
||||
for (const index in change) {
|
||||
const layer = this.layers[index] ? this.layers[index] : new Layer();
|
||||
if (!this.layers[index]) {
|
||||
this.addLayer(index, layer);
|
||||
}
|
||||
layer.acceptStateChange(change[index]);
|
||||
}
|
||||
}
|
||||
|
||||
addEntityToLayer(entity, layerIndex) {
|
||||
const layer = this.layers[layerIndex];
|
||||
if (!layer) {
|
||||
|
@ -93,6 +83,16 @@ export class Layers extends decorate(class {}) {
|
|||
this.emit('entityRemoved', entity);
|
||||
}
|
||||
|
||||
patchState(patch) {
|
||||
for (const index in patch) {
|
||||
const layer = this.layers[index] ? this.layers[index] : new Layer();
|
||||
if (!this.layers[index]) {
|
||||
this.addLayer(index, layer);
|
||||
}
|
||||
layer.patchState(patch[index]);
|
||||
}
|
||||
}
|
||||
|
||||
removeEntityFromLayer(entity, layerIndex) {
|
||||
const layer = this.layers[layerIndex];
|
||||
if (!layer) {
|
||||
|
|
|
@ -36,18 +36,6 @@ export class Room extends decorate(class {}) {
|
|||
this.on('worldChanged', this.onWorldChanged);
|
||||
}
|
||||
|
||||
acceptStateChange(change) {
|
||||
if (change.width) {
|
||||
this.width = change.width;
|
||||
}
|
||||
if (change.height) {
|
||||
this.height = change.height;
|
||||
}
|
||||
if (change.layers) {
|
||||
this.layers.acceptStateChange(change.layers);
|
||||
}
|
||||
}
|
||||
|
||||
addEntityToLayer(entity, layerIndex = 0) {
|
||||
this.layers.addEntityToLayer(entity, layerIndex);
|
||||
}
|
||||
|
@ -107,6 +95,18 @@ export class Room extends decorate(class {}) {
|
|||
this.updateBounds();
|
||||
}
|
||||
|
||||
patchState(patch) {
|
||||
if (patch.width) {
|
||||
this.width = patch.width;
|
||||
}
|
||||
if (patch.height) {
|
||||
this.height = patch.height;
|
||||
}
|
||||
if (patch.layers) {
|
||||
this.layers.patchState(patch.layers);
|
||||
}
|
||||
}
|
||||
|
||||
removeEntityFromLayer(entity, layerIndex = 0) {
|
||||
this.layers.removeEntityFromLayer(entity, layerIndex);
|
||||
}
|
||||
|
|
|
@ -19,18 +19,18 @@ export class Tiles extends decorate(class {}) {
|
|||
this._state = I.Map();
|
||||
}
|
||||
|
||||
acceptStateChange(change) {
|
||||
if (change.width) {
|
||||
this.width = change.width;
|
||||
patchState(patch) {
|
||||
if (patch.width) {
|
||||
this.width = patch.width;
|
||||
}
|
||||
if (change.height) {
|
||||
this.height = change.height;
|
||||
if (patch.height) {
|
||||
this.height = patch.height;
|
||||
}
|
||||
if (change.data) {
|
||||
if (patch.data) {
|
||||
const oldData = this.data;
|
||||
for (const i in change.data) {
|
||||
for (const i in patch.data) {
|
||||
const index = parseInt(i);
|
||||
this.data = this.data.set(index, change.data[i]);
|
||||
this.data = this.data.set(index, patch.data[i]);
|
||||
}
|
||||
if (oldData !== this.data) {
|
||||
this.emit('dataChanged');
|
||||
|
|
Loading…
Reference in New Issue
Block a user