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