Change whole gui system
This commit is contained in:
@@ -10,6 +10,7 @@
|
||||
"game_version": ">=0.1.0",
|
||||
"credits": "CREDITS.md",
|
||||
"scripts": {
|
||||
"server": "scripts/server.ts"
|
||||
"server": "scripts/server.ts",
|
||||
"client": "scripts/client.ts"
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,68 @@
|
||||
// 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]);
|
||||
}
|
||||
}
|
||||
+92
-115
@@ -1,5 +1,5 @@
|
||||
// the base game's block and item behavior, written against the same api as any other mod
|
||||
import type { BlockRef, Container, EntityRef, Player, ScreenHandle, ServerContext } from "bworld/server";
|
||||
import type { BlockRef, Container, EntityRef, Player, ServerContext } from "bworld/server";
|
||||
|
||||
export function setup(ctx: ServerContext) {
|
||||
register_hoeable(ctx);
|
||||
@@ -51,19 +51,30 @@ function drop_contents(ctx: ServerContext, block: BlockRef) {
|
||||
ctx.containers.delete(container.id);
|
||||
}
|
||||
|
||||
// a chest
|
||||
// the menus' layouts are in gui pixels, like minecraft's: their screens are in scripts/client.ts. the player's
|
||||
// inventory goes at the bottom, its rows from INVENTORY_Y and the hotbar under them
|
||||
const INVENTORY_Y = 84;
|
||||
|
||||
// a chest, like minecraft's ChestMenu: rows of 9 over the inventory
|
||||
function register_storage(ctx: ServerContext) {
|
||||
const menu_type = ctx.menus.register<{ block: BlockRef; rows: number }>("bworld:storage", (menu, args) => {
|
||||
const { block, rows } = args;
|
||||
const container = block_container(ctx, block, rows * 9);
|
||||
menu.title = "Chest";
|
||||
menu.open_data = { rows };
|
||||
for (let slot = 0; slot < rows * 9; slot++) {
|
||||
menu.add_slot(container, slot, { x: 7 + (slot % 9) * 18, y: 17 + Math.floor(slot / 9) * 18 });
|
||||
}
|
||||
menu.add_player_inventory(7, 31 + rows * 18);
|
||||
menu.still_valid_near(block);
|
||||
});
|
||||
|
||||
ctx.components.register_block<{ rows: number }>("bworld:storage", {
|
||||
on_create(block, params) {
|
||||
block_container(ctx, block, params.rows * 9);
|
||||
},
|
||||
on_interact(block, params, player) {
|
||||
const size = params.rows * 9;
|
||||
ctx.ui.open_container(player, {
|
||||
container: block_container(ctx, block, size),
|
||||
layout: Array.from({ length: size }, (_, slot) => ({ slot, x: slot % 9, y: Math.floor(slot / 9) })),
|
||||
rows: params.rows,
|
||||
});
|
||||
menu_type.open(player, { block, rows: params.rows });
|
||||
return true;
|
||||
},
|
||||
on_break(block) {
|
||||
@@ -84,44 +95,26 @@ interface FurnaceData {
|
||||
fuel_max: number;
|
||||
}
|
||||
|
||||
const FURNACE_BARS = [
|
||||
{
|
||||
x: 3.5,
|
||||
y: 1,
|
||||
value: "fuel",
|
||||
max: "fuel_max",
|
||||
direction: "up" as const,
|
||||
empty_texture: "bworld:fire_empty",
|
||||
full_texture: "bworld:fire_full",
|
||||
},
|
||||
{
|
||||
x: 4.5,
|
||||
y: 1,
|
||||
value: "progress",
|
||||
max: "progress_max",
|
||||
direction: "right" as const,
|
||||
empty_texture: "bworld:arrow_empty",
|
||||
full_texture: "bworld:arrow_full",
|
||||
},
|
||||
];
|
||||
|
||||
function register_furnace(ctx: ServerContext) {
|
||||
// who's looking at each furnace, 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 furnace_data = (block: BlockRef): FurnaceData => {
|
||||
const container = block_container(ctx, block, 3);
|
||||
block.data = { progress: 0, progress_max: 0, fuel: 0, fuel_max: 0, ...block.data, container: container.id };
|
||||
return block.data;
|
||||
};
|
||||
|
||||
const show = (screen: ScreenHandle, data: FurnaceData) => {
|
||||
screen.set_property("progress", data.progress);
|
||||
screen.set_property("progress_max", data.progress_max);
|
||||
screen.set_property("fuel", data.fuel);
|
||||
screen.set_property("fuel_max", data.fuel_max);
|
||||
};
|
||||
// like minecraft's FurnaceMenu, the screen shows the fuel left and the progress with bars
|
||||
const menu_type = ctx.menus.register<BlockRef>("bworld:furnace", (menu, block) => {
|
||||
const container = ctx.containers.get(furnace_data(block).container)!;
|
||||
menu.title = "Furnace";
|
||||
menu.add_slot(container, 0, { x: 55, y: 16 });
|
||||
menu.add_slot(container, 1, { x: 55, y: 52, filter: "fuel" });
|
||||
menu.add_slot(container, 2, { x: 115, y: 34, output_only: true });
|
||||
menu.add_player_inventory(7, INVENTORY_Y);
|
||||
for (const field of ["progress", "progress_max", "fuel", "fuel_max"] as const) {
|
||||
menu.add_data(field, () => (block.data as FurnaceData | undefined)?.[field] ?? 0);
|
||||
}
|
||||
menu.still_valid_near(block);
|
||||
});
|
||||
|
||||
// there's somewhere for the result to go
|
||||
const can_smelt = (container: Container, output: { id: string; count: number }) => {
|
||||
@@ -134,22 +127,8 @@ function register_furnace(ctx: ServerContext) {
|
||||
furnace_data(block);
|
||||
},
|
||||
on_interact(block, _params, player) {
|
||||
const data = furnace_data(block);
|
||||
const screen = ctx.ui.open_container(player, {
|
||||
container: ctx.containers.get(data.container)!,
|
||||
layout: [
|
||||
{ slot: 0, x: 3.5, y: 0 },
|
||||
{ slot: 1, x: 3.5, y: 2, filter: "fuel" },
|
||||
{ slot: 2, x: 5.5, y: 1, output_only: true },
|
||||
],
|
||||
rows: 3,
|
||||
bars: FURNACE_BARS,
|
||||
});
|
||||
show(screen, data);
|
||||
const screens = viewers.get(key(block)) ?? new Set();
|
||||
viewers.set(key(block), screens);
|
||||
screens.add(screen);
|
||||
screen.on_close(() => screens.delete(screen));
|
||||
furnace_data(block);
|
||||
menu_type.open(player, block);
|
||||
return true;
|
||||
},
|
||||
on_break(block) {
|
||||
@@ -198,8 +177,6 @@ function register_furnace(ctx: ServerContext) {
|
||||
if (ctx.world.get_state(block.x, block.y, block.z, "lit") !== lit) {
|
||||
ctx.world.set_state(block.x, block.y, block.z, "lit", lit);
|
||||
}
|
||||
|
||||
for (const screen of viewers.get(key(block)) ?? []) show(screen, data);
|
||||
},
|
||||
});
|
||||
}
|
||||
@@ -213,67 +190,67 @@ const ADDITION = 2;
|
||||
const RESULT = 3;
|
||||
|
||||
function register_smithing_table(ctx: ServerContext) {
|
||||
ctx.components.register_block("bworld:smithing_table", {
|
||||
on_interact(_block, _params, player) {
|
||||
const container = ctx.containers.create(4);
|
||||
const menu_type = ctx.menus.register<BlockRef>("bworld:smithing_table", (menu, block, player) => {
|
||||
const container = ctx.containers.create(4);
|
||||
|
||||
// the recipe the inputs make, with enough material
|
||||
const match = () => {
|
||||
const tool = container.get(TOOL);
|
||||
const material = container.get(MATERIAL);
|
||||
const addition = container.get(ADDITION);
|
||||
if (!tool || !material) return undefined;
|
||||
const recipe = ctx.recipes.smithing(tool.id, material.id, addition?.id);
|
||||
if (!recipe || material.count < recipe.material_count) return undefined;
|
||||
return { ...recipe, tool, material, addition };
|
||||
};
|
||||
// the recipe the inputs make, with enough material
|
||||
const match = () => {
|
||||
const tool = container.get(TOOL);
|
||||
const material = container.get(MATERIAL);
|
||||
const addition = container.get(ADDITION);
|
||||
if (!tool || !material) return undefined;
|
||||
const recipe = ctx.recipes.smithing(tool.id, material.id, addition?.id);
|
||||
if (!recipe || material.count < recipe.material_count) return undefined;
|
||||
return { ...recipe, tool, material, addition };
|
||||
};
|
||||
|
||||
// the result slot shows what taking it would give, the upgraded tool keeps the old one's data
|
||||
const show_result = () => {
|
||||
// the result slot shows what taking it would give, the upgraded tool keeps the old one's data
|
||||
const show_result = () => {
|
||||
const recipe = match();
|
||||
const shown = container.get(RESULT);
|
||||
if (!recipe) {
|
||||
if (shown) container.set(RESULT, undefined);
|
||||
} else if (shown?.id !== recipe.result) {
|
||||
container.set(RESULT, { id: recipe.result, count: 1, data: recipe.tool.data });
|
||||
}
|
||||
};
|
||||
const stop = container.on_change((slot) => {
|
||||
if (slot !== RESULT) show_result();
|
||||
});
|
||||
|
||||
menu.title = "Smithing";
|
||||
menu.add_slot(container, TOOL, { x: 25, y: 34 });
|
||||
menu.add_slot(container, MATERIAL, { x: 43, y: 34 });
|
||||
menu.add_slot(container, ADDITION, { x: 61, y: 34 });
|
||||
menu.add_slot(container, RESULT, {
|
||||
x: 115,
|
||||
y: 34,
|
||||
output_only: true,
|
||||
// the inputs are used up in the same step, so they can't be taken out after
|
||||
on_take(item) {
|
||||
const recipe = match();
|
||||
const shown = container.get(RESULT);
|
||||
if (!recipe) {
|
||||
if (shown) container.set(RESULT, undefined);
|
||||
} else if (shown?.id !== recipe.result) {
|
||||
container.set(RESULT, { id: recipe.result, count: 1, data: recipe.tool.data });
|
||||
}
|
||||
};
|
||||
const stop = container.on_change((slot) => {
|
||||
if (slot !== RESULT) show_result();
|
||||
});
|
||||
if (!recipe || recipe.result !== item.id) return false;
|
||||
recipe.tool.count -= 1;
|
||||
recipe.material.count -= recipe.material_count;
|
||||
if (recipe.addition) recipe.addition.count -= 1;
|
||||
return true;
|
||||
},
|
||||
});
|
||||
menu.add_player_inventory(7, INVENTORY_Y);
|
||||
menu.still_valid_near(block);
|
||||
menu.on_close(() => {
|
||||
stop();
|
||||
for (const slot of [TOOL, MATERIAL, ADDITION]) {
|
||||
const item = container.get(slot);
|
||||
if (item) player.give_item(item.id, item.count, item.data);
|
||||
}
|
||||
ctx.containers.delete(container.id);
|
||||
});
|
||||
});
|
||||
|
||||
const screen = ctx.ui.open_container(player, {
|
||||
container,
|
||||
layout: [
|
||||
{ slot: TOOL, x: 1.5, y: 0.5 },
|
||||
{ slot: MATERIAL, x: 2.5, y: 0.5 },
|
||||
{ slot: ADDITION, x: 3.5, y: 0.5 },
|
||||
{
|
||||
slot: RESULT,
|
||||
x: 5.5,
|
||||
y: 0.5,
|
||||
output_only: true,
|
||||
// the inputs are used up in the same step, so they can't be taken out after
|
||||
on_take(item) {
|
||||
const recipe = match();
|
||||
if (!recipe || recipe.result !== item.id) return false;
|
||||
recipe.tool.count -= 1;
|
||||
recipe.material.count -= recipe.material_count;
|
||||
if (recipe.addition) recipe.addition.count -= 1;
|
||||
return true;
|
||||
},
|
||||
},
|
||||
],
|
||||
rows: 2,
|
||||
});
|
||||
screen.on_close(() => {
|
||||
stop();
|
||||
for (const slot of [TOOL, MATERIAL, ADDITION]) {
|
||||
const item = container.get(slot);
|
||||
if (item) player.give_item(item.id, item.count, item.data);
|
||||
}
|
||||
ctx.containers.delete(container.id);
|
||||
});
|
||||
ctx.components.register_block("bworld:smithing_table", {
|
||||
on_interact(block, _params, player) {
|
||||
menu_type.open(player, block);
|
||||
return true;
|
||||
},
|
||||
});
|
||||
@@ -305,7 +282,7 @@ interface CropDrops {
|
||||
bonus?: string;
|
||||
}
|
||||
|
||||
// a crop, like a fabric CropBlock: its "age" state is its stage, and it grows one day every morning its farmland was
|
||||
// a crop, like minecraft's CropBlock: its "age" state is its stage, and it grows one day every morning its farmland was
|
||||
// watered the day before. see docs/modding/blocks/crops.md
|
||||
interface CropParams {
|
||||
// watered mornings from planting to fully grown
|
||||
|
||||
@@ -1,10 +1,13 @@
|
||||
// the curios button next to every inventory screen, or G like curios' keybind, asks the server to show the slots
|
||||
// the curios button next to every inventory screen, or G like curios' keybind, asks the server to open the curios
|
||||
// menu, and its screen
|
||||
import type { ClientContext } from "bworld/client";
|
||||
import { active_slots } from "./slots.ts";
|
||||
|
||||
const GAP = 16;
|
||||
const WIDTH = 120;
|
||||
const HEIGHT = 32;
|
||||
// in gui pixels
|
||||
const GAP = 5;
|
||||
const BUTTON_WIDTH = 40;
|
||||
const BUTTON_HEIGHT = 11;
|
||||
const WIDTH = 176;
|
||||
|
||||
export function setup(ctx: ClientContext) {
|
||||
// no item goes in any slot, so there's nothing to show
|
||||
@@ -12,10 +15,26 @@ export function setup(ctx: ClientContext) {
|
||||
|
||||
ctx.ui.add_inventory_overlay({
|
||||
on_render(g, screen) {
|
||||
const clicked = g.button("Curios", screen.x - WIDTH - GAP, screen.y, WIDTH, HEIGHT);
|
||||
const clicked = g.button("Curios", screen.x - BUTTON_WIDTH - GAP, screen.y, BUTTON_WIDTH, BUTTON_HEIGHT);
|
||||
if (clicked || g.key_pressed("KeyG")) {
|
||||
ctx.net.send("curios:open", {});
|
||||
}
|
||||
},
|
||||
});
|
||||
|
||||
ctx.ui.register_menu_screen("curios:curios", (menu) => {
|
||||
const { rows } = menu.open_data as { rows: number };
|
||||
const height = 114 + rows * 18;
|
||||
return {
|
||||
width: WIDTH,
|
||||
height,
|
||||
on_render_background(g) {
|
||||
g.panel(0, 0, WIDTH, height);
|
||||
},
|
||||
on_render_labels(g) {
|
||||
g.text(menu.title, 8, 6);
|
||||
g.text("Inventory", 8, height - 93);
|
||||
},
|
||||
};
|
||||
});
|
||||
}
|
||||
|
||||
@@ -72,6 +72,27 @@ export function setup(ctx: ServerContext) {
|
||||
applied.delete(player.id);
|
||||
});
|
||||
|
||||
// like curios' CuriosContainer: the slot types in rows of 9, each row centered, over the inventory. the screen is
|
||||
// in scripts/client.ts
|
||||
const rows = Math.ceil(active.length / 9);
|
||||
const menu_type = ctx.menus.register("curios:curios", (menu, _args, player) => {
|
||||
const curios = curios_of(player);
|
||||
menu.title = "Curios";
|
||||
menu.open_data = { rows };
|
||||
for (const [i, slot] of active.entries()) {
|
||||
const row = Math.floor(i / 9);
|
||||
const in_row = Math.min(9, active.length - row * 9);
|
||||
menu.add_slot(curios, SLOTS.indexOf(slot), {
|
||||
x: 7 + ((9 - in_row) / 2 + (i % 9)) * 18,
|
||||
y: 17 + row * 18,
|
||||
filter: (item) => fits(ctx.tags, slot, item.id),
|
||||
icon: `curios:empty_${slot}_slot`,
|
||||
});
|
||||
}
|
||||
menu.add_player_inventory(7, 31 + rows * 18);
|
||||
});
|
||||
|
||||
// the curios button, curios' client asks for its menu like the java mod's packet does
|
||||
ctx.net.on("curios:open", (player) => {
|
||||
if (active.length === 0) return;
|
||||
const curios = curios_of(player);
|
||||
@@ -83,21 +104,7 @@ export function setup(ctx: ServerContext) {
|
||||
curios.set(index, undefined);
|
||||
}
|
||||
}
|
||||
// in rows of 9, each row centered
|
||||
ctx.ui.open_container(player, {
|
||||
container: curios,
|
||||
layout: active.map((slot, i) => {
|
||||
const row = Math.floor(i / 9);
|
||||
const in_row = Math.min(9, active.length - row * 9);
|
||||
return {
|
||||
slot: SLOTS.indexOf(slot),
|
||||
x: (9 - in_row) / 2 + (i % 9),
|
||||
y: row,
|
||||
filter: (item) => fits(ctx.tags, slot, item.id),
|
||||
icon: `curios:empty_${slot}_slot`,
|
||||
};
|
||||
}),
|
||||
});
|
||||
menu_type.open(player, undefined);
|
||||
});
|
||||
|
||||
ctx.events.after.player_death.subscribe(({ player }) => {
|
||||
|
||||
@@ -17,6 +17,7 @@
|
||||
],
|
||||
"credits": "CREDITS.md",
|
||||
"scripts": {
|
||||
"server": "scripts/server.ts"
|
||||
"server": "scripts/server.ts",
|
||||
"client": "scripts/client.ts"
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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);
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
@@ -3,12 +3,11 @@
|
||||
// with any mod's, and draws the ones it doesn't know as their items in, then what comes out
|
||||
import type { ClientContext, Color, Graphics, InventoryScreenView, TagApi } from "bworld/client";
|
||||
|
||||
// items and slots are drawn this big
|
||||
const CELL = 54;
|
||||
const GAP = 16;
|
||||
const TEXT = 22;
|
||||
const LINE = 30;
|
||||
const BUTTON = 32;
|
||||
// in gui pixels. items and slots are drawn this big
|
||||
const CELL = 18;
|
||||
const GAP = 5;
|
||||
const LINE = 10;
|
||||
const BUTTON = 11;
|
||||
// catppuccin mocha, like the engine's gui: crust, pink, subtext 0 and blue
|
||||
const SLOT_COLOR: Color = [0.07, 0.07, 0.11, 0.6];
|
||||
const HOVER_COLOR: Color = [0.96, 0.76, 0.91, 0.35];
|
||||
@@ -67,6 +66,12 @@ class RecipeViewer {
|
||||
if (this.#view && g.key_pressed("Backspace")) this.#back();
|
||||
}
|
||||
|
||||
// the recipe views go with the inventory, it opens again on the item list
|
||||
on_close() {
|
||||
this.#view = undefined;
|
||||
this.#history = [];
|
||||
}
|
||||
|
||||
#open(item: string, mode: Mode) {
|
||||
if (this.#find(item, mode).length === 0) return;
|
||||
if (this.#view) this.#history.push(this.#view);
|
||||
@@ -106,7 +111,7 @@ class RecipeViewer {
|
||||
this.#page = (this.#page + 1) % pages;
|
||||
}
|
||||
const label = `${this.#page + 1}/${pages}`;
|
||||
g.text(label, left + (columns * CELL - g.measure_text(label)) / 2, GAP + (BUTTON - TEXT) / 2);
|
||||
g.text(label, left + (columns * CELL - g.measure_text(label)) / 2, GAP + (BUTTON - g.text_height) / 2);
|
||||
|
||||
let hovered: string | undefined;
|
||||
items.slice(this.#page * per_page, (this.#page + 1) * per_page).forEach((id, i) => {
|
||||
@@ -184,7 +189,7 @@ class RecipeViewer {
|
||||
view.page = (view.page + 1) % page_count;
|
||||
}
|
||||
const label = `${view.page + 1}/${page_count}`;
|
||||
g.text(label, x + (width - g.measure_text(label)) / 2, bottom + (BUTTON - TEXT) / 2, {
|
||||
g.text(label, x + (width - g.measure_text(label)) / 2, bottom + (BUTTON - g.text_height) / 2, {
|
||||
color: LABEL_COLOR,
|
||||
});
|
||||
}
|
||||
@@ -270,7 +275,7 @@ class RecipeViewer {
|
||||
cell(output, x + 5 * CELL, made.length > 1 ? top + i * CELL : top + CELL / 2)
|
||||
);
|
||||
if (typeof recipe.cook_time === "number") {
|
||||
g.text(seconds(recipe.cook_time), x + 5 * CELL, top + CELL * 1.5 + (CELL - TEXT) / 2, {
|
||||
g.text(seconds(recipe.cook_time), x + 5 * CELL, top + CELL * 1.5 + (CELL - g.text_height) / 2, {
|
||||
color: LABEL_COLOR,
|
||||
});
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user