fet: state packing

This commit is contained in:
cha0s 2019-04-05 23:40:33 -04:00
parent 88d1634b8f
commit e0933ade6d
2 changed files with 10 additions and 3 deletions

View File

@ -5,7 +5,7 @@ import {ActionRegistry} from '@avocado/input';
import {Stage} from '@avocado/graphics';
import {Vector} from '@avocado/math';
import {World} from '@avocado/physics/matter/world';
import {StateSynchronizer} from '@avocado/state';
import {StateSynchronizer, Unpacker} from '@avocado/state';
import {Room, RoomView} from '@avocado/topdown';
import {clearAnimation, setAnimation} from '@avocado/timing';
// 1st party.
@ -49,6 +49,7 @@ const stateSynchronizer = new StateSynchronizer({
room,
worldTime,
});
const unpacker = new Unpacker();
room.on('entityAdded', (entity) => {
// Traits that only make sense for our self entity on the client.
const selfEntityOnlyTraits = [
@ -181,10 +182,11 @@ const predictionHandle = setInterval(() => {
function onMessage({type, payload}) {
switch (type) {
case 'state-update':
const patch = unpacker.unpack(payload);
if (payload.selfEntity) {
selfEntity = payload.selfEntity;
}
stateSynchronizer.patchState(payload);
stateSynchronizer.patchState(patch);
dirty = true;
break;
default:

View File

@ -3,6 +3,7 @@ import isPlainObject from 'is-plain-object';
import {compose} from '@avocado/core';
import {Trait} from '@avocado/entity';
import {Packer} from '@avocado/state';
const decorate = compose(
);
@ -15,6 +16,7 @@ export class Informed extends decorate(Trait) {
// Let the client know who they are.
selfEntity: this.entity.instanceUuid,
});
this._packer = new Packer();
this._socket = undefined;
}
@ -112,7 +114,10 @@ export class Informed extends decorate(Trait) {
this._informState = stateSynchronizer.state;
// Emit!
if (diff) {
this._socket.send({type: 'state-update', payload: diff});
this._socket.send({
type: 'state-update',
payload: this._packer.pack(diff),
});
}
},