Little info hud

This commit is contained in:
2026-10-08 14:53:51 -03:00
parent dd66d6caa7
commit 807b38b0af
+37
View File
@@ -4,6 +4,8 @@ import type { ClientInventories } from "$/client/inventory.ts";
import { draw_item } from "$/client/rendering/render_utils.ts";
import { canvas, draw_rect, draw_text, measure_text } from "$/client/renderer/mod.ts";
import { display_name } from "$/common/utils.ts";
import { clock } from "$/common/time.ts";
import { biome_season } from "$/common/worldgen/biomes.ts";
import { TEXT_HEIGHT, TEXT_SCALE } from "./widgets.ts";
import { draw_gui_sprite, GUI_SCALE } from "./gui_sprites.ts";
@@ -37,6 +39,11 @@ const ATTACK_BAR_HEIGHT = 2;
const ATTACK_BAR_GAP = 2;
const ATTACK_BAR_BACK: [number, number, number, number] = [0, 0, 0, 0.4];
const ATTACK_BAR_FILL: [number, number, number, number] = [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: [number, number, number, number] = [0, 0, 0, 0.4];
// what's drawn over the world while playing, like minecraft's Gui: hotbar, crosshair and chat
export class Hud {
@@ -50,6 +57,7 @@ export class Hud {
const top = Math.min(icons_y() - heart_rows_height(client.player.max_health), air_y(client.player));
this.#render_held_name(client.player.inventories, top);
this.#render_crosshair(client);
this.#render_info(client);
client.chat.render(false, top - HELD_NAME_GAP);
}
@@ -139,6 +147,31 @@ export class Hud {
}
}
#render_info(client: Client) {
const { day, hour, minute } = clock(client.level.time);
const { x, z } = client.player;
const biome = client.level.biome_at(Math.floor(x), Math.floor(z));
const lines = [
`Day ${day}`,
`${String(hour).padStart(2, "0")}:${String(minute).padStart(2, "0")}`,
biome === undefined ? "Unknown" : display_name(biome),
biome === undefined ? "Unknown" : capitalize(biome_season(biome)),
];
const padding = INFO_PADDING * GUI_SCALE;
const line_height = (TEXT_HEIGHT + INFO_LINE_GAP) * TEXT_SCALE;
const width = Math.max(...lines.map((line) => measure_text(line, TEXT_SCALE))) + padding * 2;
const height = lines.length * line_height - INFO_LINE_GAP * TEXT_SCALE + padding * 2;
const left = INFO_MARGIN * GUI_SCALE;
const top = INFO_MARGIN * GUI_SCALE;
draw_rect(left, top, width, height, INFO_BACK);
lines.forEach((line, i) => {
const text_y = top + padding + i * line_height;
draw_text(line, left + padding + TEXT_SCALE, text_y + TEXT_SCALE, TEXT_SCALE, [0.15, 0.15, 0.15, 1]);
draw_text(line, left + padding, text_y, TEXT_SCALE, [1, 1, 1, 1]);
});
}
#render_crosshair(client: Client) {
const size = CROSSHAIR_SIZE * GUI_SCALE;
const x = Math.round((canvas.width - size) / 2);
@@ -191,6 +224,10 @@ function heart_rows_height(max_health: number) {
return Math.max(0, rows - 1) * heart_row_spacing(max_health) * GUI_SCALE;
}
function capitalize(word: string) {
return word[0].toUpperCase() + word.slice(1);
}
// 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";