74 lines
3.4 KiB
TypeScript
74 lines
3.4 KiB
TypeScript
// SPDX-FileCopyrightText: 2026 Paula Wisnievski
|
|
// SPDX-License-Identifier: AGPL-3.0-or-later
|
|
|
|
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, LIQUID_DEFAULTS } 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
|
|
const solid = (x: number, y: number, z: number) => y === -1 || (x === 0 && z === 1 && y === 0);
|
|
|
|
Deno.test("moving diagonally into a corner keeps going the way it's moving most, like minecraft", () => {
|
|
// mostly +z, from just beside the corner: z goes first, so the small x move is what the block stops
|
|
const mostly_z = body(-0.33, 0.65, 1, 4);
|
|
move_body(mostly_z, 0, solid);
|
|
assertAlmostEquals(mostly_z.z, 0.85);
|
|
assertAlmostEquals(mostly_z.x, -0.3);
|
|
|
|
// mostly +x: x goes first, and the small z move is stopped at the block's face
|
|
const mostly_x = body(-0.33, 0.65, 4, 1.2);
|
|
move_body(mostly_x, 0, solid);
|
|
assertAlmostEquals(mostly_x.x, -0.13);
|
|
assertAlmostEquals(mostly_x.z, 0.7);
|
|
});
|
|
|
|
Deno.test("a body on the ground steps up onto something low, not onto a full block, and only with a step height", () => {
|
|
// just walked off something, 0.1 below the top of the block at x 1 that it's walking into
|
|
const next_block = (x: number, y: number, z: number) => x === 1 && y === -1 && z === 0;
|
|
const dipped = () => ({ ...body(0.65, 0.5, 4, 0), y: -0.1 });
|
|
|
|
const stepping = dipped();
|
|
move_body(stepping, -32, next_block, 0.6, true);
|
|
assertAlmostEquals(stepping.y, 0, 1e-9);
|
|
assertAlmostEquals(stepping.x, 0.85);
|
|
|
|
// without a step height it's a wall, and it slides down it
|
|
const item = dipped();
|
|
move_body(item, -32, next_block, 0, true);
|
|
assertAlmostEquals(item.x, 0.7);
|
|
assert(item.y < -0.1);
|
|
|
|
// a full block is too tall to step onto
|
|
const wall = { ...body(0.5, 0.5, 4, 0), y: 0 };
|
|
move_body(wall, -32, (x, y, z) => y === -1 || (x === 1 && y === 0 && z === 0), 0.6, true);
|
|
assertAlmostEquals(wall.x, 0.7);
|
|
assertAlmostEquals(wall.y, 0);
|
|
});
|
|
|
|
Deno.test("liquids are the blocks that say they are one", () => {
|
|
const block = (fields: Partial<BlockRegistry>) => ({ has_collision: false, ...fields }) as BlockRegistry;
|
|
assertEquals(is_liquid_block(block({ liquid: LIQUID_DEFAULTS })), true);
|
|
// no collision and replaceable isn't enough any more, like a plant you can place into
|
|
assertEquals(is_liquid_block(block({ replaceable: true })), 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));
|
|
});
|