2021-05-10 21:27:17 +02:00
|
|
|
import { MAX_EXTRAPOLATION_TIME } from "../../Enum/EnvironmentVariable";
|
2021-05-18 15:18:35 +02:00
|
|
|
import type { PositionInterface } from "../../Connexion/ConnexionModels";
|
2021-06-25 18:14:40 +02:00
|
|
|
import type { HasPlayerMovedEvent } from "../../Api/Events/HasPlayerMovedEvent";
|
2020-06-02 10:48:04 +02:00
|
|
|
|
|
|
|
export class PlayerMovement {
|
2021-06-25 18:14:40 +02:00
|
|
|
public constructor(
|
|
|
|
private startPosition: PositionInterface,
|
|
|
|
private startTick: number,
|
|
|
|
private endPosition: HasPlayerMovedEvent,
|
|
|
|
private endTick: number
|
|
|
|
) {}
|
2020-06-02 10:48:04 +02:00
|
|
|
|
|
|
|
public isOutdated(tick: number): boolean {
|
2020-06-02 13:44:42 +02:00
|
|
|
//console.log(tick, this.endTick, MAX_EXTRAPOLATION_TIME)
|
|
|
|
|
|
|
|
// If the endPosition is NOT moving, no extrapolation needed.
|
|
|
|
if (this.endPosition.moving === false && tick > this.endTick) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-06-02 10:48:04 +02:00
|
|
|
return tick > this.endTick + MAX_EXTRAPOLATION_TIME;
|
|
|
|
}
|
|
|
|
|
2021-05-18 11:33:16 +02:00
|
|
|
public getPosition(tick: number): HasPlayerMovedEvent {
|
2020-06-02 13:44:42 +02:00
|
|
|
// Special case: end position reached and end position is not moving
|
|
|
|
if (tick >= this.endTick && this.endPosition.moving === false) {
|
2020-09-24 11:16:08 +02:00
|
|
|
//console.log('Movement finished ', this.endPosition)
|
2020-06-02 13:44:42 +02:00
|
|
|
return this.endPosition;
|
|
|
|
}
|
|
|
|
|
2021-06-25 18:14:40 +02:00
|
|
|
const x =
|
|
|
|
(this.endPosition.x - this.startPosition.x) * ((tick - this.startTick) / (this.endTick - this.startTick)) +
|
|
|
|
this.startPosition.x;
|
|
|
|
const y =
|
|
|
|
(this.endPosition.y - this.startPosition.y) * ((tick - this.startTick) / (this.endTick - this.startTick)) +
|
|
|
|
this.startPosition.y;
|
2020-09-24 11:16:08 +02:00
|
|
|
//console.log('Computed position ', x, y)
|
2020-06-02 10:48:04 +02:00
|
|
|
return {
|
|
|
|
x,
|
|
|
|
y,
|
|
|
|
direction: this.endPosition.direction,
|
2021-06-25 18:14:40 +02:00
|
|
|
moving: true,
|
|
|
|
};
|
2020-06-02 10:48:04 +02:00
|
|
|
}
|
|
|
|
}
|