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