Giant chesplate

This commit is contained in:
2026-10-01 16:58:30 -03:00
parent a7835261e1
commit 1fc92a7b00
21 changed files with 148 additions and 51 deletions
+2 -1
View File
@@ -56,8 +56,9 @@ const NO_INPUT: MoveInput = { x: 0, z: 0, jump: false, sprint: false, sneak: fal
export class LocalPlayer extends Player {
client: Client;
inventories = new ClientInventories();
// what the server says, see server/game/vitals.ts. out of 20
// what the server says, see server/game/vitals.ts. food out of 20, health out of max_health
health = 20;
max_health = 20;
food = 20;
saturation = 5;
// the attack speed attribute the server sent, and when they last attacked or switched slots, for the indicator
+33 -11
View File
@@ -12,11 +12,14 @@ 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
// 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 MAX_VITAL = 20;
const ICONS_PER_ROW = 10;
const HEART_ROW_SPACING = 10;
const MIN_HEART_ROW_SPACING = 3;
// 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;
@@ -40,9 +43,10 @@ export class Hud {
render(client: Client) {
this.#render_hotbar(client.player.inventories);
this.#render_vitals(client);
this.#render_held_name(client.player.inventories);
const top = icons_y() - heart_rows_height(client.player.max_health);
this.#render_held_name(client.player.inventories, top);
this.#render_crosshair(client);
client.chat.render(false, icons_y() - HELD_NAME_GAP);
client.chat.render(false, top - HELD_NAME_GAP);
}
#render_hotbar(inventories: ClientInventories) {
@@ -63,7 +67,8 @@ export class Hud {
}
// like minecraft: switching slots, or a different item ending up in the selected one, shows its name for a bit
#render_held_name(inventories: ClientInventories) {
// 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();
@@ -79,7 +84,7 @@ 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 x = (canvas.width - measure_text(name, TEXT_SCALE)) / 2;
const y = icons_y() - HELD_NAME_GAP - TEXT_HEIGHT * TEXT_SCALE;
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]);
@@ -96,14 +101,19 @@ export class Hud {
const food = Math.ceil(player.food);
const saturation = Math.min(player.saturation, player.food);
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 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, y);
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);
@@ -149,7 +159,19 @@ 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
// 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;
}
// 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";
+1
View File
@@ -76,6 +76,7 @@ export class ClientPacketListener {
break;
case "vitals":
client.player.health = message.health;
client.player.max_health = message.max_health;
client.player.food = message.food;
client.player.saturation = message.saturation;
break;