Added support for quiet areas
[libreadventure.git] / front / src / Phaser / Game / PlayerMovement.ts
CommitLineData
d69ce8a6
DN
1import {HasMovedEvent} from "./GameManager";
2import {MAX_EXTRAPOLATION_TIME} from "../../Enum/EnvironmentVariable";
d72e6061 3import {PositionInterface} from "../../Connection";
d69ce8a6
DN
4
5export class PlayerMovement {
d72e6061 6 public constructor(private startPosition: PositionInterface, private startTick: number, private endPosition: HasMovedEvent, private endTick: number) {
d69ce8a6
DN
7 }
8
9 public isOutdated(tick: number): boolean {
d72e6061
DN
10 //console.log(tick, this.endTick, MAX_EXTRAPOLATION_TIME)
11
12 // If the endPosition is NOT moving, no extrapolation needed.
13 if (this.endPosition.moving === false && tick > this.endTick) {
14 return true;
15 }
16
d69ce8a6
DN
17 return tick > this.endTick + MAX_EXTRAPOLATION_TIME;
18 }
19
20 public getPosition(tick: number): HasMovedEvent {
d72e6061
DN
21 // Special case: end position reached and end position is not moving
22 if (tick >= this.endTick && this.endPosition.moving === false) {
23 return this.endPosition;
24 }
25
d69ce8a6
DN
26 let x = (this.endPosition.x - this.startPosition.x) * ((tick - this.startTick) / (this.endTick - this.startTick)) + this.startPosition.x;
27 let y = (this.endPosition.y - this.startPosition.y) * ((tick - this.startTick) / (this.endTick - this.startTick)) + this.startPosition.y;
28
29 return {
30 x,
31 y,
32 direction: this.endPosition.direction,
b13c9991
RR
33 moving: true,
34 silent: true
d69ce8a6
DN
35 }
36 }
37}