refactor: state diffs
This commit is contained in:
parent
40a9731147
commit
b169bb8c2d
|
@ -28,21 +28,15 @@ export class Trait extends decorate(class {}) {
|
|||
}
|
||||
|
||||
cleanPackets() {
|
||||
for (const key in this.state) {
|
||||
this.previousState[key] = this.state[key];
|
||||
}
|
||||
}
|
||||
|
||||
static contextType() {
|
||||
return {};
|
||||
}
|
||||
|
||||
createTraitPacketUpdates(Packet) {
|
||||
const packets = [];
|
||||
if (this.isDirty) {
|
||||
packets.push(new Packet(this.state, this.entity));
|
||||
this.makeClean();
|
||||
}
|
||||
return packets;
|
||||
}
|
||||
|
||||
static defaultJSON() {
|
||||
return {
|
||||
params: this.defaultParams(),
|
||||
|
@ -89,12 +83,6 @@ export class Trait extends decorate(class {}) {
|
|||
return {};
|
||||
}
|
||||
|
||||
makeClean() {
|
||||
for (const key in this.state) {
|
||||
this.previousState[key] = this.state[key];
|
||||
}
|
||||
}
|
||||
|
||||
memoizedListeners() {
|
||||
if (!this._memoizedListeners) {
|
||||
this._memoizedListeners = this.listeners();
|
||||
|
@ -115,6 +103,19 @@ export class Trait extends decorate(class {}) {
|
|||
return false;
|
||||
}
|
||||
|
||||
stateDifferences() {
|
||||
const differences = {};
|
||||
for (const key in this.state) {
|
||||
if (this.state[key] !== this.previousState[key]) {
|
||||
differences[key] = {
|
||||
old: this.previousState[key],
|
||||
value: this.state[key],
|
||||
};
|
||||
}
|
||||
}
|
||||
return differences;
|
||||
}
|
||||
|
||||
toJSON() {
|
||||
return {
|
||||
params: this.params,
|
||||
|
|
|
@ -33,7 +33,6 @@ export class Directional extends decorate(Trait) {
|
|||
|
||||
constructor(entity, params, state) {
|
||||
super(entity, params, state);
|
||||
this._directionChanged = false;
|
||||
this.directionCount = this.params.directionCount;
|
||||
}
|
||||
|
||||
|
@ -43,13 +42,10 @@ export class Directional extends decorate(Trait) {
|
|||
}
|
||||
}
|
||||
|
||||
cleanPackets() {
|
||||
this._directionChanged = false;
|
||||
}
|
||||
|
||||
packets(informed) {
|
||||
if (this._directionChanged) {
|
||||
return new TraitUpdateDirectionalDirectionPacket(this.entity.direction);
|
||||
const {direction} = this.stateDifferences();
|
||||
if (direction) {
|
||||
return new TraitUpdateDirectionalDirectionPacket(direction.value);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -34,7 +34,6 @@ export class Positioned extends decorate(Trait) {
|
|||
const x = this.state.x;
|
||||
const y = this.state.y;
|
||||
this._position = [x, y];
|
||||
this._positionChanged = false;
|
||||
this.entity.position[0] = x;
|
||||
this.entity.position[1] = y;
|
||||
if (AVOCADO_CLIENT) {
|
||||
|
@ -45,10 +44,6 @@ export class Positioned extends decorate(Trait) {
|
|||
}
|
||||
}
|
||||
|
||||
cleanPackets() {
|
||||
this._positionChanged = false;
|
||||
}
|
||||
|
||||
destroy() {
|
||||
this.off('_positionChanged', this.on_positionChanged);
|
||||
if (AVOCADO_CLIENT) {
|
||||
|
@ -68,7 +63,6 @@ export class Positioned extends decorate(Trait) {
|
|||
if (AVOCADO_SERVER) {
|
||||
this.state.x = newPosition[0];
|
||||
this.state.y = newPosition[1];
|
||||
this._positionChanged = true;
|
||||
}
|
||||
this.entity.emit('positionChanged', oldPosition, newPosition);
|
||||
}
|
||||
|
@ -78,7 +72,8 @@ export class Positioned extends decorate(Trait) {
|
|||
}
|
||||
|
||||
packets(informed) {
|
||||
if (this._positionChanged) {
|
||||
const {x, y} = this.stateDifferences();
|
||||
if (x || y) {
|
||||
// Physics slop can end us up with negatives. Don't allow them in the
|
||||
// packed representation, even though it means a slight loss in accuracy.
|
||||
// The world bounds will (should!) keep things *eventually* correct.
|
||||
|
|
Loading…
Reference in New Issue
Block a user