Skip to content

4. Score and HUD ​

Wire the events into screen chrome: a score, a hint, and an end state.

ts
let score = 0;

const hud = fps.hud;
hud.text('score', 'Beacons: 0 / 3', 'top');
hud.hint('find the glowing beacons');

Update it from the interaction:

ts
class Beacon extends Box implements Interactable {
  collected = false;
  interactRange = 6;
  onCursorSelect() {
    if (this.collected) return;
    this.collected = true;
    score += 1;
    hud.text('score', `Beacons: ${score} / 3`, 'top');
    hud.hint(score < 3 ? '' : '');   // hint served its purpose
    if (score === 3) {
      hud.dialog({
        title: 'All beacons found',
        lines: ['That is the whole loop: entities, physics, interaction, HUD.'],
        onClose: () => hud.dialog(null),
      });
    }
  }
}

Every call upserts by id — hud.text('score', …) just rewrites the line; there is no element bookkeeping on your side.

Inventory chips ​

If you used player.inventory in part 3, render it:

ts
import { createInventoryHud } from '@maomi/sge/inventory';

const el = fps.start(player);
createInventoryHud(player.inventory, el);
return el;

A row of chips appears bottom-left whenever the inventory is non-empty.

Where to go next ​

  • The examples are complete single-file games — the board scene shows the Game class this tutorial didn't use.
  • Mobile adds the rotate prompt and fullscreen button for phones.
  • The API reference covers everything the guide didn't.