Change whole gui system
This commit is contained in:
@@ -0,0 +1,52 @@
|
||||
// the cooking pot's screen, like the java mod's CookingPotScreen. its slots are in scripts/server.ts, in gui pixels
|
||||
import type { ClientContext, Graphics } from "bworld/client";
|
||||
|
||||
const WIDTH = 176;
|
||||
const HEIGHT = 166;
|
||||
|
||||
export function setup(ctx: ClientContext) {
|
||||
ctx.ui.register_menu_screen("farmersdelight:cooking_pot", (menu) => ({
|
||||
width: WIDTH,
|
||||
height: HEIGHT,
|
||||
on_render_background(g) {
|
||||
g.panel(0, 0, WIDTH, HEIGHT);
|
||||
// lit under the ingredients while something heats the pot, the arrow fills as the meal cooks
|
||||
bar(g, 47, 53, "up", menu.data.heated, 1, "bworld:fire_empty", "bworld:fire_full");
|
||||
bar(
|
||||
g,
|
||||
95,
|
||||
25,
|
||||
"right",
|
||||
menu.data.progress,
|
||||
menu.data.progress_max,
|
||||
"bworld:arrow_empty",
|
||||
"bworld:arrow_full",
|
||||
);
|
||||
},
|
||||
on_render_labels(g) {
|
||||
g.text(menu.title, 8, 6);
|
||||
g.text("Inventory", 8, 73);
|
||||
},
|
||||
}));
|
||||
}
|
||||
|
||||
// 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]);
|
||||
}
|
||||
}
|
||||
@@ -2,7 +2,7 @@
|
||||
// CookingPotBlockEntity, cooks its recipes while something hot is under it, keeps the meal, and serves it into the
|
||||
// container the meal needs (a bowl), or straight out when it needs none. the cutting board, like its
|
||||
// CuttingBoardBlockEntity, holds a stack of anything and cuts it one at a time with the right tool
|
||||
import type { BlockRef, Container, ItemStack, ScreenHandle, ServerContext } from "bworld/server";
|
||||
import type { BlockRef, Container, ItemStack, ServerContext } from "bworld/server";
|
||||
|
||||
export function setup(ctx: ServerContext) {
|
||||
register_stove(ctx);
|
||||
@@ -91,27 +91,6 @@ interface PotData {
|
||||
meal_container: string;
|
||||
}
|
||||
|
||||
const POT_BARS = [
|
||||
{
|
||||
x: 2.2,
|
||||
y: 2,
|
||||
value: "heat",
|
||||
max: "heat_max",
|
||||
direction: "up" as const,
|
||||
empty_texture: "bworld:fire_empty",
|
||||
full_texture: "bworld:fire_full",
|
||||
},
|
||||
{
|
||||
x: 4.6,
|
||||
y: 0.5,
|
||||
value: "progress",
|
||||
max: "progress_max",
|
||||
direction: "right" as const,
|
||||
empty_texture: "bworld:arrow_empty",
|
||||
full_texture: "bworld:arrow_full",
|
||||
},
|
||||
];
|
||||
|
||||
// an ingredient: an item, or a #tag of items any of which will do
|
||||
function item_or_tag(ctx: ServerContext, where: string, id: unknown) {
|
||||
if (typeof id === "string" && id.startsWith("#")) {
|
||||
@@ -186,9 +165,6 @@ function matches(ingredients: string[][], items: string[], used = new Set<number
|
||||
|
||||
function register_cooking_pot(ctx: ServerContext) {
|
||||
const recipes = load_recipes(ctx);
|
||||
// who's looking at each pot, by position, to keep their bars moving
|
||||
const viewers = new Map<string, Set<ScreenHandle>>();
|
||||
const key = (block: BlockRef) => `${block.x},${block.y},${block.z}`;
|
||||
|
||||
const pot_data = (block: BlockRef): PotData => {
|
||||
const existing = block.data?.container && ctx.containers.get(block.data.container);
|
||||
@@ -204,12 +180,24 @@ function register_cooking_pot(ctx: ServerContext) {
|
||||
return state === undefined || ctx.world.get_state(block.x, block.y - 1, block.z, state) === 1;
|
||||
};
|
||||
|
||||
const show = (screen: ScreenHandle, data: PotData, heated: boolean) => {
|
||||
screen.set_property("progress", data.cook_time);
|
||||
screen.set_property("progress_max", data.cook_time_total);
|
||||
screen.set_property("heat", heated ? 1 : 0);
|
||||
screen.set_property("heat_max", 1);
|
||||
};
|
||||
// like the java mod's CookingPotMenu, in gui pixels: the ingredients in 2 rows of 3, the meal, and the containers
|
||||
// and served meal under it. the screen is in scripts/client.ts
|
||||
const menu_type = ctx.menus.register<BlockRef>("farmersdelight:cooking_pot", (menu, block) => {
|
||||
const container = ctx.containers.get(pot_data(block).container)!;
|
||||
menu.title = "Cooking Pot";
|
||||
for (let slot = 0; slot < INGREDIENT_SLOTS; slot++) {
|
||||
menu.add_slot(container, slot, { x: 29 + (slot % 3) * 18, y: 16 + Math.floor(slot / 3) * 18 });
|
||||
}
|
||||
// the meal stays in the pot until it's served
|
||||
menu.add_slot(container, MEAL_SLOT, { x: 122, y: 25, output_only: true, on_take: () => false });
|
||||
menu.add_slot(container, CONTAINER_SLOT, { x: 90, y: 52 });
|
||||
menu.add_slot(container, OUTPUT_SLOT, { x: 122, y: 52, output_only: true });
|
||||
menu.add_player_inventory(7, 84);
|
||||
menu.add_data("progress", () => (block.data as PotData | undefined)?.cook_time ?? 0);
|
||||
menu.add_data("progress_max", () => (block.data as PotData | undefined)?.cook_time_total ?? 0);
|
||||
menu.add_data("heated", () => is_heated(block) ? 1 : 0);
|
||||
menu.still_valid_near(block);
|
||||
});
|
||||
|
||||
// there's room in the meal slot for what it makes
|
||||
const can_cook = (container: Container, recipe: CookingRecipe) => {
|
||||
@@ -275,26 +263,7 @@ function register_cooking_pot(ctx: ServerContext) {
|
||||
return true;
|
||||
}
|
||||
|
||||
const screen = ctx.ui.open_container(player, {
|
||||
container,
|
||||
layout: [
|
||||
...Array.from(
|
||||
{ length: INGREDIENT_SLOTS },
|
||||
(_, slot) => ({ slot, x: 1.2 + slot % 3, y: Math.floor(slot / 3) }),
|
||||
),
|
||||
// the meal stays in the pot until it's served
|
||||
{ slot: MEAL_SLOT, x: 6.4, y: 0.5, output_only: true, on_take: () => false },
|
||||
{ slot: CONTAINER_SLOT, x: 4.6, y: 2 },
|
||||
{ slot: OUTPUT_SLOT, x: 6.4, y: 2, output_only: true },
|
||||
],
|
||||
rows: 3,
|
||||
bars: POT_BARS,
|
||||
});
|
||||
show(screen, data, is_heated(block));
|
||||
const screens = viewers.get(key(block)) ?? new Set();
|
||||
viewers.set(key(block), screens);
|
||||
screens.add(screen);
|
||||
screen.on_close(() => screens.delete(screen));
|
||||
menu_type.open(player, block);
|
||||
return true;
|
||||
},
|
||||
on_break(block) {
|
||||
@@ -324,8 +293,6 @@ function register_cooking_pot(ctx: ServerContext) {
|
||||
data.cook_time = Math.max(0, data.cook_time - 2);
|
||||
}
|
||||
serve(data, container);
|
||||
|
||||
for (const screen of viewers.get(key(block)) ?? []) show(screen, data, heated);
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user