381 lines
16 KiB
TypeScript
381 lines
16 KiB
TypeScript
// SPDX-FileCopyrightText: 2026 Paula Wisnievski
|
|
// SPDX-License-Identifier: AGPL-3.0-or-later
|
|
|
|
import { PLAYER_EYE_HEIGHT, PLAYER_HEIGHT, PLAYER_SWIM_EYE_HEIGHT, TICK_DELTA } from "@/common/constants.ts";
|
|
import { physics } from "@/common/physics.ts";
|
|
import type { Client } from "../client.ts";
|
|
import { InputManager } from "../input_manager.ts";
|
|
import { ClientInventories } from "../inventory.ts";
|
|
import { Player } from "./player.ts";
|
|
|
|
// the position goes to the server every other tick, and on the tick it lands so the server always sees it touch the
|
|
// ground, which is what resets its fall distance
|
|
const SEND_POSITION_TICKS = 2;
|
|
// ticks after a jump before holding jump can jump again, like minecraft's noJumpDelay. a jump is longer than this in
|
|
// the air, so holding it jumps again as soon as it lands
|
|
const JUMP_DELAY_TICKS = 10;
|
|
// like minecraft, walking isn't capped: each tick the keys add this much speed, it moves, then friction takes some
|
|
// away, so it settles where they even out, at acceleration / (1 - friction). on the ground that's 0.098 blocks per
|
|
// tick and 0.546 friction, 4.317 blocks a second. in the air it's 0.0196 and 0.91, so it keeps its speed from the
|
|
// ground but can't steer much
|
|
const GROUND_ACCELERATION = 0.098 / TICK_DELTA;
|
|
const GROUND_FRICTION = 0.546;
|
|
const AIR_ACCELERATION = 0.0196 / TICK_DELTA;
|
|
const AIR_FRICTION = 0.91;
|
|
// slower than this (0.003 blocks a tick) it stops, instead of creeping forever
|
|
const MIN_SPEED = 0.003 / TICK_DELTA;
|
|
// sprinting is 30% faster than walking, 5.612 blocks a second
|
|
const SPRINT_SPEED = 1.3;
|
|
// jumping while sprinting adds 0.2 blocks a tick forward, which averages about 7.1 blocks a second
|
|
const SPRINT_JUMP_BOOST = 0.2 / TICK_DELTA;
|
|
// like minecraft, it can't start sprinting with this much food or less, and stops when it gets there
|
|
const SPRINT_MIN_FOOD = 6;
|
|
// running into a wall stops sprinting, unless it's looking less than this far into it (sin of 8 degrees), so
|
|
// sprinting along a wall keeps going
|
|
const SPRINT_WALL_ANGLE = Math.sin(8 * Math.PI / 180);
|
|
// the fov while sprinting, halfway between walking and the speed, eased there by half each tick like minecraft
|
|
const SPRINT_FOV = (1 + SPRINT_SPEED) / 2;
|
|
const FOV_EASE = 0.5;
|
|
// like minecraft: sneaking is 30% of walking speed and lowers the eyes 0.35, which ease there by half each tick
|
|
const SNEAK_SPEED = 0.3;
|
|
// using an item over time, like drawing a bow, slows walking to a fifth and stops sprinting, like minecraft
|
|
const USING_SPEED = 0.2;
|
|
const SNEAK_EYE_HEIGHT = PLAYER_EYE_HEIGHT - 0.35;
|
|
const EYE_EASE = 0.5;
|
|
// sneaking keeps it from walking off anything taller than this
|
|
const SNEAK_MAX_DROP = 0.6;
|
|
|
|
// in water, like minecraft's LivingEntity.travel and the server's mobs: the keys add 0.02 blocks a tick, gravity is a
|
|
// sixteenth of the usual, and it slows to 0.8 of its speed each tick on every axis. walking settles at 2 blocks a
|
|
// second, and sinking at 0.45
|
|
const WATER_ACCELERATION = 0.02 / TICK_DELTA;
|
|
const WATER_GRAVITY = -32 / 16;
|
|
const WATER_DRAG = 0.8;
|
|
// holding jump in water pushes it up this much each tick, like minecraft's jumpInLiquid, rising 3.55 blocks a second
|
|
const SWIM_UP = 0.04 / TICK_DELTA;
|
|
// and sneaking pulls it down as much, like minecraft's goDownInWater, sinking 4.45 blocks a second. both cancel out
|
|
const SWIM_DOWN = 0.04 / TICK_DELTA;
|
|
// swimming into a wall with nothing solid or wet 0.6 above, like the edge of a pool, it hops up like minecraft. a bit
|
|
// harder than minecraft's 0.3, whose water is a little lower than a full block, so a ledge a block above the water can
|
|
// be climbed onto
|
|
const WATER_HOP_HEIGHT = 0.6;
|
|
const WATER_HOP = 0.36 / TICK_DELTA;
|
|
// swimming, sprinting with the eyes under water: no gravity and less drag sideways, so it goes 4 blocks a second
|
|
const SWIM_DRAG = 0.9;
|
|
// and it turns up or down toward where it's looking, this much of the way each tick, like minecraft's Player.travel
|
|
const SWIM_STEER = 0.06;
|
|
// flying, see /fly, like minecraft's creative flight: the keys add 0.05 blocks a tick and it keeps 0.91 of its speed,
|
|
// settling at 11 blocks a second, twice that sprinting. there's no gravity, jump and sneak push it up and down 0.15
|
|
// blocks a tick, and it keeps 0.6 of that each tick, rising and sinking 7.5 blocks a second
|
|
const FLY_ACCELERATION = 0.05 / TICK_DELTA;
|
|
const FLY_FRICTION = 0.91;
|
|
const FLY_SPRINT_SPEED = 2;
|
|
const FLY_VERTICAL = 0.15 / TICK_DELTA;
|
|
const FLY_VERTICAL_DRAG = 0.6;
|
|
// on land, the entity's own
|
|
const LAND_GRAVITY = -32;
|
|
const LAND_DRAG = 0.98;
|
|
|
|
// what the keys ask for this tick. x is right, z is back, as -1 to 1
|
|
interface MoveInput {
|
|
x: number;
|
|
z: number;
|
|
jump: boolean;
|
|
sprint: boolean;
|
|
sneak: boolean;
|
|
}
|
|
|
|
// while a screen is open, like minecraft ignoring movement keys then
|
|
const NO_INPUT: MoveInput = { x: 0, z: 0, jump: false, sprint: false, sneak: false };
|
|
|
|
// the player this client controls, like minecraft's LocalPlayer
|
|
export class LocalPlayer extends Player {
|
|
client: Client;
|
|
inventories = new ClientInventories();
|
|
// what the server says, see server/game/vitals.ts. food and air out of 20, health out of max_health
|
|
health = 20;
|
|
max_health = 20;
|
|
food = 20;
|
|
saturation = 5;
|
|
air = 20;
|
|
// the effects it has that show an icon, see the inventory screen. counted down here, the server only says when
|
|
// they change
|
|
effects: { id: string; amplifier: number; duration: number }[] = [];
|
|
// the attack speed attribute the server sent, and when they last attacked or switched slots, for the indicator
|
|
attack_speed = 4;
|
|
last_attack = -Infinity;
|
|
// how far it reaches blocks and entities, the attributes the server sent
|
|
block_interaction_range = 6;
|
|
entity_interaction_range = 3;
|
|
// using its held item over time, as the server says, see ctx's player.start_using
|
|
using_item = false;
|
|
|
|
// blocks per second, peaks about 1.25 blocks up with the entity's gravity and drag
|
|
jump_force = 9.23;
|
|
|
|
// the last teleport from the server, sent back with every move so the server knows which moves came after it
|
|
teleport_id = 0;
|
|
#ticks_since_sent = 0;
|
|
// counts down every tick, letting go of jump clears it
|
|
#jump_delay = 0;
|
|
// the sneaking pose, which flying doesn't have
|
|
sneaking = false;
|
|
// holding the sneak key, flying or not, which is what the server is told for placing and using things
|
|
holding_sneak = false;
|
|
// stays on after letting go of the key, see #update_sprinting
|
|
sprinting = false;
|
|
// what the server says, see /fly
|
|
flying = false;
|
|
#fov_scale = 1;
|
|
#prev_fov_scale = 1;
|
|
#prev_eye_height = PLAYER_EYE_HEIGHT;
|
|
// whether it was in water at the start of this tick, which decides how it moves for all of it
|
|
#in_water = false;
|
|
|
|
constructor(client: Client, id: string, name: string) {
|
|
super(client.level, id, name);
|
|
this.client = client;
|
|
}
|
|
|
|
// like the server's ServerPlayer.attack_strength, from real time so the indicator fills smoothly: 0 right after
|
|
// attacking, 1 after 20 / attack_speed ticks
|
|
attack_strength(): number {
|
|
const ticks = (performance.now() - this.last_attack) / 50;
|
|
return Math.min(1, Math.max(0, (ticks + 0.5) / (20 / this.attack_speed)));
|
|
}
|
|
|
|
tick() {
|
|
for (const effect of this.effects) if (effect.duration > 0) effect.duration -= 1;
|
|
// flying ignores water
|
|
this.#in_water = !this.flying && this.in_water;
|
|
const was_swimming = this.swimming;
|
|
const was_holding_sneak = this.holding_sneak;
|
|
const input = this.client.screen ? NO_INPUT : this.#read_input();
|
|
this.#apply_input(input);
|
|
// after sprinting is, so letting go stands it up the same tick
|
|
this.#update_swimming();
|
|
// out of the water but still in the short box, under something low, it falls like on land
|
|
this.gravity = this.flying ? 0 : !this.#in_water ? LAND_GRAVITY : this.swimming ? 0 : WATER_GRAVITY;
|
|
this.drag = this.flying ? FLY_VERTICAL_DRAG : this.#in_water ? WATER_DRAG : LAND_DRAG;
|
|
if (this.swimming && this.#in_water) this.#steer(input);
|
|
if (!this.flying) {
|
|
physics.push_by_fluid(
|
|
this,
|
|
(x, y, z) => this.level.fluid_at(x, y, z),
|
|
(x, y, z) => this.level.collision_shape(x, y, z),
|
|
true,
|
|
);
|
|
}
|
|
this.#prev_eye_height = this.eye_height;
|
|
const eye_height = this.swimming
|
|
? PLAYER_SWIM_EYE_HEIGHT
|
|
: this.sneaking
|
|
? SNEAK_EYE_HEIGHT
|
|
: PLAYER_EYE_HEIGHT;
|
|
this.eye_height += (eye_height - this.eye_height) * EYE_EASE;
|
|
this.#prev_fov_scale = this.#fov_scale;
|
|
this.#fov_scale += ((this.sprinting ? SPRINT_FOV : 1) - this.#fov_scale) * FOV_EASE;
|
|
|
|
const was_on_ground = this.on_ground;
|
|
// not while jumping, so it can still jump off a ledge
|
|
if (this.sneaking && this.on_ground && this.vy <= 0) {
|
|
const is_solid = (x: number, y: number, z: number) => this.level.collision_shape(x, y, z);
|
|
const [dx, dz] = physics.back_off_from_edge(
|
|
this,
|
|
this.vx * TICK_DELTA,
|
|
this.vz * TICK_DELTA,
|
|
SNEAK_MAX_DROP,
|
|
is_solid,
|
|
);
|
|
this.vx = dx / TICK_DELTA;
|
|
this.vz = dz / TICK_DELTA;
|
|
}
|
|
const from_y = this.y;
|
|
this.move();
|
|
if (this.#in_water) this.#hop_out_of_water(from_y);
|
|
if (Math.abs(this.vx) < MIN_SPEED) this.vx = 0;
|
|
if (Math.abs(this.vz) < MIN_SPEED) this.vz = 0;
|
|
this.update_swim_animation();
|
|
this.#send_position(
|
|
(this.on_ground && !was_on_ground) || this.swimming !== was_swimming ||
|
|
this.holding_sneak !== was_holding_sneak,
|
|
);
|
|
}
|
|
|
|
// like voxelibre: sprinting with the eyes under water starts it, and it keeps going while it sprints in water, along
|
|
// the surface too. it only stands back up where there's room, until then it stays in the short box
|
|
#update_swimming() {
|
|
if (!this.swimming) {
|
|
if (this.sprinting && this.eyes_in_water && !this.flying) this.set_swimming(true);
|
|
return;
|
|
}
|
|
if (this.sprinting && this.#in_water) return;
|
|
const standing = {
|
|
x: this.x,
|
|
y: this.y,
|
|
z: this.z,
|
|
width: this.width,
|
|
height: PLAYER_HEIGHT,
|
|
vx: 0,
|
|
vy: 0,
|
|
vz: 0,
|
|
};
|
|
if (physics.box_is_free(standing, 0, 0, 0, (x, y, z) => this.level.collision_shape(x, y, z))) {
|
|
this.set_swimming(false);
|
|
}
|
|
}
|
|
|
|
// turns up or down toward where it's looking. not up once its head is out, so it swims along the surface instead
|
|
// of leaping out of the water, unless it's holding jump
|
|
#steer(input: MoveInput) {
|
|
const look_y = Math.sin(this.pitch);
|
|
const head_in_water = this.level.is_fluid(
|
|
Math.floor(this.x),
|
|
Math.floor(this.y + this.height),
|
|
Math.floor(this.z),
|
|
);
|
|
if (look_y <= 0 || input.jump || head_in_water) {
|
|
this.vy += (look_y / TICK_DELTA - this.vy) * SWIM_STEER;
|
|
}
|
|
}
|
|
|
|
// turns with the mouse every frame, not every tick, like minecraft's MouseHandler.turnPlayer
|
|
turn(mouse_dx: number, mouse_dy: number) {
|
|
this.yaw += -mouse_dx * 0.001;
|
|
this.pitch += -mouse_dy * 0.001;
|
|
|
|
const limit = Math.PI / 2 - 0.01;
|
|
this.pitch = Math.max(-limit, Math.min(limit, this.pitch));
|
|
}
|
|
|
|
override horizontal_friction() {
|
|
if (this.flying) return FLY_FRICTION;
|
|
if (this.#in_water) return this.swimming ? SWIM_DRAG : WATER_DRAG;
|
|
return this.on_ground ? GROUND_FRICTION : AIR_FRICTION;
|
|
}
|
|
|
|
// like minecraft's LivingEntity.travel in water: after running into a wall, if the box one move on and 0.6 higher
|
|
// than where it started touches nothing solid or wet, it hops up, out onto the ledge
|
|
#hop_out_of_water(from_y: number) {
|
|
if (!this.colliding_x && !this.colliding_z) return;
|
|
const blocked = (x: number, y: number, z: number) =>
|
|
this.level.is_fluid(x, y, z) || this.level.collision_shape(x, y, z);
|
|
const dy = from_y - this.y + this.vy * TICK_DELTA + WATER_HOP_HEIGHT;
|
|
if (physics.box_is_free(this, this.vx * TICK_DELTA, dy, this.vz * TICK_DELTA, blocked)) this.vy = WATER_HOP;
|
|
}
|
|
|
|
// how much wider the view is than the fov option, between the last tick and this one
|
|
override render_fov_scale(partial_tick: number) {
|
|
return this.#prev_fov_scale + (this.#fov_scale - this.#prev_fov_scale) * partial_tick;
|
|
}
|
|
|
|
// the eyes between the last tick and this one, for the camera
|
|
override render_eye_height(partial_tick: number) {
|
|
return this.#prev_eye_height + (this.eye_height - this.#prev_eye_height) * partial_tick;
|
|
}
|
|
|
|
#read_input(): MoveInput {
|
|
const options = this.client.options;
|
|
const down = (key: typeof options.key_jump) => InputManager.is_key_down(key);
|
|
return {
|
|
x: Number(down(options.key_right)) - Number(down(options.key_left)),
|
|
z: Number(down(options.key_back)) - Number(down(options.key_forward)),
|
|
jump: down(options.key_jump),
|
|
sprint: down(options.key_sprint),
|
|
sneak: down(options.key_sneak),
|
|
};
|
|
}
|
|
|
|
// walking relative to where it's looking
|
|
#apply_input(input: MoveInput) {
|
|
let { x: input_x, z: input_z } = input;
|
|
const size = Math.hypot(input_x, input_z);
|
|
if (size > 0) {
|
|
const scale = (this.using_item ? USING_SPEED : 1) / size;
|
|
input_x *= scale;
|
|
input_z *= scale;
|
|
}
|
|
|
|
// sneaking while flying goes down instead
|
|
this.sneaking = input.sneak && !this.flying;
|
|
this.holding_sneak = input.sneak;
|
|
const sin = Math.sin(this.yaw);
|
|
const cos = Math.cos(this.yaw);
|
|
this.#update_sprinting(input, sin, cos);
|
|
if (this.flying) {
|
|
const acceleration = FLY_ACCELERATION * (this.sprinting ? FLY_SPRINT_SPEED : 1);
|
|
this.vx += (sin * input_z + cos * input_x) * acceleration;
|
|
this.vz += (cos * input_z - sin * input_x) * acceleration;
|
|
this.vy += (Number(input.jump) - Number(input.sneak)) * FLY_VERTICAL;
|
|
return;
|
|
}
|
|
// sprinting is no faster in water, swimming is
|
|
const acceleration = this.#in_water
|
|
? WATER_ACCELERATION * (input.sneak ? SNEAK_SPEED : 1)
|
|
: (this.on_ground ? GROUND_ACCELERATION : AIR_ACCELERATION) *
|
|
(input.sneak ? SNEAK_SPEED : this.sprinting ? SPRINT_SPEED : 1);
|
|
|
|
this.vx += (sin * input_z + cos * input_x) * acceleration;
|
|
this.vz += (cos * input_z - sin * input_x) * acceleration;
|
|
|
|
if (this.#in_water && input.sneak) this.vy -= SWIM_DOWN;
|
|
|
|
this.#jump_delay = Math.max(0, this.#jump_delay - 1);
|
|
if (!input.jump) {
|
|
this.#jump_delay = 0;
|
|
} else if (this.#in_water) {
|
|
// like minecraft, jumping in water swims up instead, even standing on the bottom
|
|
this.vy += SWIM_UP;
|
|
} else if (this.on_ground && this.#jump_delay === 0) {
|
|
this.vy += this.jump_force;
|
|
this.#jump_delay = JUMP_DELAY_TICKS;
|
|
// like minecraft, a sprinting jump pushes it forward, the way it's looking
|
|
if (this.sprinting) {
|
|
this.vx -= sin * SPRINT_JUMP_BOOST;
|
|
this.vz -= cos * SPRINT_JUMP_BOOST;
|
|
}
|
|
}
|
|
}
|
|
|
|
// like minecraft's LocalPlayer.aiStep: the key starts sprinting while moving forward, and it keeps sprinting
|
|
// until it stops moving forward, sneaks, gets hungry or runs into a wall, even after letting go of the key
|
|
#update_sprinting(input: MoveInput, sin: number, cos: number) {
|
|
// like minecraft, flying sprints even hungry, and sneaking to go down doesn't stop it
|
|
const can_sprint = input.z < 0 && !this.using_item &&
|
|
(this.flying || (!input.sneak && this.food > SPRINT_MIN_FOOD));
|
|
// how far it's looking into what it hit in the last move, the way it's facing is -sin, -cos
|
|
const into_wall = sin * this.colliding_x + cos * this.colliding_z;
|
|
if (!can_sprint || into_wall > SPRINT_WALL_ANGLE) {
|
|
this.sprinting = false;
|
|
} else if (input.sprint && (!this.#in_water || this.eyes_in_water)) {
|
|
// in water only with the eyes under, to swim, not to sprint along the bottom
|
|
this.sprinting = true;
|
|
}
|
|
}
|
|
|
|
// tells the server where it is right away, before something that depends on it like placing a block, so the
|
|
// server doesn't decide with where it was up to SEND_POSITION_TICKS ago
|
|
send_position_now() {
|
|
this.#send_position(true);
|
|
}
|
|
|
|
#send_position(now: boolean) {
|
|
this.#ticks_since_sent += 1;
|
|
if (!now && this.#ticks_since_sent < SEND_POSITION_TICKS) {
|
|
return;
|
|
}
|
|
this.#ticks_since_sent = 0;
|
|
this.client.connection.send({
|
|
type: "move",
|
|
x: this.x,
|
|
y: this.y,
|
|
z: this.z,
|
|
yaw: this.yaw,
|
|
pitch: this.pitch,
|
|
teleport: this.teleport_id,
|
|
...(this.swimming && { swimming: true }),
|
|
...(this.holding_sneak && { sneaking: true }),
|
|
});
|
|
}
|
|
}
|