180 lines
5.0 KiB
TypeScript
180 lines
5.0 KiB
TypeScript
import { SLOT_SIZE, TEXTURE_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 { block_item_texture, cube_face_texture, cube_face_tint } from "$/common/block_models.ts";
|
|
import { draw_text, draw_texture_region, draw_texture_region_skewed, 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}'`);
|
|
}
|
|
|
|
const block_info = item_info.block_id
|
|
? EverythingRegistry.get<BlockRegistry>("blocks", item_info.block_id)
|
|
: undefined;
|
|
// blocks that aren't cubes, like plants, show a flat sprite
|
|
if (block_info && block_item_texture(block_info) === undefined) {
|
|
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,
|
|
);
|
|
}
|
|
}
|
|
|
|
// ey its not a bad name
|
|
function draw_item_item(item: ItemStack, item_info: ItemRegistry, x: number, y: number) {
|
|
let texture_id = "engine:missing";
|
|
if (typeof item_info?.texture_id === "string") {
|
|
texture_id = item_info.texture_id;
|
|
} else if (typeof item_info?.texture_id === "function") {
|
|
texture_id = item_info.texture_id(item);
|
|
}
|
|
const sprite_region = get_sprite_region(texture_id);
|
|
draw_texture_region(
|
|
AssetManager.instance.get("bworld:textures"),
|
|
sprite_region.x * 16,
|
|
sprite_region.y * 16,
|
|
16,
|
|
16,
|
|
x + 3,
|
|
y + 3,
|
|
SLOT_SIZE - 6,
|
|
SLOT_SIZE - 6,
|
|
);
|
|
}
|
|
|
|
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 * TEXTURE_SIZE,
|
|
top.y * TEXTURE_SIZE,
|
|
TEXTURE_SIZE,
|
|
TEXTURE_SIZE,
|
|
[
|
|
{ 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 * TEXTURE_SIZE,
|
|
left.y * TEXTURE_SIZE,
|
|
TEXTURE_SIZE,
|
|
TEXTURE_SIZE,
|
|
[
|
|
{ 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 * TEXTURE_SIZE,
|
|
front.y * TEXTURE_SIZE,
|
|
TEXTURE_SIZE,
|
|
TEXTURE_SIZE,
|
|
[
|
|
{ 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"),
|
|
);
|
|
}
|