2020-06-02 10:48:04 +02:00
|
|
|
/**
|
|
|
|
* This class is in charge of computing the position of all players.
|
|
|
|
* Player movement is delayed by 200ms so position depends on ticks.
|
|
|
|
*/
|
2021-06-25 18:14:40 +02:00
|
|
|
import type { HasPlayerMovedEvent } from "../../Api/Events/HasPlayerMovedEvent";
|
2021-05-18 15:18:35 +02:00
|
|
|
import type { PlayerMovement } from "./PlayerMovement";
|
2020-06-02 13:44:42 +02:00
|
|
|
|
2020-06-02 10:48:04 +02:00
|
|
|
export class PlayersPositionInterpolator {
|
2020-09-18 13:57:38 +02:00
|
|
|
playerMovements: Map<number, PlayerMovement> = new Map<number, PlayerMovement>();
|
2020-06-02 13:44:42 +02:00
|
|
|
|
2021-05-10 21:27:17 +02:00
|
|
|
updatePlayerPosition(userId: number, playerMovement: PlayerMovement): void {
|
2020-06-02 13:44:42 +02:00
|
|
|
this.playerMovements.set(userId, playerMovement);
|
|
|
|
}
|
|
|
|
|
2020-09-18 13:57:38 +02:00
|
|
|
removePlayer(userId: number): void {
|
2020-06-02 13:44:42 +02:00
|
|
|
this.playerMovements.delete(userId);
|
|
|
|
}
|
2020-06-02 10:48:04 +02:00
|
|
|
|
2021-05-18 11:33:16 +02:00
|
|
|
getUpdatedPositions(tick: number): Map<number, HasPlayerMovedEvent> {
|
|
|
|
const positions = new Map<number, HasPlayerMovedEvent>();
|
2020-09-18 13:57:38 +02:00
|
|
|
this.playerMovements.forEach((playerMovement: PlayerMovement, userId: number) => {
|
2020-06-02 13:44:42 +02:00
|
|
|
if (playerMovement.isOutdated(tick)) {
|
|
|
|
//console.log("outdated")
|
|
|
|
this.playerMovements.delete(userId);
|
|
|
|
}
|
|
|
|
//console.log("moving")
|
2021-06-25 18:14:40 +02:00
|
|
|
positions.set(userId, playerMovement.getPosition(tick));
|
2020-06-02 13:44:42 +02:00
|
|
|
});
|
|
|
|
return positions;
|
|
|
|
}
|
2020-06-02 10:48:04 +02:00
|
|
|
}
|