77 lines
3.1 KiB
Markdown
77 lines
3.1 KiB
Markdown
# HUD
|
|
|
|
The HUD is what's drawn over the world while playing, under any open screen. Mods add to it with layers, like NeoForge's
|
|
GUI layers. The base game's whole HUD is layers in `mods/bworld/scripts/hud.ts`: the hotbar, hearts, hunger, air, the
|
|
held item's name, overlay messages, the crosshair, and the day, time, biome and season. The engine only draws chat,
|
|
above them.
|
|
|
|
## Layers
|
|
|
|
The client phase registers layers while it runs, with ids in its namespace:
|
|
|
|
```ts
|
|
import type { ClientContext } from "cubi/client";
|
|
|
|
export function client(ctx: ClientContext) {
|
|
ctx.hud.register("thirst:water", {
|
|
on_render(g, hud) {
|
|
// a row of drops above the hunger, on the right
|
|
const y = g.height - hud.right_height - 10;
|
|
const right = Math.floor((g.width - 182) / 2) + 182;
|
|
for (let i = 0; i < 10; i++) g.texture("thirst:drop", right - i * 8 - 9, y, 9, 9);
|
|
hud.right_height += 10;
|
|
},
|
|
});
|
|
}
|
|
```
|
|
|
|
`on_render` runs every frame with a [`Graphics`](index.md#graphics) for the whole window, in GUI pixels. Layers draw in
|
|
the order they're registered, mods in load order, so a mod's layers go over the base game's, like NeoForge's
|
|
`registerAboveAll`. A layer that throws is removed, with the error in the console.
|
|
|
|
## Stacking Along the Bottom
|
|
|
|
`hud` says how far up from the bottom of the window the HUD reaches on each side, in GUI pixels, like NeoForge's
|
|
`leftHeight` and `rightHeight`:
|
|
|
|
```ts
|
|
interface HudSpace {
|
|
left_height: number;
|
|
right_height: number;
|
|
}
|
|
```
|
|
|
|
Both start at 0 each frame. A layer that draws along the bottom draws above them and raises them, so the next one goes
|
|
on top. The base game's hotbar raises both by 29: its 22, and the room above it Minecraft's experience bar takes. Hearts
|
|
then go up the left 10 a row, hunger and air up the right. The held item's name and overlay messages go above the higher
|
|
side without raising it, and chat goes above that.
|
|
|
|
## Overlay Messages
|
|
|
|
The server phase's message with `{ overlay: true }` (see `Player.send_message`) isn't chat: it goes to the client phases
|
|
that listen for it, and the base game shows it above the hotbar for a few seconds:
|
|
|
|
```ts
|
|
let message = "";
|
|
ctx.hud.on_overlay_message((text) => (message = text));
|
|
```
|
|
|
|
## What Layers Can Read
|
|
|
|
Besides [the rest of the client's state](index.md#reading-game-state), the player's vitals and hotbar are there for the
|
|
HUD:
|
|
|
|
```ts
|
|
ctx.player.health; // and max_health, as the server last sent them
|
|
ctx.player.food; // and saturation, out of 20
|
|
ctx.player.air; // out of 20 like food, 2 a bubble. it goes down while their eyes are under water
|
|
ctx.player.is_underwater(); // their eyes are in water, like Minecraft's isUnderWater
|
|
ctx.player.attack_strength(); // 0 right after attacking, 1 once the cooldown is done
|
|
ctx.player.inventory.get(slot); // a copy of what's in a slot, hotbar is 0-8
|
|
ctx.player.selected_slot; // the hotbar slot they hold
|
|
ctx.world.day; // days since the world started, 0 on the first, and hour, 14.5 for 14:30
|
|
```
|
|
|
|
`g.sprite("cubi:heart_full", x, y)` draws one of the engine's GUI sprites at its size, like NeoForge's `blitSprite`, and
|
|
`g.item(stack, x, y)` draws a stack the way a slot does, with its count and durability bar.
|