240 lines
9.9 KiB
TypeScript
240 lines
9.9 KiB
TypeScript
// SPDX-FileCopyrightText: 2026 Paula Wisnievski
|
|
// SPDX-License-Identifier: AGPL-3.0-or-later
|
|
|
|
import type { Client } from "$/client/client.ts";
|
|
import type { LocalPlayer } from "$/client/entity/local_player.ts";
|
|
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";
|
|
|
|
// sizes and gaps in minecraft's gui pixels, drawn GUI_SCALE times bigger
|
|
const HOTBAR_WIDTH = 182;
|
|
const HOTBAR_HEIGHT = 22;
|
|
// each slot is 20 across, the item sits 3 in from its corner and the selection 1 out
|
|
const HOTBAR_CELL = 20;
|
|
const HOTBAR_ITEM_INSET = 3;
|
|
// hearts and hunger: 10 icons each, 8 apart, above the hotbar's top. more max health stacks more rows of hearts above,
|
|
// 10 apart, closer together when there are many, like minecraft
|
|
const ICON_SIZE = 9;
|
|
const ICON_SPACING = 8;
|
|
const ICONS_ABOVE_HOTBAR = 17;
|
|
const ICONS_PER_ROW = 10;
|
|
const HEART_ROW_SPACING = 10;
|
|
const MIN_HEART_ROW_SPACING = 3;
|
|
// air: 10 bubbles above the hunger, like minecraft, from when the head goes under water until it's full again
|
|
const MAX_AIR = 20;
|
|
const AIR_ROW_SPACING = 10;
|
|
// 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 = 12;
|
|
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: [number, number, number, number] = [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: [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 {
|
|
// what was held last frame, as slot and item id, and when the held item last changed
|
|
#held = "";
|
|
#held_since = -Infinity;
|
|
|
|
render(client: Client) {
|
|
this.#render_hotbar(client.player.inventories);
|
|
this.#render_vitals(client);
|
|
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);
|
|
}
|
|
|
|
#render_hotbar(inventories: ClientInventories) {
|
|
const { x, y } = hotbar_position();
|
|
draw_gui_sprite("hotbar", x, y);
|
|
|
|
const selected_x = x + (inventories.hotbar_selected * HOTBAR_CELL - 1) * GUI_SCALE;
|
|
draw_gui_sprite("hotbar_selection", selected_x, y - GUI_SCALE);
|
|
|
|
for (let index = 0; index < 9; index += 1) {
|
|
const item = inventories.inventory.get_item(index);
|
|
if (item) {
|
|
// draw_item takes a slot's corner, which is a pixel out from the item
|
|
const item_x = x + (index * HOTBAR_CELL + HOTBAR_ITEM_INSET - 1) * GUI_SCALE;
|
|
draw_item(item, item_x, y + (HOTBAR_ITEM_INSET - 1) * GUI_SCALE);
|
|
}
|
|
}
|
|
}
|
|
|
|
// like minecraft: switching slots, or a different item ending up in the selected one, shows its name for a bit
|
|
// top is the top of the hearts
|
|
#render_held_name(inventories: ClientInventories, top: number) {
|
|
const item = inventories.inventory.get_item(inventories.hotbar_selected);
|
|
const held = item ? `${inventories.hotbar_selected} ${item.type_id}` : "";
|
|
const now = performance.now();
|
|
if (held !== this.#held) {
|
|
this.#held = held;
|
|
this.#held_since = item ? now : -Infinity;
|
|
}
|
|
|
|
const shown_for = now - this.#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 name = display_name(item.type_id);
|
|
const x = (canvas.width - measure_text(name, TEXT_SCALE)) / 2;
|
|
const y = top - HELD_NAME_GAP - TEXT_HEIGHT * TEXT_SCALE;
|
|
// a shadow so it reads over bright sky and snow
|
|
draw_text(name, x + TEXT_SCALE, y + TEXT_SCALE, TEXT_SCALE, [0.15, 0.15, 0.15, alpha]);
|
|
draw_text(name, x, y, TEXT_SCALE, [1, 1, 1, alpha]);
|
|
}
|
|
|
|
// hearts on the left and hunger on the right like minecraft, each icon 2 points. saturation is appleskin's outline
|
|
// over the hunger icons, it's never more than the hunger. air's bubbles go above the hunger, see shows_air
|
|
#render_vitals(client: Client) {
|
|
const player = client.player;
|
|
const { x: left } = hotbar_position();
|
|
const right = left + HOTBAR_WIDTH * GUI_SCALE;
|
|
const y = icons_y();
|
|
const health = Math.ceil(player.health);
|
|
const food = Math.ceil(player.food);
|
|
const saturation = Math.min(player.saturation, player.food);
|
|
|
|
const hearts = Math.ceil(player.max_health / 2);
|
|
const row_spacing = heart_row_spacing(player.max_health) * GUI_SCALE;
|
|
for (let i = 0; i < hearts; i++) {
|
|
const heart_x = left + (i % ICONS_PER_ROW) * ICON_SPACING * GUI_SCALE;
|
|
const heart_y = y - Math.floor(i / ICONS_PER_ROW) * row_spacing;
|
|
draw_gui_sprite("heart_container", heart_x, heart_y);
|
|
const heart = icon_fill(health, i);
|
|
if (heart) {
|
|
draw_gui_sprite(heart === "full" ? "heart_full" : "heart_half", heart_x, heart_y);
|
|
}
|
|
}
|
|
|
|
for (let i = 0; i < ICONS_PER_ROW; i++) {
|
|
// hunger goes right to left
|
|
const food_x = right - (i * ICON_SPACING + ICON_SIZE) * GUI_SCALE;
|
|
draw_gui_sprite("food_empty", food_x, y);
|
|
const fill = icon_fill(food, i);
|
|
if (fill) {
|
|
draw_gui_sprite(fill === "full" ? "food_full" : "food_half", food_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;
|
|
draw_gui_sprite(`saturation_${quarters}`, food_x, y);
|
|
}
|
|
if (shows_air(player)) {
|
|
const air_y = y - AIR_ROW_SPACING * GUI_SCALE;
|
|
draw_gui_sprite("air_empty", food_x, air_y);
|
|
const bubble = icon_fill(player.air, i);
|
|
if (bubble) draw_gui_sprite(bubble === "full" ? "air_full" : "air_half", food_x, air_y);
|
|
}
|
|
}
|
|
}
|
|
|
|
#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, client.level.time)),
|
|
];
|
|
|
|
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);
|
|
const y = Math.round((canvas.height - size) / 2);
|
|
draw_gui_sprite("crosshair", x + GUI_SCALE, y + GUI_SCALE, undefined, undefined, CROSSHAIR_SHADOW);
|
|
draw_gui_sprite("crosshair", x, y);
|
|
|
|
// only while it's charging, like minecraft
|
|
const strength = client.player.attack_strength();
|
|
if (strength >= 1) return;
|
|
const width = ATTACK_BAR_WIDTH * GUI_SCALE;
|
|
const bar_x = Math.round((canvas.width - width) / 2);
|
|
const bar_y = y + size + ATTACK_BAR_GAP * GUI_SCALE;
|
|
draw_rect(bar_x, bar_y, width, ATTACK_BAR_HEIGHT * GUI_SCALE, ATTACK_BAR_BACK);
|
|
draw_rect(bar_x, bar_y, Math.round(width * strength), ATTACK_BAR_HEIGHT * GUI_SCALE, ATTACK_BAR_FILL);
|
|
}
|
|
}
|
|
|
|
function hotbar_position() {
|
|
return {
|
|
x: Math.round((canvas.width - HOTBAR_WIDTH * GUI_SCALE) / 2),
|
|
y: canvas.height - HOTBAR_HEIGHT * GUI_SCALE,
|
|
};
|
|
}
|
|
|
|
// the top of the hearts and hunger
|
|
function icons_y() {
|
|
return hotbar_position().y - ICONS_ABOVE_HOTBAR * GUI_SCALE;
|
|
}
|
|
|
|
// as soon as the head is under, so it shows full before it starts going down, and while it fills back up after
|
|
function shows_air(player: LocalPlayer) {
|
|
return player.eyes_in_water || player.air < MAX_AIR;
|
|
}
|
|
|
|
// the top of the bubbles while they show, the hearts' otherwise
|
|
function air_y(player: LocalPlayer) {
|
|
return icons_y() - (shows_air(player) ? AIR_ROW_SPACING * GUI_SCALE : 0);
|
|
}
|
|
|
|
// how far apart rows of hearts are, in gui pixels
|
|
function heart_row_spacing(max_health: number) {
|
|
const rows = Math.ceil(max_health / 2 / ICONS_PER_ROW);
|
|
return Math.max(HEART_ROW_SPACING - (rows - 2), MIN_HEART_ROW_SPACING);
|
|
}
|
|
|
|
// how much higher the rows of hearts past the first go, on the canvas
|
|
function heart_rows_height(max_health: number) {
|
|
const rows = Math.ceil(max_health / 2 / ICONS_PER_ROW);
|
|
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";
|
|
if (value === i * 2 + 1) return "half";
|
|
return undefined;
|
|
}
|