Basic swimming

This commit is contained in:
2026-10-01 21:12:30 -03:00
parent 6e5703a482
commit d5015edfa1
10 changed files with 203 additions and 20 deletions
+30
View File
@@ -172,6 +172,20 @@ export function back_off_from_edge(
return [dx, dz];
}
// whether the body's box, moved by dx, dy and dz, would touch nothing blocked, like minecraft's Entity.isFree
export function box_is_free(
body: Body,
dx: number,
dy: number,
dz: number,
is_blocked: (x: number, y: number, z: number) => boolean,
): boolean {
const half = body.width / 2;
const min = [body.x - half + dx, body.y + dy, body.z - half + dz];
const max = [body.x + half + dx, body.y + body.height + dy, body.z + half + dz];
return !overlaps_solid(min, max, is_blocked);
}
function overlaps_solid(min: number[], max: number[], is_solid: (x: number, y: number, z: number) => boolean) {
for (let x = Math.floor(min[0] + EPSILON); x <= Math.floor(max[0] - EPSILON); x++) {
for (let y = Math.floor(min[1] + EPSILON); y <= Math.floor(max[1] - EPSILON); y++) {
@@ -182,3 +196,19 @@ function overlaps_solid(min: number[], max: number[], is_solid: (x: number, y: n
}
return false;
}
// in water like minecraft's wasTouchingWater, close enough: its feet or the middle of its box are in liquid
export function in_water(body: Body, is_liquid: (x: number, y: number, z: number) => boolean): boolean {
const x = Math.floor(body.x);
const z = Math.floor(body.z);
return is_liquid(x, Math.floor(body.y), z) || is_liquid(x, Math.floor(body.y + body.height / 2), z);
}
// its eyes are in liquid, so it can't breathe
export function eyes_in_water(
body: Body,
eye_height: number,
is_liquid: (x: number, y: number, z: number) => boolean,
): boolean {
return is_liquid(Math.floor(body.x), Math.floor(body.y + eye_height), Math.floor(body.z));
}