217 lines
8.8 KiB
TypeScript
217 lines
8.8 KiB
TypeScript
// SPDX-FileCopyrightText: 2026 Paula Wisnievski
|
|
// SPDX-License-Identifier: AGPL-3.0-or-later
|
|
|
|
// the screens of the base game's menus, see scripts/server.ts for their slots. in gui pixels, like minecraft's. and the
|
|
// juicer's visual
|
|
import { mat4 } from "gl-matrix";
|
|
import type {
|
|
ClientContext,
|
|
ClientMenu,
|
|
Color,
|
|
FluidStack,
|
|
Graphics,
|
|
Instance,
|
|
Instances,
|
|
ItemStack,
|
|
MenuScreen,
|
|
Visual,
|
|
VisualBlock,
|
|
} from "bworld/client";
|
|
import { MINIGAMES } from "./minigames/mod.ts";
|
|
import { JUICER_CAPACITY, JUICER_PRESSES } from "./shared.ts";
|
|
import { stonecutting_options, stonecutting_recipes } from "./stonecutting.ts";
|
|
|
|
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:crabpot", (menu) => container_screen(menu, HEIGHT));
|
|
ctx.ui.register_menu_screen("bworld:worm_bin", input_output_screen);
|
|
ctx.ui.register_menu_screen("bworld:keg", input_output_screen);
|
|
ctx.ui.register_menu_screen("bworld:smithing_table", smithing_screen);
|
|
ctx.ui.register_menu_screen("bworld:stonecutter", (menu) => stonecutter_screen(ctx, menu));
|
|
ctx.ui.register_menu_screen("bworld:fishing_minigame", fishing_minigame_screen);
|
|
ctx.render.register_visual("bworld:juicer", juicer_visual);
|
|
}
|
|
|
|
// the middle of the bottom copy of what's in the juicer, just over its floor, and how much higher each next one is
|
|
const JUICER_ITEM_Y = 4 / 16;
|
|
const JUICER_COPY_RISE = 1 / 48;
|
|
// how many more copies a stack this much bigger shows
|
|
const JUICER_ITEMS_A_COPY = 16;
|
|
// the juice fills the 12 by 12 pixels inside its walls from its floor up, a pixel deep while there's any and 12 when
|
|
// it's full
|
|
const JUICER_INSIDE = [2, 14];
|
|
const JUICER_DEPTH = 12;
|
|
// how far its press goes down, from where it rests to the floor, where the press that makes the juice would take it. it
|
|
// goes a share of the way for each press toward the next juice, and back up once that's made
|
|
const JUICER_PRESS_TRAVEL = 6 / 16;
|
|
|
|
// the juicer's press, what's in it and its juice, by its "presses", "stack" and "tank" client data (see register_juicer
|
|
// in scripts/server.ts). the press is its own model, not part of the block's, so it can go down as it's pressed. the
|
|
// items are drawn like create's basin draws its ingredients: tipped over and turned, with a copy more for every
|
|
// JUICER_ITEMS_A_COPY. each copy is turned further and a bit higher than the one under it, so they pile up
|
|
function juicer_visual(block: VisualBlock, instances: Instances): Visual {
|
|
const press = instances.transformed("bworld:juicer_press");
|
|
const press_target = () => JUICER_PRESS_TRAVEL * (block.get_client_data<number>("presses") ?? 0) / JUICER_PRESSES;
|
|
// how far down the press is, and where the presses say it goes, which it catches up with a quarter of the way each
|
|
// tick. one made mid way starts there
|
|
let target = press_target();
|
|
let down: number | undefined;
|
|
let last: number | undefined;
|
|
let shown: Instance[] = [];
|
|
const update = () => {
|
|
target = press_target();
|
|
for (const instance of shown) instance.delete();
|
|
shown = [];
|
|
const tank = block.get_client_data<FluidStack>("tank");
|
|
if (tank) {
|
|
const juice = instances.fluid(tank.fluid);
|
|
const [low, high] = JUICER_INSIDE;
|
|
const depth = Math.min(JUICER_DEPTH, Math.max(1, JUICER_DEPTH * tank.amount / JUICER_CAPACITY));
|
|
juice.set_box([low, low, low], [high, low + depth, high]);
|
|
shown.push(juice);
|
|
}
|
|
const stack = block.get_client_data<ItemStack>("stack");
|
|
const copies = stack ? 1 + Math.floor(stack.count / JUICER_ITEMS_A_COPY) : 0;
|
|
shown.push(...Array.from({ length: copies }, (_, i) => {
|
|
const copy = instances.item(stack!);
|
|
const matrix = mat4.fromTranslation(mat4.create(), [0.5, JUICER_ITEM_Y + i * JUICER_COPY_RISE, 0.5]);
|
|
mat4.rotateY(matrix, matrix, (35 + i * 72) * Math.PI / 180);
|
|
mat4.rotateX(matrix, matrix, 65 * Math.PI / 180);
|
|
mat4.translate(matrix, matrix, [-0.5, -0.5, -0.5]);
|
|
copy.set_transform(matrix);
|
|
return copy;
|
|
}));
|
|
};
|
|
update();
|
|
return {
|
|
update,
|
|
on_frame(time) {
|
|
const ticks = last === undefined ? 0 : time - last;
|
|
last = time;
|
|
if (down === target) return;
|
|
down = down === undefined ? target : down + (target - down) * Math.min(1, ticks / 4);
|
|
if (Math.abs(target - down) < 1 / 256) down = target;
|
|
press.set_transform([1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, -down, 0, 1]);
|
|
},
|
|
};
|
|
}
|
|
|
|
// 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
|
|
const { fuel, fuel_max, progress, progress_max } = menu.data;
|
|
g.progress("bworld:fire_empty", "bworld:fire_full", 55, 34, 18, 18, fuel / fuel_max, "up");
|
|
g.progress("bworld:arrow_empty", "bworld:arrow_full", 85, 34, 18, 18, progress / progress_max);
|
|
});
|
|
}
|
|
|
|
// its input on the left, an arrow to what it makes on the right, like the worm bin's slots and the keg's tanks
|
|
function input_output_screen(menu: ClientMenu): MenuScreen {
|
|
return container_screen(menu, HEIGHT, (g) => {
|
|
g.texture("bworld:arrow_empty", 85, 34, 18, 18);
|
|
});
|
|
}
|
|
|
|
function smithing_screen(menu: ClientMenu): MenuScreen {
|
|
return container_screen(menu, HEIGHT, (g) => {
|
|
g.texture("bworld:arrow_empty", 88, 34, 18, 18);
|
|
});
|
|
}
|
|
|
|
// what the input can be cut into, as a grid of buttons that scrolls when there are more than fit. a button is the
|
|
// recipe's place in that list, which the server's menu reads the same way, see stonecutting.ts
|
|
const CUT_X = 52;
|
|
const CUT_Y = 14;
|
|
const CUT_COLUMNS = 4;
|
|
const CUT_ROWS = 3;
|
|
const CUT_WIDTH = 16;
|
|
const CUT_HEIGHT = 18;
|
|
const SCROLLBAR_X = 119;
|
|
const SCROLLBAR_WIDTH = 12;
|
|
const SCROLLER_HEIGHT = 15;
|
|
const TRACK_COLOR: Color = [0.55, 0.55, 0.55, 1];
|
|
const SCROLLER_COLOR: Color = [1, 1, 1, 1];
|
|
const SCROLLER_OFF_COLOR: Color = [0.78, 0.78, 0.78, 1];
|
|
const SELECTED_COLOR: Color = [1, 1, 1, 0.45];
|
|
|
|
function stonecutter_screen(ctx: ClientContext, menu: ClientMenu): MenuScreen {
|
|
const recipes = stonecutting_recipes(ctx);
|
|
// the first row shown, and whether the scroller is being dragged
|
|
let row = 0;
|
|
let dragging = false;
|
|
return container_screen(menu, HEIGHT, (g) => {
|
|
const options = stonecutting_options(recipes, menu.item(0)?.id);
|
|
const rows_hidden = Math.max(0, Math.ceil(options.length / CUT_COLUMNS) - CUT_ROWS);
|
|
const track_height = CUT_ROWS * CUT_HEIGHT;
|
|
const { mouse } = g;
|
|
const over = (x: number, y: number, w: number, h: number) =>
|
|
mouse.x >= x && mouse.y >= y && mouse.x < x + w && mouse.y < y + h;
|
|
|
|
if (mouse.pressed && rows_hidden > 0 && over(SCROLLBAR_X, CUT_Y, SCROLLBAR_WIDTH, track_height)) {
|
|
dragging = true;
|
|
}
|
|
if (!mouse.down) dragging = false;
|
|
if (dragging) {
|
|
const along = (mouse.y - CUT_Y - SCROLLER_HEIGHT / 2) / (track_height - SCROLLER_HEIGHT);
|
|
row = Math.round(Math.max(0, Math.min(1, along)) * rows_hidden);
|
|
}
|
|
row = Math.min(row, rows_hidden);
|
|
const scroller_y = CUT_Y + (rows_hidden > 0 ? (row / rows_hidden) * (track_height - SCROLLER_HEIGHT) : 0);
|
|
g.rect(SCROLLBAR_X, CUT_Y, SCROLLBAR_WIDTH, track_height, TRACK_COLOR);
|
|
g.rect(
|
|
SCROLLBAR_X,
|
|
scroller_y,
|
|
SCROLLBAR_WIDTH,
|
|
SCROLLER_HEIGHT,
|
|
rows_hidden > 0 ? SCROLLER_COLOR : SCROLLER_OFF_COLOR,
|
|
);
|
|
|
|
for (let i = 0; i < CUT_COLUMNS * CUT_ROWS; i++) {
|
|
const index = row * CUT_COLUMNS + i;
|
|
const recipe = options[index];
|
|
if (!recipe) break;
|
|
const x = CUT_X + (i % CUT_COLUMNS) * CUT_WIDTH;
|
|
const y = CUT_Y + Math.floor(i / CUT_COLUMNS) * CUT_HEIGHT;
|
|
if (g.button("", x, y, CUT_WIDTH, CUT_HEIGHT)) menu.click_button(index);
|
|
if (index === menu.data.selected) g.rect(x, y, CUT_WIDTH, CUT_HEIGHT, SELECTED_COLOR);
|
|
// an item is drawn in an 18 pixel slot, the button is 16 wide
|
|
g.item(recipe.output.id, x - 1, y, recipe.output.count);
|
|
if (over(x, y, CUT_WIDTH, CUT_HEIGHT)) g.item_tooltip(recipe.output.id, mouse.x, mouse.y);
|
|
}
|
|
});
|
|
}
|
|
|
|
// a fishing minigame's screen, by the minigame's name, see minigames/mod.ts
|
|
function fishing_minigame_screen(menu: ClientMenu): MenuScreen {
|
|
const { minigame, fish } = menu.open_data as { minigame: string; fish: ItemStack };
|
|
const screen = MINIGAMES[minigame]?.screen;
|
|
if (!screen) throw new Error(`the fishing minigame ${minigame} has no screen`);
|
|
return screen(menu, fish);
|
|
}
|