44 lines
1.2 KiB
TypeScript
44 lines
1.2 KiB
TypeScript
import type { PlayerInfo } from "$/common/protocol.ts";
|
|
import { TICK_DELTA } from "$/common/constants.ts";
|
|
import type { ClientLevel } from "../level/client_level.ts";
|
|
import { Player } from "./player.ts";
|
|
|
|
const SMOOTHING = 12;
|
|
|
|
// another player on the server, it moves where the server says instead of simulating anything
|
|
export class RemotePlayer extends Player {
|
|
// where the server last said it is, the drawn position eases towards it so movement isn't choppy
|
|
target_x: number;
|
|
target_y: number;
|
|
target_z: number;
|
|
|
|
constructor(level: ClientLevel, info: PlayerInfo) {
|
|
super(level, info.id, info.name);
|
|
this.set_position(info.x, info.y, info.z);
|
|
this.target_x = info.x;
|
|
this.target_y = info.y;
|
|
this.target_z = info.z;
|
|
this.yaw = info.yaw;
|
|
this.pitch = info.pitch;
|
|
if (info.skin) {
|
|
this.set_skin(info.skin);
|
|
}
|
|
}
|
|
|
|
lerp_to(x: number, y: number, z: number, yaw: number, pitch: number) {
|
|
this.target_x = x;
|
|
this.target_y = y;
|
|
this.target_z = z;
|
|
this.yaw = yaw;
|
|
this.pitch = pitch;
|
|
}
|
|
|
|
tick() {
|
|
const t = Math.min(1, TICK_DELTA * SMOOTHING);
|
|
this.x += (this.target_x - this.x) * t;
|
|
this.y += (this.target_y - this.y) * t;
|
|
this.z += (this.target_z - this.z) * t;
|
|
this.update_walk_animation();
|
|
}
|
|
}
|