Files
bworld/client/gui/gui_player_inventory.ts
T
2026-10-02 13:16:50 -03:00

88 lines
3.5 KiB
TypeScript

import { GuiMenuScreen } from "./gui_screen.ts";
import type { LocalPlayer } from "../entity/local_player.ts";
import { EverythingRegistry } from "$/common/everything_registry.ts";
import type { EffectJson } from "$/common/mod_data.ts";
import { TICKS_PER_SECOND } from "$/common/constants.ts";
import { display_name } from "$/common/utils.ts";
import { ClientMessage, PLAYER_MENU_EXTRA } from "$/common/protocol.ts";
import { player_menu } from "../menu.ts";
import type { ModGraphics } from "./mod_graphics.ts";
const WIDTH = 176;
const HEIGHT = 166;
const SLOT = 18;
// around the slots, like minecraft's
const MARGIN = 7;
// effect icons in a row on a panel of their own, just above the inventory's
const ICON = 16;
const ICON_GAP = 2;
const ICON_MARGIN = 4;
const TIME_COLOR: [number, number, number, number] = [0.66, 0.66, 0.66, 1];
// menu 0's screen: the crafting grid over the inventory, and any slots mods added, with the player's effects above.
// the recipes live on the server, which fills in the result
export class GuiPlayerInventory extends GuiMenuScreen {
#player: LocalPlayer;
constructor(player: LocalPlayer, send: (message: ClientMessage) => void) {
super(player.inventories, send, player_menu(player.inventories), WIDTH, HEIGHT);
this.#player = player;
}
render_background(g: ModGraphics): void {
g.panel(0, 0, WIDTH, HEIGHT);
this.#render_effects(g);
// the slots mods added outside the panel get one of their own, around them with the same margin
const outside = this.menu.slots.slice(PLAYER_MENU_EXTRA)
.filter((s) => s.x + SLOT < 0 || s.x > WIDTH || s.y + SLOT < 0 || s.y > HEIGHT);
if (outside.length > 0) {
const left = Math.min(...outside.map((s) => s.x)) - MARGIN;
const top = Math.min(...outside.map((s) => s.y)) - MARGIN;
const right = Math.max(...outside.map((s) => s.x)) + SLOT + MARGIN;
const bottom = Math.max(...outside.map((s) => s.y)) + SLOT + MARGIN;
g.panel(left, top, right - left, bottom - top);
}
}
// the ones with an icon, left to right in the order they were added. hovering one says what it is
#render_effects(g: ModGraphics) {
const shown = this.#player.effects.flatMap((effect) => {
const icon = EverythingRegistry.get<EffectJson>("effects", effect.id)?.icon;
return icon ? [{ ...effect, icon }] : [];
});
if (shown.length === 0) return;
const width = shown.length * (ICON + ICON_GAP) - ICON_GAP + ICON_MARGIN * 2;
const height = ICON + ICON_MARGIN * 2;
const top = -height - ICON_GAP;
g.panel(0, top, width, height);
const mouse = g.mouse;
shown.forEach((effect, i) => {
const x = ICON_MARGIN + i * (ICON + ICON_GAP);
const y = top + ICON_MARGIN;
g.texture(effect.icon, x, y, ICON, ICON);
if (mouse.x >= x && mouse.x < x + ICON && mouse.y >= y && mouse.y < y + ICON) {
const name = display_name(effect.id) + level(effect.amplifier);
g.tooltip([name, { text: time_left(effect.duration), color: TIME_COLOR }], mouse.x, mouse.y);
}
});
}
render_labels(g: ModGraphics): void {
g.text(this.menu.title, 26, 6);
g.text("Inventory", 8, 73);
}
}
// like minecraft: nothing for level 1, then II, III...
function level(amplifier: number) {
const numerals = ["", " II", " III", " IV", " V", " VI", " VII", " VIII", " IX", " X"];
return numerals[amplifier] ?? ` ${amplifier + 1}`;
}
// minutes and seconds, like 1:05
function time_left(ticks: number) {
if (ticks < 0) return "Forever";
const seconds = Math.ceil(ticks / TICKS_PER_SECOND);
return `${Math.floor(seconds / 60)}:${String(seconds % 60).padStart(2, "0")}`;
}