Basic swimming

This commit is contained in:
2026-10-01 21:12:30 -03:00
parent 6e5703a482
commit d5015edfa1
10 changed files with 203 additions and 20 deletions
+65 -3
View File
@@ -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
View File
@@ -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));
});