63 lines
1.8 KiB
TypeScript
63 lines
1.8 KiB
TypeScript
// SPDX-FileCopyrightText: 2026 Paula Wisnievski
|
|
// SPDX-License-Identifier: AGPL-3.0-or-later
|
|
|
|
import type { EntityEvent, PlayerInfo } from "@/common/protocol.ts";
|
|
import { TICK_DELTA } from "@/common/constants.ts";
|
|
import { ItemStack } from "@/common/inventory.ts";
|
|
import type { ClientLevel } from "../level/client_level.ts";
|
|
import { Player } from "./player.ts";
|
|
|
|
const SMOOTHING = 12;
|
|
// how long they flash red after being hurt, like minecraft
|
|
const HURT_TICKS = 10;
|
|
|
|
// 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;
|
|
// counting down after a hurt event
|
|
hurt_time = 0;
|
|
|
|
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, info.slim);
|
|
}
|
|
this.worn = (info.worn ?? []).map(ItemStack.from_data);
|
|
this.set_swimming(info.swimming === true);
|
|
}
|
|
|
|
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;
|
|
}
|
|
|
|
// players respawn straight away, so dying doesn't show
|
|
on_event(event: EntityEvent) {
|
|
if (event === "hurt") {
|
|
this.hurt_time = HURT_TICKS;
|
|
}
|
|
}
|
|
|
|
tick() {
|
|
if (this.hurt_time > 0) this.hurt_time -= 1;
|
|
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();
|
|
this.update_swim_animation();
|
|
}
|
|
}
|