Breathing !

This commit is contained in:
2026-10-02 11:28:30 -03:00
parent 9ca717d2d0
commit 88d6de13d5
10 changed files with 50 additions and 12 deletions
+2 -1
View File
@@ -79,11 +79,12 @@ 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. food out of 20, health out of max_health
// what the server says, see server/game/vitals.ts. food and air out of 20, health out of max_health
health = 20;
max_health = 20;
food = 20;
saturation = 5;
air = 20;
// the attack speed attribute the server sent, and when they last attacked or switched slots, for the indicator
attack_speed = 4;
last_attack = -Infinity;
+4
View File
@@ -24,6 +24,10 @@ export const GUI_ATLAS = {
saturation_2: { x: 70, y: 213, width: 9, height: 9, border: [0, 0, 0, 0] },
saturation_3: { x: 80, y: 213, width: 9, height: 9, border: [0, 0, 0, 0] },
saturation_4: { x: 90, y: 213, width: 9, height: 9, border: [0, 0, 0, 0] },
// drawn by hand, not from a resource pack
air_empty: { x: 100, y: 213, width: 9, height: 9, border: [0, 0, 0, 0] },
air_full: { x: 110, y: 213, width: 9, height: 9, border: [0, 0, 0, 0] },
air_half: { x: 120, y: 213, width: 9, height: 9, border: [0, 0, 0, 0] },
} as const;
export type GuiSprite = keyof typeof GUI_ATLAS;
+22 -2
View File
@@ -1,4 +1,5 @@
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";
@@ -20,6 +21,9 @@ 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;
@@ -43,7 +47,7 @@ export class Hud {
render(client: Client) {
this.#render_hotbar(client.player.inventories);
this.#render_vitals(client);
const top = icons_y() - heart_rows_height(client.player.max_health);
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);
client.chat.render(false, top - HELD_NAME_GAP);
@@ -91,7 +95,7 @@ export class Hud {
}
// 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
// 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();
@@ -126,6 +130,12 @@ export class Hud {
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);
}
}
}
@@ -159,6 +169,16 @@ 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);
+1
View File
@@ -79,6 +79,7 @@ export class ClientPacketListener {
client.player.max_health = message.max_health;
client.player.food = message.food;
client.player.saturation = message.saturation;
client.player.air = message.air;
break;
case "attack_speed":
client.player.attack_speed = message.value;