Fences
This commit is contained in:
+74
-24
@@ -1,9 +1,16 @@
|
||||
// a block's shape, like minecraft's VoxelShape: the boxes it's made of. it's what the player points at and what the
|
||||
// cracks are drawn on when it breaks (minecraft's getShape), and what entities bump into when it has collision
|
||||
// (getCollisionShape). blocks get theirs from the boxes of their model, or set "shape" like minecraft's Block.box
|
||||
import { block_variant, element_rotation, find_model, type ModelJson } from "./block_models.ts";
|
||||
import {
|
||||
block_parts,
|
||||
element_rotation,
|
||||
find_model,
|
||||
looks_by_state,
|
||||
type ModelJson,
|
||||
states_of,
|
||||
} from "./block_models.ts";
|
||||
import { type BlockRegistry, EverythingRegistry } from "./everything_registry.ts";
|
||||
import { get_state_value } from "./utils.ts";
|
||||
import { STATE_SHIFT } from "./constants.ts";
|
||||
|
||||
// min x y z then max x y z, in block space (0 to 1)
|
||||
export type Box = readonly [number, number, number, number, number, number];
|
||||
@@ -41,43 +48,70 @@ export function model_shape(model: ModelJson, y_rotation = 0): Shape {
|
||||
return boxes.map((box) => turn(box, y_rotation));
|
||||
}
|
||||
|
||||
// boxes in pixels, like a block's "shape" ([x1, y1, z1, x2, y2, z2], 0 to 16), turned like the model is
|
||||
export function pixel_shape(boxes: number[][], y_rotation = 0): Shape {
|
||||
return boxes.map((box) => turn(clamp(box.map((v) => v / 16)), y_rotation));
|
||||
// boxes in pixels, like a block's "shape" ([x1, y1, z1, x2, y2, z2], 0 to 16), turned like the model is. a collision
|
||||
// shape can go up to 24 pixels high, like minecraft's fences, so nothing jumps over them
|
||||
export function pixel_shape(boxes: number[][], y_rotation = 0, top = 1): Shape {
|
||||
return boxes.map((box) => turn(clamp(box.map((v) => v / 16), top), y_rotation));
|
||||
}
|
||||
|
||||
// what the block is at a value with state bits: its "shape" when it (or a variant) sets one, otherwise its model's
|
||||
// how high a collision shape can go, in blocks
|
||||
export const COLLISION_TOP = 1.5;
|
||||
|
||||
// what the block is at a value with state bits: each part's (or the variant's) "shape" when it sets one, otherwise
|
||||
// its model's boxes
|
||||
export function block_shape(block: BlockRegistry, states?: Record<string, number>): Shape {
|
||||
const variant = block_variant(block, states);
|
||||
if (variant.shape) return pixel_shape(variant.shape, variant.y);
|
||||
const model = find_model(variant.model, Object.fromEntries(EverythingRegistry.entries<ModelJson>("models")));
|
||||
return model ? model_shape(model, variant.y) : FULL_SHAPE;
|
||||
const models = Object.fromEntries(EverythingRegistry.entries<ModelJson>("models"));
|
||||
return block_parts(block, states).flatMap((part) => {
|
||||
if (part.shape) return pixel_shape(part.shape, part.y);
|
||||
const model = find_model(part.model, models);
|
||||
return model ? model_shape(model, part.y) : FULL_SHAPE;
|
||||
});
|
||||
}
|
||||
|
||||
// what entities bump into, like getCollisionShape: each part's "collision_shape" when it sets one, otherwise its shape
|
||||
export function block_collision_shape(block: BlockRegistry, states?: Record<string, number>): Shape {
|
||||
if (!block.has_collision) return EMPTY_SHAPE;
|
||||
const parts = block_parts(block, states);
|
||||
if (parts.every((part) => !part.collision_shape)) return block_shape(block, states);
|
||||
const models = Object.fromEntries(EverythingRegistry.entries<ModelJson>("models"));
|
||||
return parts.flatMap((part) => {
|
||||
if (part.collision_shape) return pixel_shape(part.collision_shape, part.y, COLLISION_TOP);
|
||||
if (part.shape) return pixel_shape(part.shape, part.y);
|
||||
const model = find_model(part.model, models);
|
||||
return model ? model_shape(model, part.y) : FULL_SHAPE;
|
||||
});
|
||||
}
|
||||
|
||||
// shapes by block, then by value with state bits, worked out the first time they're asked for
|
||||
const cache = new WeakMap<BlockRegistry, Map<number, Shape>>();
|
||||
const shapes_cache = new WeakMap<BlockRegistry, Map<number, Shape>>();
|
||||
const collision_cache = new WeakMap<BlockRegistry, Map<number, Shape>>();
|
||||
|
||||
// the block's shape at a value from the world, see block_shape
|
||||
export function shape_at(block: BlockRegistry, value: number): Shape {
|
||||
function cached(
|
||||
cache: WeakMap<BlockRegistry, Map<number, Shape>>,
|
||||
block: BlockRegistry,
|
||||
value: number,
|
||||
make: (block: BlockRegistry, states?: Record<string, number>) => Shape,
|
||||
): Shape {
|
||||
let shapes = cache.get(block);
|
||||
if (!shapes) cache.set(block, shapes = new Map());
|
||||
const key = block.variants ? value : 0;
|
||||
const by_state = looks_by_state(block);
|
||||
const key = by_state ? value >>> STATE_SHIFT : 0;
|
||||
let shape = shapes.get(key);
|
||||
if (!shape) {
|
||||
let states: Record<string, number> | undefined;
|
||||
if (block.variants && block.states) {
|
||||
states = {};
|
||||
for (const state of block.states) states[state.name] = get_state_value(value, block, state.name)!;
|
||||
}
|
||||
shape = block_shape(block, states);
|
||||
shape = make(block, by_state ? states_of(block, value) : undefined);
|
||||
shapes.set(key, shape);
|
||||
}
|
||||
return shape;
|
||||
}
|
||||
|
||||
// what entities bump into, like getCollisionShape: the shape, or nothing for blocks with "collision": false
|
||||
// the block's shape at a value from the world, see block_shape
|
||||
export function shape_at(block: BlockRegistry, value: number): Shape {
|
||||
return cached(shapes_cache, block, value, block_shape);
|
||||
}
|
||||
|
||||
// what entities bump into at a value from the world, see block_collision_shape. nothing for "collision": false
|
||||
export function collision_shape_at(block: BlockRegistry, value: number): Shape {
|
||||
return block.has_collision ? shape_at(block, value) : EMPTY_SHAPE;
|
||||
return cached(collision_cache, block, value, block_collision_shape);
|
||||
}
|
||||
|
||||
// how far along the ray (origin plus direction times distance) it first goes into one of the boxes of a block at
|
||||
@@ -119,8 +153,24 @@ export function clip_shape(
|
||||
return closest;
|
||||
}
|
||||
|
||||
function clamp(box: number[]): Box {
|
||||
const c = box.map((v) => Math.min(Math.max(v, 0), 1));
|
||||
// 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 {
|
||||
const axis = face === "west" || face === "east" ? 0 : face === "bottom" || face === "top" ? 1 : 2;
|
||||
const high = face === "east" || face === "top" || face === "south";
|
||||
const [a, b] = [(axis + 1) % 3, (axis + 2) % 3];
|
||||
const touching = shape.filter((box) => high ? box[axis + 3] >= 1 - FLAT && box[axis] < 1 : box[axis] <= FLAT);
|
||||
for (let i = 0; i < 16; i++) {
|
||||
for (let j = 0; j < 16; j++) {
|
||||
const [u, v] = [(i + 0.5) / 16, (j + 0.5) / 16];
|
||||
if (!touching.some((box) => box[a] <= u && u <= box[a + 3] && box[b] <= v && v <= box[b + 3])) return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
function clamp(box: number[], top = 1): Box {
|
||||
const c = box.map((v, i) => Math.min(Math.max(v, 0), i % 3 === 1 ? top : 1));
|
||||
return [c[0], c[1], c[2], c[3], c[4], c[5]];
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user