Change whole gui system

This commit is contained in:
2026-10-01 16:22:02 -03:00
parent 013a4fbb97
commit deba36260e
48 changed files with 1925 additions and 1302 deletions
+2 -1
View File
@@ -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"
}
}
+68
View File
@@ -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
View File
@@ -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