44 lines
1.9 KiB
TypeScript
44 lines
1.9 KiB
TypeScript
import { assert, assertAlmostEquals } from "@std/assert";
|
|
import { move_body } from "$/common/physics.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);
|
|
});
|