Skip to content

HUD ​

Every game owns a hud (created by BaseGame, attached to the container by start()). It is plain DOM — no framework, inline styles, removed with the game.

Slots ​

A HUD element goes in a slot, not at coordinates:

top   top-left   above-center   below-center   bottom   center   rail

above-center / below-center hug a centered playfield from outside; their distance from center is one CSS variable the game sets from the size of its own board:

ts
game.hud.root.style.setProperty('--hud-gap', '30vh');

rail is a screen-sized layer for a panel that positions itself (a leaderboard, a side panel); center also hosts dialogs, above it.

Elements ​

ts
hud.text('score', 'Score: 12', 'top');          // upserts by id
hud.bar('hp', { value: 70, max: 100, color: '#e33', slot: 'top' });
hud.hint('press any key');                       // blinking attract line ('' clears)
hud.banner('Game over\nnice run');               // center card ('' clears)
hud.button('retry', 'Retry', onRetry, 'below-center');
hud.dialog({ title: 'Rules', lines: ['…'], onClose });  // modal over a dimmed world
hud.mount('rail', myPanelEl);                    // adopt your own element (null removes)

Everything upserts by id: calling text('score', …) each frame is the intended API. Re-declaring a button rebinds its handler. The HUD root is pointer-transparent — the world stays clickable — and only buttons/dialogs opt back in.

Inventory ​

Inventory (on every Player) is an id → count map with a change event. createInventoryHud(inventory, parent) renders it as a row of chips that hides itself when empty:

ts
import { createInventoryHud } from '@maomi/sge/inventory';
const invHud = createInventoryHud(player.inventory, el);
// invHud.dispose() when done

Styling your own chrome ​

applyStyles(el, { … }) and the shared CARD style token (from @maomi/sge/style) are the engine's one idiom for imperative DOM styling — typed, composable, no CSS files:

ts
applyStyles(panel, { width: '260px', padding: '16px', ...CARD });

Test hooks ​

Engine-built controls carry stable data-testids — inventory, inv-item, crosshair, shooter-crosshair, stick-left, jump-btn — treat them as public contract for e2e suites.