Make fishing rods not actually target water
This commit is contained in:
@@ -153,6 +153,53 @@ export function clip_shape(
|
||||
return closest;
|
||||
}
|
||||
|
||||
export type ShapeFace = "west" | "east" | "bottom" | "top" | "north" | "south";
|
||||
|
||||
export interface BlockRaycastHit {
|
||||
x: number;
|
||||
y: number;
|
||||
z: number;
|
||||
// the side of the block's box it went in by
|
||||
face: ShapeFace;
|
||||
distance: number;
|
||||
}
|
||||
|
||||
// the first block a ray from origin goes into within max_distance, cell by cell, like minecraft's Level.clip.
|
||||
// direction is a unit vector, and shape_of says what each block there is, undefined for nothing to hit. a block the
|
||||
// ray starts inside isn't hit
|
||||
export function raycast_blocks(
|
||||
origin: readonly number[],
|
||||
direction: readonly number[],
|
||||
max_distance: number,
|
||||
shape_of: (x: number, y: number, z: number) => Shape | undefined,
|
||||
): BlockRaycastHit | undefined {
|
||||
const cell = origin.map(Math.floor);
|
||||
const step = direction.map(Math.sign);
|
||||
// how far along the ray it takes to cross a whole cell on each axis, and how far until it crosses the next one
|
||||
const delta = direction.map((d) => d === 0 ? Infinity : Math.abs(1 / d));
|
||||
const next = [0, 1, 2].map((axis) =>
|
||||
direction[axis] === 0
|
||||
? Infinity
|
||||
: (step[axis] > 0 ? cell[axis] + 1 - origin[axis] : origin[axis] - cell[axis]) * delta[axis]
|
||||
);
|
||||
for (;;) {
|
||||
const shape = shape_of(cell[0], cell[1], cell[2]);
|
||||
const hit = shape && clip_shape(shape, cell[0], cell[1], cell[2], [...origin], [...direction], max_distance);
|
||||
if (hit) {
|
||||
const face: ShapeFace = hit.axis === 0
|
||||
? (hit.sign > 0 ? "west" : "east")
|
||||
: hit.axis === 1
|
||||
? (hit.sign > 0 ? "bottom" : "top")
|
||||
: (hit.sign > 0 ? "north" : "south");
|
||||
return { x: cell[0], y: cell[1], z: cell[2], face, distance: hit.distance };
|
||||
}
|
||||
const axis = next[0] < next[1] ? (next[0] < next[2] ? 0 : 2) : (next[1] < next[2] ? 1 : 2);
|
||||
if (next[axis] > max_distance) return undefined;
|
||||
cell[axis] += step[axis];
|
||||
next[axis] += delta[axis];
|
||||
}
|
||||
}
|
||||
|
||||
// whether the shape's boxes cover the whole of one of the block's sides, like minecraft's Block.isFaceFull. checked
|
||||
// pixel by pixel, which is as fine as shapes in pixels get
|
||||
export function covers_face(shape: Shape, face: "west" | "east" | "bottom" | "top" | "north" | "south"): boolean {
|
||||
|
||||
Reference in New Issue
Block a user