Health and hunger system

This commit is contained in:
2026-09-26 21:07:36 -03:00
parent 0fa6ec4ed3
commit 8d9c85d379
18 changed files with 662 additions and 8 deletions
+4
View File
@@ -10,6 +10,10 @@ const SEND_POSITION_TICKS = 2;
export class LocalPlayer extends Player {
client: Client;
inventories = new ClientInventories();
// what the server says, see server/game/vitals.ts. out of 20
health = 20;
food = 20;
saturation = 5;
move_speed = 4;
// blocks per second, peaks about 1.25 blocks up with the entity's gravity and drag
+43 -3
View File
@@ -3,7 +3,7 @@ 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_stroke, draw_text, measure_text, Texture } from "$/client/renderer/mod.ts";
import { canvas, draw_rect, draw_rect_stroke, draw_text, measure_text, Texture } from "$/client/renderer/mod.ts";
import { display_name } from "$/common/utils.ts";
import { TEXT_HEIGHT, TEXT_SCALE } from "./widgets.ts";
@@ -13,6 +13,16 @@ const CROSSHAIR_SIZE = 8;
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];
// what's drawn over the world while playing, like minecraft's Gui: hotbar, crosshair and chat
export class Hud {
@@ -22,6 +32,7 @@ export class Hud {
render(client: Client) {
this.#render_hotbar(client.player.inventories);
this.#render_vitals(client);
this.#render_held_name(client.player.inventories);
this.#render_crosshair();
client.chat.render(false);
@@ -81,14 +92,43 @@ 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 hotbar_top = canvas.height - (PADDING * 2 + SLOT_SIZE);
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 = hotbar_top - HELD_NAME_GAP - TEXT_HEIGHT * TEXT_SCALE;
const y = bars_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]);
}
// 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
#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;
this.#bar(left, y, bar_width, player.health / MAX_VITAL, HEALTH_COLOR);
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);
}
}
#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,
+6 -1
View File
@@ -9,6 +9,7 @@ type Color = [number, number, number, number];
const NAME_COLOR: Color = [1, 1, 1, 1];
const LORE_COLOR: Color = [0.66, 0.66, 0.66, 1];
const FOOD_COLOR: Color = [0.85, 0.6, 0.35, 1];
const BACKGROUND: Color = [0.06, 0.02, 0.1, 0.94];
const BORDER: Color = [0.31, 0.1, 0.6, 1];
const BORDER_WIDTH = 2;
@@ -25,7 +26,11 @@ export interface TooltipLine {
// its name, the item's lore from its json, then what the server's scripts said about this stack
export function item_tooltip(item: ItemStack): TooltipLine[] {
const lines: TooltipLine[] = [{ text: display_name(item.type_id), color: NAME_COLOR }];
const static_lore = EverythingRegistry.get<ItemRegistry>("items", item.type_id)?.lore;
const info = EverythingRegistry.get<ItemRegistry>("items", item.type_id);
if (info?.food) {
lines.push({ text: `Restores ${info.food.nutrition} hunger`, color: FOOD_COLOR });
}
const static_lore = info?.lore;
for (const lore of [static_lore, item.lore]) {
for (const text of lore?.split("\n") ?? []) {
lines.push({ text, color: LORE_COLOR });
+5
View File
@@ -34,6 +34,11 @@ export class ClientPacketListener {
case "time":
level.time = message.time;
break;
case "vitals":
client.player.health = message.health;
client.player.food = message.food;
client.player.saturation = message.saturation;
break;
case "player_leave":
level.remove_entity(message.id);
break;