Build a HUD
Build a heads-up display of your own with a stat bar, unread counts and the tracked quest's objective, in plain JS, React or Vue.
Use this when the React UI's stat list, notice strip and quest log are too plain for your game. Set up the runner first, as Start your own UI shows. Each piece of a HUD reads the view, which is the same whatever renders it, so each tab below reads the same fields. The examples use the showcase's stats and plugins.
Add a stat bar
Find the stat in view.stats, a list of StatViews with each one's value, min, max and text. This bar takes the stat's id, so one piece renders a bar for any stat:
// src/hud.ts
import type { View } from '@ifloom/engine'
/** A bar for one of the game's stats, by its key: statBar(view, 'energy'). */
export function statBar(view: View, key: string): HTMLElement | null {
const stat = view.stats.find((entry) => entry.key === key)
if (!stat) return null
const range = stat.max - stat.min
const share = range > 0 ? (stat.value - stat.min) / range : 0
const meter = document.createElement('div')
meter.className = 'meter'
meter.setAttribute('role', 'meter')
meter.setAttribute('aria-label', stat.label)
meter.setAttribute('aria-valuemin', String(stat.min))
meter.setAttribute('aria-valuemax', String(stat.max))
meter.setAttribute('aria-valuenow', String(stat.value))
meter.setAttribute('aria-valuetext', stat.text)
const fill = meter.appendChild(document.createElement('div'))
fill.style.width = `${share * 100}%`
fill.style.background = share < 0.25 ? 'crimson' : 'seagreen'
return meter
}max is the stat's ceiling now, so a ceiling that grows during the game is already worked out. role="meter" and the values let a screen reader say how full it is.
If a plain bar is all you need in the React UI, give the stat the hint ui: { display: 'bar' } or 'pips', and the built-in StatList renders it. StatList is plain on purpose, so write your own, as above, for a bar that changes colour. To reword a levelled stat's "Level 2" or its bar's label, set ui.statLevel or ui.statProgress in your content's strings.
Add a strip of unread counts
Read the unseen notices from view.notices.unseen, newest first, and a plugin's counts from its part of view.plugins. The messages plugin keeps an unread count:
// src/hud.ts
import type { View } from '@ifloom/engine'
let showNotices = false
/** The unread counts. The bell shows the notices; `redraw` renders the page again with the list open or shut. */
export function unread(view: View, redraw: () => void): HTMLElement {
const strip = document.createElement('div')
strip.className = 'unread'
const notices = view.notices.unseen
const texts = view.plugins.messages?.unread ?? 0
if (notices.length) {
const bell = strip.appendChild(document.createElement('button'))
bell.textContent = `🔔 ${notices.length}`
bell.onclick = () => {
showNotices = !showNotices
redraw()
}
}
if (texts) strip.appendChild(document.createElement('span')).textContent = `✉ ${texts}`
if (showNotices) {
const list = strip.appendChild(document.createElement('ul'))
for (const notice of notices.slice(0, 10)) list.appendChild(document.createElement('li')).textContent = notice.title
}
return strip
}A count of 0 renders nothing. The bell shows the newest notices, and sending { type: 'dismiss' } with runner.apply marks them all seen (see dismiss). In the React tab, the texts button opens the phone with useDevice and a DeviceDialog, the pair the built-in device buttons use. In the other tabs it's a count, which you can turn into a button that opens your own messages panel.
Add an objective line
Read the active quests from view.plugins.quests.active, which the quests plugin lists with the tracked one first. Each has its name and the hint for the stage the player is on:
// src/hud.ts
import type { View } from '@ifloom/engine'
/** The tracked quest's name and hint, or null when none is tracked. */
export function objective(view: View): HTMLElement | null {
const quest = view.plugins.quests?.active.find((entry) => entry.tracked)
if (!quest) return null
const line = document.createElement('p')
line.className = 'objective'
line.appendChild(document.createElement('strong')).textContent = quest.name
if (quest.hint) line.append(` · ${quest.hint}`)
return line
}The player chooses which quest to track, which sends the quests plugin's quests/track command. To show the newest quest when none is tracked, take active[0] instead.
Put it on screen
Put the three pieces together in a bar over the scene:
// src/main.ts, in the vanilla template
import { objective, statBar, unread } from './hud.ts'
function draw(view: View): void {
root.replaceChildren()
const hud = add(root, 'div', '', 'hud')
hud.append(...[objective(view), unread(view, () => draw(runner.view())), statBar(view, 'energy')].filter((part) => part !== null))
// …then the title, the scene and its choices, as before.
}In plain JS, the pieces render again with the rest of the page on every change. In Vue and React, each component renders again when the view changes.
In the React UI, your components go anywhere in your layout among the built-in parts, inside the game's provider. Put <Hud /> at the top of the Story in the react template's src/layout.tsx, and delete its StatList from the side column:
// src/layout.tsx: the react template's, with <Hud /> at the top of the story
<Story>
<Hud />
<Scene />
<RoomActions />
</Story>For a bar fixed across the top of the window, see Rearrange or replace the React UI's parts.
Check it worked
Play in npm run dev and work shifts at the café, which cost 25 energy each: the bar shrinks and turns red below a quarter. Meet Ava and sleep, and the strip shows ✉ 1 for her text. Start a quest and track it, and its name and hint show in the objective line.
Variations
What just changed. Feedback after a choice, such as +30 coins, is already worked out in view.changes, for the stats your content names in changes.stats. The React UI's Changes renders it, and the default layout leaves it out, so place it after the scene. Render what changed has the same list in plain JS and Vue.
For more, see:
- The view and
StatView. - Render the stats and Render notices.
- Render the quest log.
- Arrange the layout, for the rest of the page.
Last updated on