123 lines
4.8 KiB
TypeScript
123 lines
4.8 KiB
TypeScript
// SPDX-FileCopyrightText: 2026 Paula Wisnievski
|
|
// SPDX-License-Identifier: AGPL-3.0-or-later
|
|
|
|
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";
|
|
import { draw_gui_player } from "../rendering/gui_player.ts";
|
|
import { InputManager } from "../input_manager.ts";
|
|
import { AssetManager } from "../assets.ts";
|
|
import type { Texture } from "../renderer/mod.ts";
|
|
import { GUI_SCALE } from "./gui_sprites.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];
|
|
// the box the player stands in, left of the crafting grid, like minecraft's
|
|
const PLAYER_BOX = { x: MARGIN, y: MARGIN, size: 64 };
|
|
const PLAYER_BOX_COLOR: [number, number, number, number] = [0, 0, 0, 0.6];
|
|
// gui pixels to a skin pixel, so the player's 32 fit in the box with room to look around
|
|
const PLAYER_PIXEL = 1.6;
|
|
// the feet, above the box's bottom
|
|
const PLAYER_FEET = 4;
|
|
|
|
// menu 0's screen: the player looking at the mouse beside 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_player(g);
|
|
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 player in their box, in their skin and what they wear, turned toward the mouse
|
|
#render_player(g: ModGraphics) {
|
|
const { x, y, size } = PLAYER_BOX;
|
|
g.rect(x, y, size, size, PLAYER_BOX_COLOR);
|
|
const skin = this.#player.skin ?? AssetManager.instance.get<Texture>("cubi:skin");
|
|
const mouse = InputManager.get_mouse_position();
|
|
draw_gui_player(
|
|
skin,
|
|
this.#player.slim,
|
|
this.x + (x + size / 2) * GUI_SCALE,
|
|
this.y + (y + size - PLAYER_FEET) * GUI_SCALE,
|
|
PLAYER_PIXEL * GUI_SCALE,
|
|
GUI_SCALE,
|
|
mouse.x,
|
|
mouse.y,
|
|
this.#player.worn,
|
|
);
|
|
}
|
|
|
|
// 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, 79, 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")}`;
|
|
}
|