fix: sidechannel position update

This commit is contained in:
cha0s 2021-03-21 18:40:30 -05:00
parent d1bc293c97
commit 001cd57d4e
2 changed files with 13 additions and 15 deletions

View File

@ -240,6 +240,7 @@ export default () => class Mobile extends decorate(Trait) {
tick(elapsed) {
if (this.entity.isMobile && !Vector.isZero(this.actionMovement)) {
this.entity.markSidechannelPositionUpdate();
this.entity.requestMovement(this.actionMovement);
}
if (Vector.isZero(this.#appliedMovement)) {

View File

@ -7,25 +7,23 @@ const decorate = compose(
Vector.Mixin('trackedPosition', 'x', 'y', {
track: true,
}),
Vector.Mixin('serverPosition', 'serverX', 'serverY', {
track: true,
}),
Vector.Mixin('serverPosition', 'serverX', 'serverY'),
);
// < 16768 will pack into 1 short per axe and give +/- 0.25 precision.
export default () => class Positioned extends decorate(Trait) {
#sidechannelUpdate = false;
constructor() {
super();
this.on('trackedPositionChanged', this.onTrackedPositionChanged, this);
if ('client' === process.env.SIDE) {
this.on('serverPositionChanged', this.onServerPositionChanged, this);
}
}
acceptPacket(packet) {
if ('TraitUpdatePositionedPosition' === packet.constructor.type) {
[this.serverX, this.serverY] = packet.data;
this.serverPositionDirty = true;
}
}
@ -76,14 +74,12 @@ export default () => class Positioned extends decorate(Trait) {
cleanPackets() {
super.cleanPackets();
this.#sidechannelUpdate = false;
}
destroy() {
super.destroy();
this.off('trackedPositionChanged', this.ontrackedPositionChanged);
if ('client' === process.env.SIDE) {
this.off('serverPositionChanged', this.onServerPositionChanged);
}
}
async load(json) {
@ -108,13 +104,9 @@ export default () => class Positioned extends decorate(Trait) {
this.entity.emit('positionChanged', oldPosition, newPosition);
}
onServerPositionChanged() {
this.serverPositionDirty = true;
}
packetsFor() {
packetsFor(informed) {
const {x, y} = this.stateDifferences();
if (x || y) {
if ((informed === this.entity && this.#sidechannelUpdate) || x || y) {
return [[
'TraitUpdatePositionedPosition',
this.entity.position,
@ -128,6 +120,11 @@ export default () => class Positioned extends decorate(Trait) {
distanceFrom: (other) => Vector.distance(this.entity.position, other.position),
markSidechannelPositionUpdate: () => {
this.#sidechannelUpdate = true;
this.markAsDirty();
},
pointAround: (radius, angle) => {
if (!this.entity.is('Positioned')) {
return [0, 0];