This commit is contained in:
2026-10-22 19:55:59 -03:00
parent 1b30d71a47
commit 4cd29a17b3
37 changed files with 427 additions and 155 deletions
+30 -6
View File
@@ -33,6 +33,10 @@ const AIR_ROW_SPACING = 10;
const HELD_NAME_MS = 2000;
const HELD_NAME_FADE_MS = 500;
const HELD_NAME_GAP = 12;
// a server's overlay message, see MessageOptions.overlay: 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: [number, number, number, number] = [0, 0, 0, 0.4];
@@ -53,12 +57,21 @@ export class Hud {
// what was held last frame, as slot and item id, and when the held item last changed
#held = "";
#held_since = -Infinity;
#overlay = "";
#overlay_since = -Infinity;
// shows text above the hotbar for a moment, see MessageOptions.overlay
show_overlay(text: string) {
this.#overlay = text;
this.#overlay_since = performance.now();
}
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_overlay(top);
this.#render_crosshair(client);
this.#render_info(client);
client.chat.render(false, top - HELD_NAME_GAP);
@@ -97,12 +110,16 @@ export class Hud {
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]);
draw_centered(display_name(item.type_id), top - HELD_NAME_GAP - TEXT_HEIGHT * TEXT_SCALE, alpha);
}
// top is the top of the hearts, like for the held item's name, which it goes above
#render_overlay(top: number) {
const shown_for = performance.now() - this.#overlay_since;
if (shown_for > OVERLAY_MS) return;
const alpha = Math.min(1, (OVERLAY_MS - shown_for) / OVERLAY_FADE_MS);
const y = top - HELD_NAME_GAP - OVERLAY_GAP * GUI_SCALE - TEXT_HEIGHT * TEXT_SCALE * 2;
draw_centered(this.#overlay, y, alpha);
}
// hearts on the left and hunger on the right like minecraft, each icon 2 points. saturation is appleskin's outline
@@ -193,6 +210,13 @@ export class Hud {
}
}
// text across the middle of the screen at y, with a shadow so it reads over bright sky and snow
function draw_centered(text: string, y: number, alpha: number) {
const x = (canvas.width - measure_text(text, TEXT_SCALE)) / 2;
draw_text(text, x + TEXT_SCALE, y + TEXT_SCALE, TEXT_SCALE, [0.15, 0.15, 0.15, alpha]);
draw_text(text, x, y, TEXT_SCALE, [1, 1, 1, alpha]);
}
function hotbar_position() {
return {
x: Math.round((canvas.width - HOTBAR_WIDTH * GUI_SCALE) / 2),