refactor: informed logic elegance
This commit is contained in:
parent
cbccd7b1ae
commit
6b221837a7
|
@ -70,26 +70,25 @@ export class Informed extends decorate(Trait) {
|
|||
}
|
||||
|
||||
reduceStateDiff(diff) {
|
||||
if (!diff || !diff.room || !diff.room.layers) {
|
||||
return diff
|
||||
}
|
||||
const room = this.entity.room;
|
||||
const camera = this.entity.camera;
|
||||
for (const index in diff.room.layers) {
|
||||
// Index entities.
|
||||
const layer = room.layer(index);
|
||||
const visibleEntities = layer.visibleEntities(
|
||||
camera.rectangle
|
||||
);
|
||||
const indexedEntities = {};
|
||||
for (const entity of visibleEntities) {
|
||||
indexedEntities[entity.instanceUuid] = entity;
|
||||
if (diff && diff.room && diff.room.layers) {
|
||||
const room = this.entity.room;
|
||||
const camera = this.entity.camera;
|
||||
for (const index in diff.room.layers) {
|
||||
// Index entities.
|
||||
const layer = room.layer(index);
|
||||
const visibleEntities = layer.visibleEntities(
|
||||
camera.rectangle
|
||||
);
|
||||
const indexedEntities = {};
|
||||
for (const entity of visibleEntities) {
|
||||
indexedEntities[entity.instanceUuid] = entity;
|
||||
}
|
||||
// Reduce entities.
|
||||
diff.room.layers[index].entityList = this.entityReducer(
|
||||
diff.room.layers[index].entityList || {},
|
||||
indexedEntities,
|
||||
);
|
||||
}
|
||||
// Reduce entities.
|
||||
diff.room.layers[index].entityList = this.entityReducer(
|
||||
diff.room.layers[index].entityList || {},
|
||||
indexedEntities,
|
||||
);
|
||||
}
|
||||
return this.collapseStateDiff(diff);
|
||||
}
|
||||
|
@ -109,20 +108,17 @@ export class Informed extends decorate(Trait) {
|
|||
inform: (stateSynchronizer) => {
|
||||
// Take diff.
|
||||
let diff = stateSynchronizer.diff(this._informState);
|
||||
diff = this.reduceStateDiff(diff);
|
||||
diff = diff ? diff : {};
|
||||
this._informState = stateSynchronizer.state;
|
||||
// Let the client know who they are.
|
||||
if (!this._sentSelf) {
|
||||
diff.selfEntity = this.entity.instanceUuid;
|
||||
this._sentSelf = true;
|
||||
}
|
||||
// Remove dead updates.
|
||||
if (0 === Object.keys(diff).length) {
|
||||
return;
|
||||
}
|
||||
diff = this.reduceStateDiff(diff);
|
||||
// Emit!
|
||||
this._socket.send({type: 'state-update', payload: diff});
|
||||
if (diff) {
|
||||
this._socket.send({type: 'state-update', payload: diff});
|
||||
}
|
||||
},
|
||||
|
||||
};
|
||||
|
|
Loading…
Reference in New Issue
Block a user