Make fishing rods not actually target water
This commit is contained in:
@@ -7,6 +7,7 @@ import {
|
||||
EMPTY_SHAPE,
|
||||
FULL_SHAPE,
|
||||
model_shape,
|
||||
raycast_blocks,
|
||||
type Shape,
|
||||
} from "$/common/block_shapes.ts";
|
||||
import { block_from_json, validate_block } from "$/common/mod_data.ts";
|
||||
@@ -118,3 +119,42 @@ Deno.test("the outline is the edges of the shape's boxes, a little outside them"
|
||||
// edges two boxes share are drawn once
|
||||
assertEquals(shape_edges([...SLAB, ...SLAB]).length, 12);
|
||||
});
|
||||
|
||||
// a world of full blocks at the listed positions, and half height slabs at the others listed
|
||||
function raycast_world(full: number[][], slabs: number[][] = []) {
|
||||
const has = (list: number[][], x: number, y: number, z: number) =>
|
||||
list.some(([bx, by, bz]) => bx === x && by === y && bz === z);
|
||||
return (x: number, y: number, z: number): Shape | undefined =>
|
||||
has(full, x, y, z) ? FULL_SHAPE : has(slabs, x, y, z) ? [[0, 0, 0, 1, 0.5, 1]] : undefined;
|
||||
}
|
||||
|
||||
Deno.test("raycast_blocks finds the first block along the ray, and the side it went in by", () => {
|
||||
const world = raycast_world([[5, 0, 0], [8, 0, 0]]);
|
||||
const east = raycast_blocks([0.5, 0.5, 0.5], [1, 0, 0], 20, world)!;
|
||||
assertEquals([east.x, east.y, east.z, east.face], [5, 0, 0, "west"]);
|
||||
assertAlmostEquals(east.distance, 4.5);
|
||||
|
||||
const west = raycast_blocks([10.5, 0.5, 0.5], [-1, 0, 0], 20, world)!;
|
||||
assertEquals([west.x, west.face], [8, "east"]);
|
||||
assertAlmostEquals(west.distance, 1.5);
|
||||
|
||||
assertEquals(raycast_blocks([0.5, 0.5, 0.5], [1, 0, 0], 4, world), undefined);
|
||||
assertEquals(raycast_blocks([0.5, 0.5, 0.5], [0, 0, 1], 20, world), undefined);
|
||||
});
|
||||
|
||||
Deno.test("raycast_blocks goes diagonally and down onto the top of a block", () => {
|
||||
// it reaches y 0 at x and z 2.7, on the block's top
|
||||
const world = raycast_world([[2, -1, 2]]);
|
||||
const d = 1 / Math.sqrt(3);
|
||||
const hit = raycast_blocks([0.2, 2.5, 0.2], [d, -d, d], 20, world)!;
|
||||
assertEquals([hit.x, hit.y, hit.z, hit.face], [2, -1, 2, "top"]);
|
||||
assertAlmostEquals(hit.distance, 2.5 * Math.sqrt(3));
|
||||
});
|
||||
|
||||
Deno.test("raycast_blocks goes over the empty half of a slab, and not out of a block it starts in", () => {
|
||||
const world = raycast_world([[0, 0, 0], [4, 0, 0]], [[2, 0, 0]]);
|
||||
const over = raycast_blocks([0.5, 0.75, 0.5], [1, 0, 0], 20, world)!;
|
||||
assertEquals([over.x, over.face], [4, "west"]);
|
||||
const into = raycast_blocks([0.5, 0.25, 0.5], [1, 0, 0], 20, world)!;
|
||||
assertEquals([into.x, into.face], [2, "west"]);
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user