Added support for quiet areas
[libreadventure.git] / front / src / Phaser / Game / PlayerMovement.ts
1 import {HasMovedEvent} from "./GameManager";
2 import {MAX_EXTRAPOLATION_TIME} from "../../Enum/EnvironmentVariable";
3 import {PositionInterface} from "../../Connection";
4
5 export class PlayerMovement {
6 public constructor(private startPosition: PositionInterface, private startTick: number, private endPosition: HasMovedEvent, private endTick: number) {
7 }
8
9 public isOutdated(tick: number): boolean {
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
17 return tick > this.endTick + MAX_EXTRAPOLATION_TIME;
18 }
19
20 public getPosition(tick: number): HasMovedEvent {
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
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,
33 moving: true,
34 silent: true
35 }
36 }
37 }