267 lines
8.0 KiB
TypeScript
267 lines
8.0 KiB
TypeScript
// SPDX-FileCopyrightText: 2026 Paula Wisnievski
|
|
// SPDX-License-Identifier: AGPL-3.0-or-later
|
|
|
|
import { mat4, vec3 } from "gl-matrix";
|
|
import { SLOT_SIZE } from "$/common/constants.ts";
|
|
import { get_sprite_region } from "$/client/sprites.ts";
|
|
import { BlockRegistry, EverythingRegistry, ItemRegistry } from "$/common/everything_registry.ts";
|
|
import { AssetManager } from "$/client/assets.ts";
|
|
import { ItemStack } from "$/common/inventory.ts";
|
|
import { item_texture } from "$/common/utils.ts";
|
|
import {
|
|
cube_face_texture,
|
|
cube_face_tint,
|
|
type DisplayTransform,
|
|
ITEM_FACE_SHADE,
|
|
item_tint,
|
|
} from "$/common/block_models.ts";
|
|
import { display_matrix, item_block, item_model_of, type ItemModel, sprite_u, sprite_v } from "./item_renderer.ts";
|
|
import {
|
|
draw_rect,
|
|
draw_text,
|
|
draw_texture_region,
|
|
draw_texture_region_skewed,
|
|
push_vertex,
|
|
Texture,
|
|
use_texture,
|
|
} from "$/client/renderer/mod.ts";
|
|
|
|
// the corners keep their size (times scale), the edges stretch along and the middle fills the rest, like minecraft's
|
|
// nine slice gui sprites. borders are in source pixels
|
|
export function draw_nine_slice(
|
|
image: Texture,
|
|
source_x: number,
|
|
source_y: number,
|
|
source_width: number,
|
|
source_height: number,
|
|
left: number,
|
|
right: number,
|
|
top: number,
|
|
bottom: number,
|
|
x: number,
|
|
y: number,
|
|
width: number,
|
|
height: number,
|
|
color = [1, 1, 1, 1],
|
|
scale = 1,
|
|
) {
|
|
// start and size of each column and row, in the source and on screen
|
|
const source_columns = [[source_x, left], [source_x + left, source_width - left - right], [
|
|
source_x + source_width - right,
|
|
right,
|
|
]];
|
|
const source_rows = [[source_y, top], [source_y + top, source_height - top - bottom], [
|
|
source_y + source_height - bottom,
|
|
bottom,
|
|
]];
|
|
const columns = [[x, left * scale], [x + left * scale, width - (left + right) * scale], [
|
|
x + width - right * scale,
|
|
right * scale,
|
|
]];
|
|
const rows = [[y, top * scale], [y + top * scale, height - (top + bottom) * scale], [
|
|
y + height - bottom * scale,
|
|
bottom * scale,
|
|
]];
|
|
|
|
for (let row = 0; row < 3; row++) {
|
|
for (let column = 0; column < 3; column++) {
|
|
const [sx, sw] = source_columns[column];
|
|
const [sy, sh] = source_rows[row];
|
|
const [dx, dw] = columns[column];
|
|
const [dy, dh] = rows[row];
|
|
if (sw > 0 && sh > 0 && dw > 0 && dh > 0) {
|
|
const [r, g, b, a] = color;
|
|
draw_texture_region(image, sx, sy, sw, sh, dx, dy, dw, dh, false, false, r, g, b, a);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
export function draw_item(item: ItemStack, x: number, y: number) {
|
|
const item_info = EverythingRegistry.get<ItemRegistry>("items", item.type_id);
|
|
|
|
if (!item_info) {
|
|
throw Error(`Didn't find item registry for '${item.type_id}'`);
|
|
}
|
|
|
|
// blocks that aren't cubes, like plants, show a flat sprite
|
|
const model = item_model_of(item);
|
|
const block_info = item_block(item);
|
|
if (model) {
|
|
draw_item_model(model, icon_matrix(x, y, model.display.gui));
|
|
} else if (block_info) {
|
|
draw_item_block(block_info, x, y);
|
|
} else {
|
|
draw_item_item(item, item_info, x, y);
|
|
}
|
|
|
|
if (item.max_amount !== 1) {
|
|
draw_text(
|
|
String(item.amount),
|
|
x + SLOT_SIZE + 2 - 11 * 2,
|
|
y + SLOT_SIZE + 2 - 11 * 2,
|
|
1,
|
|
[63 / 255, 63 / 255, 63 / 255],
|
|
);
|
|
draw_text(
|
|
String(item.amount),
|
|
x + SLOT_SIZE - 11 * 2,
|
|
y + SLOT_SIZE - 11 * 2,
|
|
1,
|
|
);
|
|
}
|
|
|
|
// durability bar, only on worn tools: 13 by 2 pixels near the bottom, going from green to red
|
|
const left = item.durability();
|
|
if (left !== undefined && item_info.tool && left < item_info.tool.durability) {
|
|
const fraction = Math.max(0, left / item_info.tool.durability);
|
|
const pixel = SLOT_SIZE / 18;
|
|
draw_rect(x + 3 * pixel, y + 14 * pixel, 13 * pixel, 2 * pixel, [0, 0, 0, 1]);
|
|
draw_rect(x + 3 * pixel, y + 14 * pixel, Math.round(13 * fraction) * pixel, pixel, [
|
|
1 - fraction,
|
|
fraction,
|
|
0,
|
|
1,
|
|
]);
|
|
}
|
|
}
|
|
|
|
// ey its not a bad name
|
|
function draw_item_item(item: ItemStack, item_info: ItemRegistry, x: number, y: number) {
|
|
const texture_id = item_texture(item, item_info);
|
|
const sprite_region = get_sprite_region(texture_id);
|
|
draw_texture_region(
|
|
AssetManager.instance.get("bworld:textures"),
|
|
sprite_region.x,
|
|
sprite_region.y,
|
|
sprite_region.width,
|
|
sprite_region.height,
|
|
x + 3,
|
|
y + 3,
|
|
SLOT_SIZE - 6,
|
|
SLOT_SIZE - 6,
|
|
);
|
|
}
|
|
|
|
// from a model's points to a slot, as screen x and y and depth toward the viewer. with a gui transform, the transform
|
|
// starts from the model's 16 pixels across the slot's middle, y up. without one it's the cube icon's corners (see
|
|
// draw_item_block), so models are drawn at the same size and angle: seen from above the north west corner, west on the
|
|
// left and north on the right
|
|
function icon_matrix(x: number, y: number, gui?: DisplayTransform): mat4 {
|
|
if (gui) {
|
|
const unit = SLOT_SIZE * 16 / 18;
|
|
const base = mat4.fromTranslation(mat4.create(), [x + SLOT_SIZE / 2, y + SLOT_SIZE / 2, 0]);
|
|
return display_matrix(mat4.scale(base, base, [unit, -unit, unit]), gui);
|
|
}
|
|
const size = (SLOT_SIZE - 4 * 2) / 2;
|
|
const cx = x + SLOT_SIZE / 2;
|
|
const cy = y + SLOT_SIZE / 2 - 10;
|
|
// toward the viewer is -x, +y, -z
|
|
return mat4.fromValues(size, -size / 2, -1, 0, 0, -size, 1, 0, -size, -size / 2, -1, 0, cx, cy + size * 1.5, 0, 1);
|
|
}
|
|
|
|
// an item's model in a slot: its faces that face the viewer, back to front
|
|
function draw_item_model(model: ItemModel, matrix: mat4) {
|
|
const atlas = AssetManager.instance.get<Texture>("bworld:textures");
|
|
use_texture(atlas.tex);
|
|
// a mirroring matrix (like the y flip to the screen's y down) turns which way a face's corners go round
|
|
const flip = Math.sign(mat4.determinant(matrix));
|
|
const visible = model.quads
|
|
.map((quad) => {
|
|
const corners = [0, 1, 2, 3].map((i) =>
|
|
vec3.transformMat4(vec3.create(), quad.positions.slice(i * 3, i * 3 + 3) as vec3, matrix)
|
|
);
|
|
const e1 = vec3.subtract(vec3.create(), corners[1], corners[0]);
|
|
const e2 = vec3.subtract(vec3.create(), corners[3], corners[0]);
|
|
const facing = (e1[0] * e2[1] - e1[1] * e2[0]) * flip;
|
|
const depth = corners.reduce((sum, corner) => sum + corner[2], 0);
|
|
return { quad, corners, facing, depth };
|
|
})
|
|
.filter(({ facing }) => facing > 1e-6)
|
|
.sort((a, b) => a.depth - b.depth);
|
|
|
|
for (const { quad, corners } of visible) {
|
|
const region = get_sprite_region(quad.texture);
|
|
const [r, g, b] = model.block ? item_tint(model.block, quad.variables) : [1, 1, 1];
|
|
const shade = quad.shade ? ITEM_FACE_SHADE[quad.face] : 1;
|
|
for (const i of [0, 1, 2, 0, 2, 3]) {
|
|
push_vertex(
|
|
corners[i][0],
|
|
corners[i][1],
|
|
0,
|
|
sprite_u(atlas, region, quad.uvs[i * 2]),
|
|
sprite_v(atlas, region, quad.uvs[i * 2 + 1]),
|
|
shade * r,
|
|
shade * g,
|
|
shade * b,
|
|
1,
|
|
);
|
|
}
|
|
}
|
|
}
|
|
|
|
function draw_item_block(block_info: BlockRegistry, x: number, y: number) {
|
|
const top_texture = cube_face_texture(block_info, "top");
|
|
const front_texture = cube_face_texture(block_info, "front");
|
|
const left_texture = cube_face_texture(block_info, "side");
|
|
|
|
const atlas = AssetManager.instance.get<Texture>("bworld:textures");
|
|
|
|
const top = get_sprite_region(top_texture);
|
|
const front = get_sprite_region(front_texture);
|
|
const left = get_sprite_region(left_texture);
|
|
|
|
const padding = 4;
|
|
const size = (SLOT_SIZE - padding * 2) / 2;
|
|
|
|
const cx = x + SLOT_SIZE / 2;
|
|
const cy = y + SLOT_SIZE / 2 - 10;
|
|
|
|
const half_w = size;
|
|
const half_h = size / 2;
|
|
const height = size;
|
|
|
|
draw_texture_region_skewed(
|
|
atlas,
|
|
top.x,
|
|
top.y,
|
|
top.width,
|
|
top.height,
|
|
[
|
|
{ x: cx, y: cy - half_h },
|
|
{ x: cx + half_w, y: cy },
|
|
{ x: cx, y: cy + half_h },
|
|
{ x: cx - half_w, y: cy },
|
|
],
|
|
...cube_face_tint(block_info, "top"),
|
|
);
|
|
draw_texture_region_skewed(
|
|
atlas,
|
|
left.x,
|
|
left.y,
|
|
left.width,
|
|
left.height,
|
|
[
|
|
{ x: cx - half_w, y: cy },
|
|
{ x: cx, y: cy + half_h },
|
|
{ x: cx, y: cy + half_h + height },
|
|
{ x: cx - half_w, y: cy + height },
|
|
],
|
|
...cube_face_tint(block_info, "side"),
|
|
);
|
|
draw_texture_region_skewed(
|
|
atlas,
|
|
front.x,
|
|
front.y,
|
|
front.width,
|
|
front.height,
|
|
[
|
|
{ x: cx + half_w, y: cy },
|
|
{ x: cx, y: cy + half_h },
|
|
{ x: cx, y: cy + half_h + height },
|
|
{ x: cx + half_w, y: cy + height },
|
|
],
|
|
...cube_face_tint(block_info, "front"),
|
|
);
|
|
}
|