Hud api
This commit is contained in:
@@ -36,7 +36,7 @@ clicking them does something else.
|
||||
| Script | Runs on | Sent to players | For |
|
||||
| ---------- | -------------------------------- | ---------------------- | ------------------------------------------------------------------- |
|
||||
| `server` | the server, in the game worker | **never** | game logic: block and item components, events, commands, containers |
|
||||
| `client` | every player's browser | yes | presentation: inventory overlays, and later custom screens and HUD |
|
||||
| `client` | every player's browser | yes | presentation: screens, inventory overlays and the HUD |
|
||||
| `shared` | the server and every browser | yes | block rules: the states blocks are placed with, joining neighbors |
|
||||
| `worldgen` | the server, when it makes chunks | no | features that decorate terrain, deterministic |
|
||||
| `content` | the server, while it loads mods | no, only what it makes | blocks made in code, like one for every kind of rock |
|
||||
|
||||
@@ -0,0 +1,76 @@
|
||||
# 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
|
||||
|
||||
A client script registers layers during `setup`, with ids in its namespace:
|
||||
|
||||
```ts
|
||||
import type { ClientContext } from "cubi/client";
|
||||
|
||||
export function setup(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
|
||||
|
||||
A server script's message with `{ overlay: true }` (see `Player.send_message`) isn't chat: it goes to the client scripts
|
||||
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.
|
||||
@@ -7,7 +7,8 @@ There are a few ways for a mod to show players a GUI. Pick the first one that's
|
||||
| [Menus](../inventories/menus.md) | server, and client | Inventories and machines: slots, filters, progress bars |
|
||||
| [Plain screens](#plain-screens) | client only | Screens with nothing the server checks: maps, guide books |
|
||||
| [Inventory overlays](#inventory-overlays) | client only | Drawing next to every menu's screen, like a recipe list |
|
||||
| Forms and HUD elements | not built yet | See [below](#not-built-yet) |
|
||||
| [HUD layers](hud.md) | client only | Drawing over the world while playing, like health bars |
|
||||
| Forms | not built yet | See [below](#not-built-yet) |
|
||||
|
||||
Everything is in **GUI pixels**, like NeoForge's: 18 of them are a slot, and the game draws each one 3 screen pixels
|
||||
wide.
|
||||
@@ -111,7 +112,13 @@ interface Graphics {
|
||||
|
||||
rect(x: number, y: number, w: number, h: number, color?: [r, g, b, a]): void;
|
||||
panel(x: number, y: number, w: number, h: number): void; // the game's panel background
|
||||
text(text: string, x: number, y: number, options?: { scale?: number; color?: [r, g, b, a] }): void; // scale 1 is normal
|
||||
// scale 1 is normal. shadow draws it again under, in dark grey, so it reads over the world
|
||||
text(
|
||||
text: string,
|
||||
x: number,
|
||||
y: number,
|
||||
options?: { scale?: number; color?: [r, g, b, a]; shadow?: boolean },
|
||||
): void;
|
||||
measure_text(text: string, scale?: number): number;
|
||||
// any texture. part is [u, v, width, height] from 0 to 1, the part of it to draw, like a progress bar's filled part
|
||||
texture(id: string, x: number, y: number, w: number, h: number, part?: [u, v, w, h]): void;
|
||||
@@ -126,7 +133,10 @@ interface Graphics {
|
||||
filled: number,
|
||||
direction?: "right" | "up",
|
||||
): void;
|
||||
item(id: string, x: number, y: number, count?: number): void; // an item as a slot shows it
|
||||
// one of the engine's gui sprites, like "cubi:heart_full", at its size unless given one. color multiplies it
|
||||
sprite(id: string, x: number, y: number, options?: { width?: number; height?: number; color?: [r, g, b, a] }): void;
|
||||
// an item as a slot shows it. a stack draws with its count and data, like a worn tool's durability bar
|
||||
item(item: string | ItemStack, x: number, y: number, count?: number): void;
|
||||
// a fluid as a slot shows it, like { id: "bworld:juice", data: { item: "bworld:apple" } }. filled is how much of
|
||||
// the slot it fills from the bottom up, 0 to 1, like a tank's amount / capacity
|
||||
fluid(fluid: FluidVariant, x: number, y: number, filled?: number): void;
|
||||
@@ -203,7 +213,8 @@ ctx.player.biome; // and ctx.player.season, see ../worldgen/biomes.md#seasons
|
||||
ctx.world.get_block(x, y, z); // a block id, what this client sees
|
||||
ctx.world.get_client_data<number>(x, y, z, "speed"); // see ../blocks/tiledata.md#client-data, a copy
|
||||
ctx.world.biome_at(x, y, z); // and ctx.world.season_at(x, y, z), undefined where nothing's loaded
|
||||
ctx.world.time; // see Time
|
||||
ctx.world.time; // see Time, and ctx.world.day and ctx.world.hour like the server's
|
||||
ctx.player.health; // and the rest of the vitals, the hotbar and the attack cooldown, see hud.md
|
||||
ctx.world.min_y; // and ctx.world.max_y, see ../worldgen/terrain.md#height
|
||||
ctx.items.ids(); // every item, in load order
|
||||
ctx.items.exists(id);
|
||||
@@ -235,7 +246,6 @@ The client and server APIs have types for these, but calling them throws for now
|
||||
|
||||
- **Forms** (`ctx.ui.message_form`, `action_form` and `modal_form` on the server): simple dialogs defined entirely by
|
||||
the server script, like Bedrock's server forms. They'll be menus whose screens the engine draws.
|
||||
- **HUD elements** (`ctx.hud.register`): drawing over the world every frame.
|
||||
- **Keybinds** (`ctx.input.bind`). Until then, an inventory overlay can check `g.key_pressed` while an inventory is
|
||||
open.
|
||||
|
||||
|
||||
Reference in New Issue
Block a user