This commit is contained in:
2026-10-02 11:07:09 -03:00
parent d5015edfa1
commit 9ca717d2d0
18 changed files with 449 additions and 38 deletions
+97 -7
View File
@@ -1,4 +1,4 @@
import { assert, assertAlmostEquals, assertEquals } from "@std/assert";
import { assert, assertAlmostEquals, assertEquals, assertFalse } from "@std/assert";
import { LocalPlayer } from "$/client/entity/local_player.ts";
import { InputManager } from "$/client/input_manager.ts";
import { Options } from "$/client/options.ts";
@@ -9,7 +9,7 @@ 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 sent: { y: number; swimming?: boolean }[] = [];
const client = {
level: {
has_collision: world?.solid ?? ((_x: number, y: number) => y < 0 || y >= ceiling),
@@ -17,7 +17,10 @@ function setup(ceiling = Infinity, world?: { solid: Blocks; water: Blocks }) {
},
screen: undefined,
options: new Options(),
connection: { send: (message: { type: string; y: number }) => message.type === "move" && sent.push(message) },
connection: {
send: (message: { type: string; y: number; swimming?: boolean }) =>
message.type === "move" && sent.push(message),
},
};
const player = new LocalPlayer(client as unknown as Client, "id", "alice");
player.set_position(0.5, 0, 0.5);
@@ -275,16 +278,14 @@ function speed_after(player: LocalPlayer, ticks: number, keys: string[]) {
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", () => {
Deno.test("in water it sinks slowly, walks slowly, 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);
assertAlmostEquals(speed_after(in_pool(), 60, ["KeyW"]).horizontal, 2, 0.01);
const rising = in_pool();
rising.set_position(0.5, 0, 0.5);
@@ -310,3 +311,92 @@ Deno.test("swimming up against the edge of a pool hops out onto a ledge a block
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}`);
});
// holding these keys for that many ticks
function hold(player: LocalPlayer, ticks: number, keys: string[]) {
for (const key of keys) InputManager.keys_down.add(key);
// like ClientLevel.tick
for (let tick = 0; tick < ticks; tick++) {
player.save_previous_position();
player.tick();
}
InputManager.keys_down.clear();
}
const SWIM = ["KeyW", "ControlLeft"];
Deno.test("sprinting with the eyes under water swims, in the short box with low eyes, and tells the server right away", () => {
const { player, sent } = setup(Infinity, pool(10));
player.set_position(0.5, 3, 0.5);
hold(player, 2, SWIM);
assert(player.swimming);
assertEquals(player.height, 0.8);
assertEquals(sent.at(-1)?.swimming, true);
hold(player, 10, SWIM);
assertAlmostEquals(player.eye_height, 0.6, 0.01);
// level, it doesn't sink, and it's faster than walking in water
const y = player.y;
assertAlmostEquals(speed_after(player, 80, SWIM).horizontal, 4, 0.01);
assertAlmostEquals(player.y, y, 0.01);
// letting go stands it back up, there's room
hold(player, 1, []);
assertFalse(player.swimming);
assertEquals(player.height, 1.79);
assertEquals(sent.at(-1)?.swimming, undefined);
});
Deno.test("it tips over to swim and stands back up over 10 ticks, lying the way it's going", () => {
const { player } = setup(Infinity, pool(10));
player.set_position(0.5, 5, 0.5);
player.pitch = -0.5;
hold(player, 6, SWIM);
assert(player.swim_amount > 0 && player.swim_amount < 1, `at ${player.swim_amount}`);
hold(player, 30, SWIM);
assertEquals(player.swim_amount, 1);
// diving, the body points down
assert(player.swim_pitch < -0.2, `pitch ${player.swim_pitch}`);
// still, it lies the way it looks
player.pitch = 0.3;
hold(player, 100, []);
assertEquals(player.swim_amount, 0);
assertAlmostEquals(player.swim_pitch, 0.3, 0.01);
});
Deno.test("swimming goes where it looks, down and up, but stays in the water at the surface", () => {
const diving = setup(Infinity, pool(10)).player;
diving.set_position(0.5, 5, 0.5);
diving.pitch = -0.5;
hold(diving, 30, SWIM);
assert(diving.swimming && diving.y < 3, `at y ${diving.y}`);
// water up to y 4: it comes up to the surface and swims along it with its head out, its feet in
const rising = setup(Infinity, pool(4)).player;
rising.set_position(0.5, 1, 0.5);
rising.pitch = 0.5;
hold(rising, 100, SWIM);
assert(rising.swimming && rising.in_water, "still swimming in the water");
assert(rising.y > 3 && rising.y < 4, `at y ${rising.y}`);
});
Deno.test("swimming fits through a 1 block gap, and stays lying down in it after letting go", () => {
// water up to y 3, and from z 0 on a ceiling at y 1, so the water there is a tunnel 1 block high
const { player } = setup(Infinity, {
solid: (_x, y, z) => y < 0 || (z < 0 && y >= 1),
water: (_x, y, z) => y >= 0 && (z < 0 ? y < 1 : y < 3),
});
player.set_position(0.5, 0, 0.5);
hold(player, 40, SWIM);
assert(player.z < -2, `stopped at z ${player.z}`);
hold(player, 10, []);
assert(player.swimming);
assertEquals(player.height, 0.8);
});
Deno.test("wading with the eyes out of the water doesn't start sprinting, so it doesn't swim", () => {
const { player } = setup(Infinity, pool(1));
hold(player, 20, SWIM);
assertFalse(player.sprinting);
assertFalse(player.swimming);
});