fet: state packing
This commit is contained in:
parent
88d1634b8f
commit
e0933ade6d
|
@ -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:
|
||||
|
|
|
@ -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),
|
||||
});
|
||||
}
|
||||
},
|
||||
|
||||
|
|
Loading…
Reference in New Issue
Block a user