Swimming
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
import { PLAYER_EYE_HEIGHT, TICK_DELTA } from "$/common/constants.ts";
|
||||
import { PLAYER_EYE_HEIGHT, PLAYER_HEIGHT, PLAYER_SWIM_EYE_HEIGHT, TICK_DELTA } from "$/common/constants.ts";
|
||||
import { back_off_from_edge, box_is_free } from "$/common/physics.ts";
|
||||
import type { Client } from "../client.ts";
|
||||
import { InputManager } from "../input_manager.ts";
|
||||
@@ -55,6 +55,10 @@ const SWIM_DOWN = 0.04 / TICK_DELTA;
|
||||
// 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;
|
||||
// on land, the entity's own
|
||||
const LAND_GRAVITY = -32;
|
||||
const LAND_DRAG = 0.98;
|
||||
@@ -115,11 +119,21 @@ export class LocalPlayer extends Player {
|
||||
|
||||
tick() {
|
||||
this.#in_water = this.in_water;
|
||||
this.gravity = this.#in_water ? WATER_GRAVITY : LAND_GRAVITY;
|
||||
const was_swimming = this.swimming;
|
||||
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.#in_water ? LAND_GRAVITY : this.swimming ? 0 : WATER_GRAVITY;
|
||||
this.drag = this.#in_water ? WATER_DRAG : LAND_DRAG;
|
||||
this.#apply_input(this.client.screen ? NO_INPUT : this.#read_input());
|
||||
if (this.swimming && this.#in_water) this.#steer(input);
|
||||
this.#prev_eye_height = this.eye_height;
|
||||
const eye_height = this.sneaking ? SNEAK_EYE_HEIGHT : PLAYER_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;
|
||||
@@ -143,7 +157,43 @@ export class LocalPlayer extends Player {
|
||||
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.#send_position(this.on_ground && !was_on_ground);
|
||||
this.update_swim_animation();
|
||||
this.#send_position((this.on_ground && !was_on_ground) || this.swimming !== was_swimming);
|
||||
}
|
||||
|
||||
// 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.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 (box_is_free(standing, 0, 0, 0, (x, y, z) => this.level.has_collision(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_liquid(
|
||||
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
|
||||
@@ -156,7 +206,7 @@ export class LocalPlayer extends Player {
|
||||
}
|
||||
|
||||
override horizontal_friction() {
|
||||
if (this.#in_water) return WATER_DRAG;
|
||||
if (this.#in_water) return this.swimming ? SWIM_DRAG : WATER_DRAG;
|
||||
return this.on_ground ? GROUND_FRICTION : AIR_FRICTION;
|
||||
}
|
||||
|
||||
@@ -241,7 +291,8 @@ export class LocalPlayer extends Player {
|
||||
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) {
|
||||
} 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;
|
||||
}
|
||||
}
|
||||
@@ -266,6 +317,7 @@ export class LocalPlayer extends Player {
|
||||
yaw: this.yaw,
|
||||
pitch: this.pitch,
|
||||
teleport: this.teleport_id,
|
||||
...(this.swimming && { swimming: true }),
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user