Files
bworld/mods/bworld/scripts/client.ts
T
2026-10-01 16:22:02 -03:00

69 lines
2.1 KiB
TypeScript

// the screens of the base game's menus, see scripts/server.ts for their slots. in gui pixels, like minecraft's
import type { ClientContext, ClientMenu, Graphics, MenuScreen } from "bworld/client";
const WIDTH = 176;
// like minecraft's 176x166 container screens
const HEIGHT = 166;
export function setup(ctx: ClientContext) {
ctx.ui.register_menu_screen("bworld:storage", chest_screen);
ctx.ui.register_menu_screen("bworld:furnace", furnace_screen);
ctx.ui.register_menu_screen("bworld:smithing_table", smithing_screen);
}
// a panel with the menu's title and the inventory's label over its rows, drawing anything else over it
function container_screen(menu: ClientMenu, height: number, draw?: (g: Graphics) => void): MenuScreen {
return {
width: WIDTH,
height,
on_render_background(g) {
g.panel(0, 0, WIDTH, height);
draw?.(g);
},
on_render_labels(g) {
g.text(menu.title, 8, 6);
g.text("Inventory", 8, height - 93);
},
};
}
function chest_screen(menu: ClientMenu): MenuScreen {
const { rows } = menu.open_data as { rows: number };
return container_screen(menu, 114 + rows * 18);
}
function furnace_screen(menu: ClientMenu): MenuScreen {
return container_screen(menu, HEIGHT, (g) => {
// the fuel left burns down, the progress fills to the right
bar(g, 55, 34, "up", menu.data.fuel, menu.data.fuel_max, "bworld:fire_empty", "bworld:fire_full");
bar(g, 85, 34, "right", menu.data.progress, menu.data.progress_max, "bworld:arrow_empty", "bworld:arrow_full");
});
}
function smithing_screen(menu: ClientMenu): MenuScreen {
return container_screen(menu, HEIGHT, (g) => {
g.texture("bworld:arrow_empty", 88, 34, 18, 18);
});
}
// an 18 pixel square progress bar, value / max of full drawn over empty
function bar(
g: Graphics,
x: number,
y: number,
direction: "up" | "right",
value = 0,
max = 0,
empty: string,
full: string,
) {
const size = 18;
const done = max > 0 ? Math.max(0, Math.min(1, value / max)) : 0;
g.texture(empty, x, y, size, size);
if (direction === "right") {
g.texture(full, x, y, size * done, size, [0, 0, done, 1]);
} else {
g.texture(full, x, y + size * (1 - done), size, size * done, [0, 1 - done, 1, done]);
}
}