Player api

This commit is contained in:
2026-10-13 22:59:48 -03:00
parent b34e97e487
commit a10b15a3fd
22 changed files with 581 additions and 89 deletions
+22 -4
View File
@@ -38,6 +38,8 @@ 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
@@ -102,6 +104,11 @@ export class LocalPlayer extends Player {
// 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;
@@ -111,7 +118,10 @@ export class LocalPlayer extends Player {
#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
@@ -139,6 +149,7 @@ export class LocalPlayer extends Player {
// 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
@@ -185,7 +196,10 @@ export class LocalPlayer extends Player {
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.#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
@@ -277,12 +291,14 @@ export class LocalPlayer extends Player {
let { x: input_x, z: input_z } = input;
const size = Math.hypot(input_x, input_z);
if (size > 0) {
input_x /= size;
input_z /= size;
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);
@@ -325,7 +341,8 @@ export class LocalPlayer extends Player {
// 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.flying || (!input.sneak && this.food > SPRINT_MIN_FOOD));
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) {
@@ -357,6 +374,7 @@ export class LocalPlayer extends Player {
pitch: this.pitch,
teleport: this.teleport_id,
...(this.swimming && { swimming: true }),
...(this.holding_sneak && { sneaking: true }),
});
}
}