Change the gui

This commit is contained in:
2026-09-27 14:52:30 -03:00
parent d0cbec1846
commit 29e61a235b
20 changed files with 451 additions and 359 deletions
+77 -76
View File
@@ -1,28 +1,29 @@
import { SLOT_SIZE } from "$/common/constants.ts";
import { AssetManager } from "$/client/assets.ts";
import type { Client } from "$/client/client.ts";
import type { ClientInventories } from "$/client/inventory.ts";
import { draw_item, draw_nine_slice } from "$/client/rendering/render_utils.ts";
import { canvas, draw_rect, draw_rect_stroke, draw_text, measure_text, Texture } from "$/client/renderer/mod.ts";
import { draw_item } from "$/client/rendering/render_utils.ts";
import { canvas, draw_text, measure_text } from "$/client/renderer/mod.ts";
import { display_name } from "$/common/utils.ts";
import { TEXT_HEIGHT, TEXT_SCALE } from "./widgets.ts";
import { draw_gui_sprite, GUI_SCALE } from "./gui_sprites.ts";
const PADDING = 10;
const CROSSHAIR_SIZE = 8;
// 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
const ICON_SIZE = 9;
const ICON_SPACING = 8;
const ICONS_ABOVE_HOTBAR = 17;
const MAX_VITAL = 20;
// 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;
// the health and hunger bars, side by side just above the hotbar
const BAR_HEIGHT = 14;
const BAR_GAP = 8;
const BAR_BORDER = 2;
const MAX_VITAL = 20;
const HEALTH_COLOR: [number, number, number, number] = [0.84, 0.12, 0.12, 1];
const FOOD_COLOR: [number, number, number, number] = [0.55, 0.33, 0.14, 1];
const SATURATION_COLOR: [number, number, number, number] = [1, 0.84, 0.25, 1];
const BAR_BACKGROUND: [number, number, number, number] = [0.1, 0.08, 0.08, 0.75];
const BAR_OUTLINE: [number, number, number, number] = [0, 0, 0, 0.8];
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];
// what's drawn over the world while playing, like minecraft's Gui: hotbar, crosshair and chat
export class Hud {
@@ -35,43 +36,22 @@ export class Hud {
this.#render_vitals(client);
this.#render_held_name(client.player.inventories);
this.#render_crosshair();
client.chat.render(false);
client.chat.render(false, icons_y() - HELD_NAME_GAP);
}
#render_hotbar(inventories: ClientInventories) {
const ui = AssetManager.instance.get<Texture>("bworld:ui");
const { x, y } = hotbar_position();
draw_gui_sprite("hotbar", x, y);
const hotbar_width = PADDING * 2 + SLOT_SIZE * 9;
const hotbar_height = PADDING * 2 + SLOT_SIZE;
const x = canvas.width / 2 - hotbar_width / 2;
const y = canvas.height - hotbar_height;
draw_nine_slice(ui, 160, 0, 16, 16, 4, 4, 4, 4, x, y, hotbar_width, hotbar_height);
for (let index = 0; index < 9; index += 1) {
const selected = inventories.hotbar_selected === index;
draw_nine_slice(
ui,
selected ? 19 * 16 : 160 + 32,
selected ? 16 : 0,
16,
16,
4,
4,
4,
4,
x + PADDING + index * SLOT_SIZE,
y + PADDING,
SLOT_SIZE,
SLOT_SIZE,
);
}
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(item, x + PADDING + index * SLOT_SIZE, y + PADDING);
// 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);
}
}
}
@@ -92,50 +72,71 @@ export class Hud {
}
const alpha = Math.min(1, (HELD_NAME_MS - shown_for) / HELD_NAME_FADE_MS);
const name = display_name(item.type_id);
const bars_top = canvas.height - (PADDING * 2 + SLOT_SIZE) - BAR_GAP - BAR_HEIGHT;
const x = (canvas.width - measure_text(name, TEXT_SCALE)) / 2;
const y = bars_top - HELD_NAME_GAP - TEXT_HEIGHT * TEXT_SCALE;
const y = icons_y() - 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]);
}
// health on the left, hunger on the right, like minecraft's hearts and drumsticks. saturation is a gold border
// around as much of the hunger bar as there's saturation, it's never more than the hunger
// 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
#render_vitals(client: Client) {
const player = client.player;
const hotbar_width = PADDING * 2 + SLOT_SIZE * 9;
const bar_width = (hotbar_width - BAR_GAP) / 2;
const left = canvas.width / 2 - hotbar_width / 2;
const y = canvas.height - (PADDING * 2 + SLOT_SIZE) - BAR_GAP - BAR_HEIGHT;
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);
this.#bar(left, y, bar_width, player.health / MAX_VITAL, HEALTH_COLOR);
for (let i = 0; i < MAX_VITAL / 2; i++) {
const heart_x = left + i * ICON_SPACING * GUI_SCALE;
draw_gui_sprite("heart_container", heart_x, y);
const heart = icon_fill(health, i);
if (heart) {
draw_gui_sprite(heart === "full" ? "heart_full" : "heart_half", heart_x, y);
}
const food_x = left + bar_width + BAR_GAP;
this.#bar(food_x, y, bar_width, player.food / MAX_VITAL, FOOD_COLOR);
const saturation = Math.min(player.saturation, player.food) / MAX_VITAL;
if (saturation > 0) {
const width = Math.max(BAR_BORDER * 2, bar_width * saturation);
draw_rect(food_x, y, width, BAR_BORDER, SATURATION_COLOR);
draw_rect(food_x, y + BAR_HEIGHT - BAR_BORDER, width, BAR_BORDER, SATURATION_COLOR);
draw_rect(food_x, y, BAR_BORDER, BAR_HEIGHT, SATURATION_COLOR);
draw_rect(food_x + width - BAR_BORDER, y, BAR_BORDER, BAR_HEIGHT, SATURATION_COLOR);
// 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);
}
}
}
#bar(x: number, y: number, width: number, fraction: number, color: [number, number, number, number]) {
draw_rect(x - 1, y - 1, width + 2, BAR_HEIGHT + 2, BAR_OUTLINE);
draw_rect(x, y, width, BAR_HEIGHT, BAR_BACKGROUND);
draw_rect(x, y, width * Math.max(0, Math.min(1, fraction)), BAR_HEIGHT, color);
}
#render_crosshair() {
draw_rect_stroke(
(canvas.width - CROSSHAIR_SIZE) / 2,
(canvas.height - CROSSHAIR_SIZE) / 2,
CROSSHAIR_SIZE,
CROSSHAIR_SIZE,
[0, 0, 0, 0.6],
);
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);
}
}
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;
}
// whether icon i of a 0 to 20 value 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;
}