82 lines
3.5 KiB
TypeScript
82 lines
3.5 KiB
TypeScript
// SPDX-FileCopyrightText: 2026 Paula Wisnievski
|
|
// SPDX-License-Identifier: AGPL-3.0-or-later
|
|
|
|
import { PLAYER_EYE_HEIGHT, PLAYER_HEIGHT, PLAYER_SWIM_HEIGHT, PLAYER_WIDTH } from "@/common/constants.ts";
|
|
import { ItemStack } from "@/common/inventory.ts";
|
|
import type { ClientLevel } from "../level/client_level.ts";
|
|
import type { Texture } from "../renderer/mod.ts";
|
|
import { decode_skin } from "../skins.ts";
|
|
import { Entity } from "./entity.ts";
|
|
|
|
// how much of the way it tips over or stands up each tick, so it takes 10 ticks
|
|
const SWIM_EASE = 0.1;
|
|
// how much of the way the body turns toward which way it's going each tick
|
|
const SWIM_PITCH_EASE = 0.3;
|
|
// blocks a tick. slower than this the body lies the way they look instead, it's how they moved that's too jittery
|
|
const SWIM_MOVING = 0.05;
|
|
|
|
export abstract class Player extends Entity {
|
|
name: string;
|
|
// like minecraft's players: slabs yes, full blocks need a jump
|
|
override step_height = 0.6;
|
|
// more than minecraft, where it's only the tick it leaves the ground: stepping from the edge of a block onto one
|
|
// diagonal to it drops it a little first, this lets it still step up onto that one
|
|
override step_grace_ticks = 3;
|
|
// undefined draws the default skin
|
|
skin: Texture | undefined;
|
|
// 3 pixel wide arms, for skins made for them
|
|
slim = false;
|
|
// what they wear that shows, items with a worn model, see the server's player_worn
|
|
worn: ItemStack[] = [];
|
|
// lying flat in the short box, see set_swimming
|
|
swimming = false;
|
|
// how far over they've tipped, 0 standing to 1 lying flat, see update_swim_animation
|
|
swim_amount = 0;
|
|
prev_swim_amount = 0;
|
|
// which way the body lies while swimming, radians up from flat: the way they're going, or looking when still
|
|
swim_pitch = 0;
|
|
prev_swim_pitch = 0;
|
|
|
|
constructor(level: ClientLevel, id: string, name: string) {
|
|
super(level, id, PLAYER_WIDTH, PLAYER_HEIGHT, PLAYER_EYE_HEIGHT);
|
|
this.name = name;
|
|
}
|
|
|
|
// swimming lies them flat in the short box, and not swimming stands them back up. the local player checks there's
|
|
// room first, remote players' clients already did
|
|
set_swimming(swimming: boolean) {
|
|
this.swimming = swimming;
|
|
this.height = swimming ? PLAYER_SWIM_HEIGHT : PLAYER_HEIGHT;
|
|
}
|
|
|
|
// after moving each tick, for tipping over and standing up smoothly, local and remote players alike. the pitch comes
|
|
// from how they moved, so remote players get it without anything more from the server
|
|
update_swim_animation() {
|
|
this.prev_swim_amount = this.swim_amount;
|
|
this.prev_swim_pitch = this.swim_pitch;
|
|
const amount = this.swimming ? 1 : 0;
|
|
this.swim_amount += Math.max(-SWIM_EASE, Math.min(SWIM_EASE, amount - this.swim_amount));
|
|
const [dx, dy, dz] = [this.x - this.prev_x, this.y - this.prev_y, this.z - this.prev_z];
|
|
const moving = Math.atan2(dy, Math.hypot(dx, dz));
|
|
const weight = Math.min(Math.hypot(dx, dy, dz) / SWIM_MOVING, 1);
|
|
const pitch = this.pitch + (moving - this.pitch) * weight;
|
|
this.swim_pitch += (pitch - this.swim_pitch) * SWIM_PITCH_EASE;
|
|
}
|
|
|
|
// a frame partial_tick of the way to the next tick
|
|
render_swim(partial_tick: number) {
|
|
return {
|
|
amount: this.prev_swim_amount + (this.swim_amount - this.prev_swim_amount) * partial_tick,
|
|
pitch: this.prev_swim_pitch + (this.swim_pitch - this.prev_swim_pitch) * partial_tick,
|
|
};
|
|
}
|
|
|
|
// a base64 png from the server, or undefined for the default
|
|
set_skin(skin: string | undefined, slim = false) {
|
|
const texture = skin ? decode_skin(skin) : undefined;
|
|
this.skin?.tex.destroy();
|
|
this.skin = texture;
|
|
this.slim = slim;
|
|
}
|
|
}
|