Basic swimming
This commit is contained in:
@@ -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}`);
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user