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;
|
||||
|
||||
@@ -7,6 +7,7 @@ import {
|
||||
block_light_opacity,
|
||||
BlockRegistry,
|
||||
EverythingRegistry,
|
||||
is_liquid_block,
|
||||
RENDER_LAYERS,
|
||||
RenderLayer,
|
||||
} from "$/common/everything_registry.ts";
|
||||
@@ -302,6 +303,12 @@ export class ClientLevel {
|
||||
return block === VOID || (block !== AIR && (this.#blocks[block]?.has_collision ?? true));
|
||||
}
|
||||
|
||||
// water, see is_liquid_block. unloaded chunks aren't
|
||||
is_liquid(x: number, y: number, z: number) {
|
||||
const block = this.get_block(x, y, z);
|
||||
return block !== VOID && block !== AIR && is_liquid_block(this.#blocks[block]);
|
||||
}
|
||||
|
||||
// only changes what this client shows, drops and everything else happen on the server
|
||||
break_block(x: number, y: number, z: number) {
|
||||
const block_chunk_x = Math.floor(x / CHUNK_SIZE);
|
||||
|
||||
@@ -150,6 +150,12 @@ export interface BlockRegistry {
|
||||
compiled_states?: CompiledStateDefinition[];
|
||||
}
|
||||
|
||||
// water, and any other block entities pass through and players can place into. what entities swim in, and where
|
||||
// falling doesn't hurt
|
||||
export function is_liquid_block(block: BlockRegistry | undefined): boolean {
|
||||
return block !== undefined && !block.has_collision && block.replaceable === true;
|
||||
}
|
||||
|
||||
// what a tool is made of, like minecraft's ToolMaterial
|
||||
export interface ToolProperties {
|
||||
// like "pickaxe", matched against blocks' tool_to_break
|
||||
|
||||
@@ -172,6 +172,20 @@ export function back_off_from_edge(
|
||||
return [dx, dz];
|
||||
}
|
||||
|
||||
// whether the body's box, moved by dx, dy and dz, would touch nothing blocked, like minecraft's Entity.isFree
|
||||
export function box_is_free(
|
||||
body: Body,
|
||||
dx: number,
|
||||
dy: number,
|
||||
dz: number,
|
||||
is_blocked: (x: number, y: number, z: number) => boolean,
|
||||
): boolean {
|
||||
const half = body.width / 2;
|
||||
const min = [body.x - half + dx, body.y + dy, body.z - half + dz];
|
||||
const max = [body.x + half + dx, body.y + body.height + dy, body.z + half + dz];
|
||||
return !overlaps_solid(min, max, is_blocked);
|
||||
}
|
||||
|
||||
function overlaps_solid(min: number[], max: number[], is_solid: (x: number, y: number, z: number) => boolean) {
|
||||
for (let x = Math.floor(min[0] + EPSILON); x <= Math.floor(max[0] - EPSILON); x++) {
|
||||
for (let y = Math.floor(min[1] + EPSILON); y <= Math.floor(max[1] - EPSILON); y++) {
|
||||
@@ -182,3 +196,19 @@ function overlaps_solid(min: number[], max: number[], is_solid: (x: number, y: n
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
// in water like minecraft's wasTouchingWater, close enough: its feet or the middle of its box are in liquid
|
||||
export function in_water(body: Body, is_liquid: (x: number, y: number, z: number) => boolean): boolean {
|
||||
const x = Math.floor(body.x);
|
||||
const z = Math.floor(body.z);
|
||||
return is_liquid(x, Math.floor(body.y), z) || is_liquid(x, Math.floor(body.y + body.height / 2), z);
|
||||
}
|
||||
|
||||
// its eyes are in liquid, so it can't breathe
|
||||
export function eyes_in_water(
|
||||
body: Body,
|
||||
eye_height: number,
|
||||
is_liquid: (x: number, y: number, z: number) => boolean,
|
||||
): boolean {
|
||||
return is_liquid(Math.floor(body.x), Math.floor(body.y + eye_height), Math.floor(body.z));
|
||||
}
|
||||
|
||||
@@ -7,7 +7,7 @@
|
||||
import { TICKS_PER_SECOND } from "$/common/constants.ts";
|
||||
import type { BlockRegistry } from "$/common/everything_registry.ts";
|
||||
import type { ItemStack } from "$/common/inventory.ts";
|
||||
import { type Collisions, move_body } from "$/common/physics.ts";
|
||||
import { type Collisions, eyes_in_water, in_water, move_body } from "$/common/physics.ts";
|
||||
import type { EntityData, EntityEvent, EntityInfo } from "$/common/protocol.ts";
|
||||
import type { ServerPlayer } from "../player.ts";
|
||||
|
||||
@@ -239,15 +239,12 @@ export abstract class Entity {
|
||||
}
|
||||
|
||||
update_in_water() {
|
||||
const x = Math.floor(this.x);
|
||||
const z = Math.floor(this.z);
|
||||
this.in_water = this.level.is_liquid(x, Math.floor(this.y), z) ||
|
||||
this.level.is_liquid(x, Math.floor(this.y + this.height / 2), z);
|
||||
this.in_water = in_water(this, (x, y, z) => this.level.is_liquid(x, y, z));
|
||||
}
|
||||
|
||||
// its eyes are in water, so it can't breathe
|
||||
get eyes_in_water() {
|
||||
return this.level.is_liquid(Math.floor(this.x), Math.floor(this.y + this.eye_height), Math.floor(this.z));
|
||||
return eyes_in_water(this, this.eye_height, (x, y, z) => this.level.is_liquid(x, y, z));
|
||||
}
|
||||
|
||||
// falls and moves by its velocity for one tick (see move_body), and counts how far it fell. returns what it hit
|
||||
|
||||
@@ -3,6 +3,7 @@
|
||||
// when those move or change, and forgets them when they leave it
|
||||
import { CHUNK_SIZE } from "$/common/constants.ts";
|
||||
import { is_within_view } from "$/common/chunk_streaming.ts";
|
||||
import { is_liquid_block } from "$/common/everything_registry.ts";
|
||||
import type { ItemStack } from "$/common/inventory.ts";
|
||||
import type { EntityEvent, EntityInfo, ServerMessage } from "$/common/protocol.ts";
|
||||
import type { ServerPlayer } from "../player.ts";
|
||||
@@ -89,8 +90,7 @@ export class EntityManager implements EntityLevel {
|
||||
}
|
||||
|
||||
is_liquid(x: number, y: number, z: number) {
|
||||
const info = this.#host.world.get_block_info(x, y, z);
|
||||
return info !== undefined && !info.has_collision && info.replaceable === true;
|
||||
return is_liquid_block(this.#host.world.get_block_info(x, y, z));
|
||||
}
|
||||
|
||||
get_block_info(x: number, y: number, z: number) {
|
||||
|
||||
@@ -10,7 +10,7 @@ import {
|
||||
TICK_DELTA,
|
||||
TICKS_PER_SECOND,
|
||||
} from "$/common/constants.ts";
|
||||
import { BlockRegistry, EverythingRegistry, ItemRegistry } from "$/common/everything_registry.ts";
|
||||
import { BlockRegistry, EverythingRegistry, is_liquid_block, ItemRegistry } from "$/common/everything_registry.ts";
|
||||
import { Container, ItemData, ItemStack } from "$/common/inventory.ts";
|
||||
import {
|
||||
AIR_ID,
|
||||
@@ -1446,8 +1446,7 @@ export class GameServer {
|
||||
|
||||
// in a block entities pass through and players can place into, like water
|
||||
#in_liquid(x: number, y: number, z: number) {
|
||||
const info = this.world.get_block_info(Math.floor(x), Math.floor(y), Math.floor(z));
|
||||
return info !== undefined && !info.has_collision && info.replaceable === true;
|
||||
return is_liquid_block(this.world.get_block_info(Math.floor(x), Math.floor(y), Math.floor(z)));
|
||||
}
|
||||
|
||||
// right click with food: it's eaten if they're hungry, or it's always edible
|
||||
|
||||
@@ -4,11 +4,17 @@ import { InputManager } from "$/client/input_manager.ts";
|
||||
import { Options } from "$/client/options.ts";
|
||||
import type { Client } from "$/client/client.ts";
|
||||
|
||||
// a player on flat ground at y 0, maybe with a ceiling, and the positions it sends
|
||||
function setup(ceiling = Infinity) {
|
||||
type Blocks = (x: number, y: number, z: number) => boolean;
|
||||
|
||||
// a player on flat ground at y 0, maybe with a ceiling, and the positions it sends. or in a world of its own: what's
|
||||
// solid and what's water
|
||||
function setup(ceiling = Infinity, world?: { solid: Blocks; water: Blocks }) {
|
||||
const sent: { y: number }[] = [];
|
||||
const client = {
|
||||
level: { has_collision: (_x: number, y: number) => y < 0 || y >= ceiling },
|
||||
level: {
|
||||
has_collision: world?.solid ?? ((_x: number, y: number) => y < 0 || y >= ceiling),
|
||||
is_liquid: world?.water ?? (() => false),
|
||||
},
|
||||
screen: undefined,
|
||||
options: new Options(),
|
||||
connection: { send: (message: { type: string; y: number }) => message.type === "move" && sent.push(message) },
|
||||
@@ -248,3 +254,59 @@ Deno.test("stepping from the edge of a block onto the one diagonal to it doesn't
|
||||
assertEquals(player.y, 0);
|
||||
assert(player.on_ground);
|
||||
});
|
||||
|
||||
// a pool from y 0 to water_top, on a floor at y -1, with a wall at z -1 from y 0 to wall_top, its face at z 0. water
|
||||
// over the wall when it's lower
|
||||
function pool(water_top: number, wall_top = 1) {
|
||||
const wall = (y: number, z: number) => z <= -1 && y >= 0 && y < wall_top;
|
||||
return {
|
||||
solid: (_x: number, y: number, z: number) => y < 0 || wall(y, z),
|
||||
water: (_x: number, y: number, z: number) => y >= 0 && y < water_top && !wall(y, z),
|
||||
};
|
||||
}
|
||||
|
||||
// how fast it's going in blocks a second, from its last tick
|
||||
function speed_after(player: LocalPlayer, ticks: number, keys: string[]) {
|
||||
for (const key of keys) InputManager.keys_down.add(key);
|
||||
for (let tick = 0; tick < ticks; tick++) player.tick();
|
||||
const { x, y, z } = player;
|
||||
player.tick();
|
||||
InputManager.keys_down.clear();
|
||||
return { horizontal: Math.hypot(player.x - x, player.z - z) * 20, vertical: (player.y - y) * 20 };
|
||||
}
|
||||
|
||||
Deno.test("in water it sinks slowly, walks slowly, sprints no faster, swims up holding jump and down sneaking", () => {
|
||||
const in_pool = () => {
|
||||
const { player } = setup(Infinity, pool(10));
|
||||
player.set_position(0.5, 2, 0.5);
|
||||
return player;
|
||||
};
|
||||
assertAlmostEquals(speed_after(in_pool(), 60, []).vertical, -0.45, 0.01);
|
||||
const walking = speed_after(in_pool(), 60, ["KeyW"]).horizontal;
|
||||
assertAlmostEquals(walking, 2, 0.01);
|
||||
assertAlmostEquals(speed_after(in_pool(), 60, ["KeyW", "ControlLeft"]).horizontal, walking, 0.01);
|
||||
|
||||
const rising = in_pool();
|
||||
rising.set_position(0.5, 0, 0.5);
|
||||
assertAlmostEquals(speed_after(rising, 30, ["Space"]).vertical, 3.55, 0.01);
|
||||
|
||||
const sinking = in_pool();
|
||||
sinking.set_position(0.5, 9, 0.5);
|
||||
assertAlmostEquals(speed_after(sinking, 30, ["ShiftLeft"]).vertical, -4.45, 0.01);
|
||||
// holding both, they cancel out and it sinks like holding neither
|
||||
assertAlmostEquals(speed_after(in_pool(), 60, ["Space", "ShiftLeft"]).vertical, -0.45, 0.01);
|
||||
});
|
||||
|
||||
Deno.test("swimming up against the edge of a pool hops out onto a ledge a block above the water", () => {
|
||||
// swimming up alone only gets the feet a little above the water
|
||||
const { player } = setup(Infinity, pool(3, 4));
|
||||
player.set_position(0.5, 1, 0.5);
|
||||
for (let tick = 0; tick < 60; tick++) speed_after(player, 0, ["KeyW", "Space"]);
|
||||
assert(player.z < -0.5 && player.y >= 4, `ended at z ${player.z}, y ${player.y}`);
|
||||
|
||||
// not against a wall under water
|
||||
const under = setup(Infinity, pool(10)).player;
|
||||
under.set_position(0.5, 0, 0.5);
|
||||
for (let tick = 0; tick < 60; tick++) speed_after(under, 0, ["KeyW"]);
|
||||
assert(under.z > 0 && under.y < 1, `went over it to z ${under.z}, y ${under.y}`);
|
||||
});
|
||||
|
||||
+29
-2
@@ -1,5 +1,6 @@
|
||||
import { assert, assertAlmostEquals } from "@std/assert";
|
||||
import { move_body } from "$/common/physics.ts";
|
||||
import { assert, assertAlmostEquals, assertEquals } from "@std/assert";
|
||||
import { eyes_in_water, in_water, move_body } from "$/common/physics.ts";
|
||||
import { type BlockRegistry, is_liquid_block } from "$/common/everything_registry.ts";
|
||||
|
||||
const body = (x: number, z: number, vx: number, vz: number) => ({ x, y: 0, z, vx, vy: 0, vz, width: 0.6, height: 1.8 });
|
||||
// a block at x 0 to 1, z 1 to 2, on a floor
|
||||
@@ -41,3 +42,29 @@ Deno.test("a body on the ground steps up onto something low, not onto a full blo
|
||||
assertAlmostEquals(wall.x, 0.7);
|
||||
assertAlmostEquals(wall.y, 0);
|
||||
});
|
||||
|
||||
Deno.test("water is a block with no collision that can be placed into", () => {
|
||||
const block = (has_collision: boolean, replaceable?: boolean) => ({ has_collision, replaceable }) as BlockRegistry;
|
||||
assertEquals(is_liquid_block(block(false, true)), true);
|
||||
assertEquals(is_liquid_block(block(true, true)), false);
|
||||
assertEquals(is_liquid_block(block(false)), false);
|
||||
assertEquals(is_liquid_block(undefined), false);
|
||||
});
|
||||
|
||||
Deno.test("a body is in water by its feet or the middle of its box, and its eyes by its eye height", () => {
|
||||
// water up to y 1, two blocks deep
|
||||
const water = (_x: number, y: number, _z: number) => y === 0 || y === -1;
|
||||
const at = (y: number) => ({ ...body(0.5, 0.5, 0, 0), y });
|
||||
|
||||
// standing in it up to the waist
|
||||
assert(in_water(at(0), water));
|
||||
assert(!eyes_in_water(at(0), 1.6, water));
|
||||
// sunk to the bottom, the eyes too
|
||||
assert(in_water(at(-1), water) && eyes_in_water(at(-1), 0.9, water));
|
||||
// standing on the bottom of a 1 deep pool: only the feet are in it
|
||||
const pool = (_x: number, y: number, _z: number) => y === 0;
|
||||
assert(in_water(at(0), pool));
|
||||
// with the feet below it, the middle of its box is in it
|
||||
assert(in_water(at(-0.5), pool));
|
||||
assert(!in_water(at(1), pool));
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user