Basic swimming
This commit is contained in:
@@ -150,6 +150,12 @@ export interface BlockRegistry {
|
||||
compiled_states?: CompiledStateDefinition[];
|
||||
}
|
||||
|
||||
// water, and any other block entities pass through and players can place into. what entities swim in, and where
|
||||
// falling doesn't hurt
|
||||
export function is_liquid_block(block: BlockRegistry | undefined): boolean {
|
||||
return block !== undefined && !block.has_collision && block.replaceable === true;
|
||||
}
|
||||
|
||||
// what a tool is made of, like minecraft's ToolMaterial
|
||||
export interface ToolProperties {
|
||||
// like "pickaxe", matched against blocks' tool_to_break
|
||||
|
||||
@@ -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));
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user