Basic swimming
This commit is contained in:
+11
-1
@@ -1,4 +1,4 @@
|
||||
import { move_body } from "$/common/physics.ts";
|
||||
import { eyes_in_water, in_water, move_body } from "$/common/physics.ts";
|
||||
import type { ClientLevel } from "../level/client_level.ts";
|
||||
|
||||
// anything that exists in the level and moves, like minecraft's Entity. position is the middle of its feet.
|
||||
@@ -59,6 +59,16 @@ export abstract class Entity {
|
||||
return this.colliding_y === 1;
|
||||
}
|
||||
|
||||
// in water, like the server's Entity.in_water
|
||||
get in_water() {
|
||||
return in_water(this, (x, y, z) => this.level.is_liquid(x, y, z));
|
||||
}
|
||||
|
||||
// its eyes are in water
|
||||
get eyes_in_water() {
|
||||
return eyes_in_water(this, this.eye_height, (x, y, z) => this.level.is_liquid(x, y, z));
|
||||
}
|
||||
|
||||
// after moving each tick. full speed is 5 blocks per second, a bit faster than walking
|
||||
update_walk_animation() {
|
||||
const moved = Math.hypot(this.x - this.prev_x, this.z - this.prev_z);
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import { PLAYER_EYE_HEIGHT, TICK_DELTA } from "$/common/constants.ts";
|
||||
import { back_off_from_edge } from "$/common/physics.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";
|
||||
import { ClientInventories } from "../inventory.ts";
|
||||
@@ -40,6 +40,25 @@ 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;
|
||||
// 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;
|
||||
@@ -79,6 +98,8 @@ export class LocalPlayer extends Player {
|
||||
#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);
|
||||
@@ -93,6 +114,9 @@ export class LocalPlayer extends Player {
|
||||
}
|
||||
|
||||
tick() {
|
||||
this.#in_water = this.in_water;
|
||||
this.gravity = this.#in_water ? WATER_GRAVITY : LAND_GRAVITY;
|
||||
this.drag = this.#in_water ? WATER_DRAG : LAND_DRAG;
|
||||
this.#apply_input(this.client.screen ? NO_INPUT : this.#read_input());
|
||||
this.#prev_eye_height = this.eye_height;
|
||||
const eye_height = this.sneaking ? SNEAK_EYE_HEIGHT : PLAYER_EYE_HEIGHT;
|
||||
@@ -114,7 +138,9 @@ export class LocalPlayer extends Player {
|
||||
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.#send_position(this.on_ground && !was_on_ground);
|
||||
@@ -130,9 +156,20 @@ export class LocalPlayer extends Player {
|
||||
}
|
||||
|
||||
override horizontal_friction() {
|
||||
if (this.#in_water) return 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.has_collision(x, y, z) || this.level.is_liquid(x, y, z);
|
||||
const dy = from_y - this.y + this.vy * TICK_DELTA + WATER_HOP_HEIGHT;
|
||||
if (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;
|
||||
@@ -168,15 +205,23 @@ export class LocalPlayer extends Player {
|
||||
const sin = Math.sin(this.yaw);
|
||||
const cos = Math.cos(this.yaw);
|
||||
this.#update_sprinting(input, sin, cos);
|
||||
const acceleration = (this.on_ground ? GROUND_ACCELERATION : AIR_ACCELERATION) *
|
||||
(input.sneak ? SNEAK_SPEED : this.sprinting ? SPRINT_SPEED : 1);
|
||||
// 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;
|
||||
|
||||
Reference in New Issue
Block a user