This commit is contained in:
2026-11-14 00:29:34 -03:00
parent 6c6be61dd0
commit 297d87164c
15 changed files with 593 additions and 281 deletions
+3 -1
View File
@@ -2,7 +2,7 @@
// 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
// juicer's visual and the hud, see hud.ts
import { mat4 } from "gl-matrix";
import type {
ClientContext,
@@ -18,6 +18,7 @@ import type {
VisualBlock,
} from "cubi/client";
import { TOOLTIP_PRIORITY } from "cubi/client";
import { register_hud } from "./hud.ts";
import { MINIGAMES } from "./minigames/mod.ts";
import { JUICER_CAPACITY, JUICER_PRESSES } from "./shared.ts";
import { STONECUTTING, stonecutting_options, type StonecuttingRecipe } from "./stonecutting.ts";
@@ -38,6 +39,7 @@ export function setup(ctx: ClientContext) {
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);
register_hud(ctx);
ctx.ui.add_item_tooltip((item, lines) => {
const value = ctx.items.sell_value(item.id, item.data);
+1 -5
View File
@@ -6,6 +6,7 @@
// bworld:curio_modifiers component change the wearer's attributes, like curios' attribute modifiers, and ones with
// bworld:curio_unhurt_effect give them an effect once they've gone a while without being hurt
import type { AttributeModifier, Container, ItemStack, Player, ServerContext, TagApi } from "cubi/server";
import { title } from "./shared.ts";
// where the left column of slots goes, from the inventory screen's left edge. the panel around them ends just left of it
const COLUMNS_X = -45;
@@ -169,11 +170,6 @@ function describe({ attribute, amount, operation }: WornModifier): string {
return `${sign}${value} ${name}`;
}
// "bworld:regeneration" is "Regeneration", like minecraft's effect names
function title(id: string): string {
return id.split(":").at(-1)!.split("_").map((word) => word[0].toUpperCase() + word.slice(1)).join(" ");
}
function roman(level: number): string {
return ["I", "II", "III", "IV", "V", "VI", "VII", "VIII", "IX", "X"][level - 1] ?? `${level}`;
}
+218
View File
@@ -0,0 +1,218 @@
// SPDX-FileCopyrightText: 2026 Paula Wisnievski
// SPDX-License-Identifier: AGPL-3.0-or-later
// what's drawn over the world while playing, like minecraft's gui: the hotbar, hearts, hunger and air, the held item's
// name, overlay messages, the crosshair, and the day, time, biome and season. in gui pixels
import type { ClientContext, Color, Graphics, HudSpace } from "cubi/client";
import { title } from "./shared.ts";
const HOTBAR_WIDTH = 182;
const HOTBAR_HEIGHT = 22;
// each slot is 20 across, the item's slot corner 2 in from the cell's and the selection 1 out
const HOTBAR_CELL = 20;
const HOTBAR_ITEM_INSET = 2;
// above the hotbar, the room minecraft's experience bar takes
const HOTBAR_ROOM = 7;
// hearts, hunger and air are rows of 10 icons, 8 apart. each row takes 10 up, the icon and a gap under it. more max
// health stacks more rows of hearts, closer together when there are many, like minecraft
const ICON_SIZE = 9;
const ICON_SPACING = 8;
const ICONS_PER_ROW = 10;
const ROW_HEIGHT = 10;
const MIN_HEART_ROW_SPACING = 3;
// air goes from 20 down while the head is under water, 2 a bubble like hunger
const MAX_AIR = 20;
// how long the held item's name shows above the hotbar after it changes, and the last part of that it fades out in
const HELD_NAME_MS = 2000;
const HELD_NAME_FADE_MS = 500;
const HELD_NAME_GAP = 4;
// a server's overlay message, above the held item's name, for as long as minecraft's
const OVERLAY_MS = 3000;
const OVERLAY_FADE_MS = 500;
const OVERLAY_GAP = 4;
const CROSSHAIR_SIZE = 15;
// under the crosshair so it shows on bright sky and snow, minecraft inverts the colors behind it instead
const CROSSHAIR_SHADOW: Color = [0, 0, 0, 0.4];
// the attack indicator, like minecraft's: a bar under the crosshair filling up while the attack cooldown runs
const ATTACK_BAR_WIDTH = 16;
const ATTACK_BAR_HEIGHT = 2;
const ATTACK_BAR_GAP = 2;
const ATTACK_BAR_BACK: Color = [0, 0, 0, 0.4];
const ATTACK_BAR_FILL: Color = [1, 1, 1, 0.9];
// the day, time, biome and season in the top left, on a dark box so it reads over the sky
const INFO_MARGIN = 4;
const INFO_PADDING = 3;
const INFO_LINE_GAP = 1;
const INFO_BACK: Color = [0, 0, 0, 0.4];
export function register_hud(ctx: ClientContext) {
ctx.hud.register("bworld:hotbar", { on_render: (g, hud) => render_hotbar(ctx, g, hud) });
ctx.hud.register("bworld:health", { on_render: (g, hud) => render_health(ctx, g, hud) });
ctx.hud.register("bworld:food", { on_render: (g, hud) => render_food(ctx, g, hud) });
ctx.hud.register("bworld:air", { on_render: (g, hud) => render_air(ctx, g, hud) });
// like minecraft: switching slots, or a different item ending up in the selected one, shows its name for a bit
let held = "";
let held_since = -Infinity;
ctx.hud.register("bworld:held_item_name", {
on_render(g, hud) {
const slot = ctx.player.selected_slot;
const item = ctx.player.inventory.get(slot);
const now = performance.now();
if ((item ? `${slot} ${item.id}` : "") !== held) {
held = item ? `${slot} ${item.id}` : "";
held_since = item ? now : -Infinity;
}
const shown_for = now - held_since;
if (!item || shown_for > HELD_NAME_MS) return;
const alpha = Math.min(1, (HELD_NAME_MS - shown_for) / HELD_NAME_FADE_MS);
const y = g.height - hud_top(hud) - HELD_NAME_GAP - g.text_height;
text_centered(g, ctx.items.name(item.id), y, alpha);
},
});
let overlay = "";
let overlay_since = -Infinity;
ctx.hud.on_overlay_message((text) => {
overlay = text;
overlay_since = performance.now();
});
ctx.hud.register("bworld:overlay_message", {
on_render(g, hud) {
const shown_for = performance.now() - overlay_since;
if (shown_for > OVERLAY_MS) return;
const alpha = Math.min(1, (OVERLAY_MS - shown_for) / OVERLAY_FADE_MS);
// above where the held item's name goes
const y = g.height - hud_top(hud) - HELD_NAME_GAP - OVERLAY_GAP - g.text_height * 2;
text_centered(g, overlay, y, alpha);
},
});
ctx.hud.register("bworld:crosshair", { on_render: (g) => render_crosshair(ctx, g) });
ctx.hud.register("bworld:info", { on_render: (g) => render_info(ctx, g) });
}
function render_hotbar(ctx: ClientContext, g: Graphics, hud: HudSpace) {
const x = hotbar_x(g);
const y = g.height - HOTBAR_HEIGHT;
g.sprite("cubi:hotbar", x, y);
g.sprite("cubi:hotbar_selection", x + ctx.player.selected_slot * HOTBAR_CELL - 1, y - 1);
for (let slot = 0; slot < 9; slot++) {
const item = ctx.player.inventory.get(slot);
if (item) g.item(item, x + slot * HOTBAR_CELL + HOTBAR_ITEM_INSET, y + HOTBAR_ITEM_INSET);
}
hud.left_height += HOTBAR_HEIGHT + HOTBAR_ROOM;
hud.right_height += HOTBAR_HEIGHT + HOTBAR_ROOM;
}
// on the left, each heart 2 points, rows going up
function render_health(ctx: ClientContext, g: Graphics, hud: HudSpace) {
const { max_health } = ctx.player;
const health = Math.ceil(ctx.player.health);
const hearts = Math.ceil(max_health / 2);
const rows = Math.ceil(hearts / ICONS_PER_ROW);
const row_spacing = Math.max(ROW_HEIGHT - (rows - 2), MIN_HEART_ROW_SPACING);
const y = g.height - hud.left_height - ROW_HEIGHT;
for (let i = 0; i < hearts; i++) {
const heart_x = hotbar_x(g) + (i % ICONS_PER_ROW) * ICON_SPACING;
const heart_y = y - Math.floor(i / ICONS_PER_ROW) * row_spacing;
g.sprite("cubi:heart_container", heart_x, heart_y);
const fill = icon_fill(health, i);
if (fill) g.sprite(fill === "full" ? "cubi:heart_full" : "cubi:heart_half", heart_x, heart_y);
}
hud.left_height += ROW_HEIGHT + Math.max(0, rows - 1) * row_spacing;
}
// on the right, going right to left. saturation is appleskin's outline over the hunger icons, never more than hunger
function render_food(ctx: ClientContext, g: Graphics, hud: HudSpace) {
const food = Math.ceil(ctx.player.food);
const saturation = Math.min(ctx.player.saturation, ctx.player.food);
const y = g.height - hud.right_height - ROW_HEIGHT;
for (let i = 0; i < ICONS_PER_ROW; i++) {
const x = right_icon_x(g, i);
g.sprite("cubi:food_empty", x, y);
const fill = icon_fill(food, i);
if (fill) g.sprite(fill === "full" ? "cubi:food_full" : "cubi:food_half", x, y);
const saturated = Math.max(0, Math.min(1, (saturation - i * 2) / 2));
if (saturated > 0) {
const quarters = saturated >= 1 ? 4 : saturated > 0.5 ? 3 : saturated > 0.25 ? 2 : 1;
g.sprite(`cubi:saturation_${quarters}`, x, y);
}
}
hud.right_height += ROW_HEIGHT;
}
// bubbles above the hunger, as soon as the head is under, so it shows full before it starts going down, and while it
// fills back up after
function render_air(ctx: ClientContext, g: Graphics, hud: HudSpace) {
const air = ctx.player.air;
if (!ctx.player.is_underwater() && air >= MAX_AIR) return;
const y = g.height - hud.right_height - ROW_HEIGHT;
for (let i = 0; i < ICONS_PER_ROW; i++) {
const x = right_icon_x(g, i);
g.sprite("cubi:air_empty", x, y);
const fill = icon_fill(air, i);
if (fill) g.sprite(fill === "full" ? "cubi:air_full" : "cubi:air_half", x, y);
}
hud.right_height += ROW_HEIGHT;
}
function render_crosshair(ctx: ClientContext, g: Graphics) {
const x = Math.floor((g.width - CROSSHAIR_SIZE) / 2);
const y = Math.floor((g.height - CROSSHAIR_SIZE) / 2);
g.sprite("cubi:crosshair", x + 1, y + 1, { color: CROSSHAIR_SHADOW });
g.sprite("cubi:crosshair", x, y);
// only while it's charging, like minecraft
const strength = ctx.player.attack_strength();
if (strength >= 1) return;
const bar_x = Math.floor((g.width - ATTACK_BAR_WIDTH) / 2);
const bar_y = y + CROSSHAIR_SIZE + ATTACK_BAR_GAP;
g.rect(bar_x, bar_y, ATTACK_BAR_WIDTH, ATTACK_BAR_HEIGHT, ATTACK_BAR_BACK);
g.rect(bar_x, bar_y, ATTACK_BAR_WIDTH * strength, ATTACK_BAR_HEIGHT, ATTACK_BAR_FILL);
}
function render_info(ctx: ClientContext, g: Graphics) {
const { day, hour } = ctx.world;
const minute = Math.round((hour % 1) * 60);
const { biome, season } = ctx.player;
const lines = [
`Day ${day + 1}`,
`${String(Math.floor(hour)).padStart(2, "0")}:${String(minute).padStart(2, "0")}`,
biome === undefined ? "Unknown" : title(biome),
season === undefined ? "Unknown" : title(season),
];
const line_height = g.text_height + INFO_LINE_GAP;
const width = Math.max(...lines.map((line) => g.measure_text(line))) + INFO_PADDING * 2;
const height = lines.length * line_height - INFO_LINE_GAP + INFO_PADDING * 2;
g.rect(INFO_MARGIN, INFO_MARGIN, width, height, INFO_BACK);
lines.forEach((line, i) => {
g.text(line, INFO_MARGIN + INFO_PADDING, INFO_MARGIN + INFO_PADDING + i * line_height, { shadow: true });
});
}
function hotbar_x(g: Graphics) {
return Math.floor((g.width - HOTBAR_WIDTH) / 2);
}
// icon i of a row on the right, counting from the right end of the hotbar
function right_icon_x(g: Graphics, i: number) {
return hotbar_x(g) + HOTBAR_WIDTH - i * ICON_SPACING - ICON_SIZE;
}
// the top of the rows of icons above the hotbar, from the bottom of the window
function hud_top(hud: HudSpace) {
return Math.max(hud.left_height, hud.right_height);
}
// text across the middle of the window at y, with a shadow so it reads over bright sky and snow
function text_centered(g: Graphics, text: string, y: number, alpha: number) {
g.text(text, (g.width - g.measure_text(text)) / 2, y, { color: [1, 1, 1, alpha], shadow: true });
}
// whether icon i of a value, 2 points an icon, is full, half or empty
function icon_fill(value: number, i: number): "full" | "half" | undefined {
if (value >= i * 2 + 2) return "full";
if (value === i * 2 + 1) return "half";
return undefined;
}
+5
View File
@@ -205,3 +205,8 @@ function register_juice_and_wine(ctx: SharedContext) {
},
});
}
// "bworld:regeneration" is "Regeneration", like minecraft's effect names, biomes' and seasons' too
export function title(id: string): string {
return id.split(":").at(-1)!.split("_").map((word) => word[0].toUpperCase() + word.slice(1)).join(" ");
}