Render stats, things and what changed
Render the stats, the things the player holds, what the last command changed, notices and dice rolls in a UI of your own.
Render the numbers and lists around the story: the player's stats, the things the player holds, what the last command changed, notices and dice. Set up the runner first, as Start your own UI shows.
Render the stats
view.stats lists the player's stats that are listed and visible now, in the order the game declares them, each a StatView. The React UI's StatList renders them in the side column of the default layout, as bars or pips when a stat's display hint asks.
import type { RunnerSnapshot } from '@ifloom/engine'
// add, as in the vanilla template's src/main.ts.
import { add } from './runner.ts'
/** The listed stats: each label and value, a bar from min to max, and how far to the next level. */
export function drawStats(parent: HTMLElement, { view }: RunnerSnapshot): void {
const list = add(parent, 'dl', '', 'stats')
for (const stat of view.stats) {
add(list, 'dt', stat.level ? `${stat.label} (level ${stat.level.level})` : stat.label)
const value = add(list, 'dd', stat.text)
const bar = add(value, 'meter')
Object.assign(bar, { min: stat.min, max: stat.max, value: stat.value })
// Towards the next level, from 0 to 1; there's no next one at the top.
if (stat.level?.next != null) Object.assign(add(value, 'progress'), { max: 1, value: stat.level.progress })
}
}
Each stat has its label, its value written through its format as text, such as "12 coins", and its min and max for a bar. A stat with levels has level, with its level, the value the next one starts at, and progress towards it from 0 to 1. view.statsById has every stat by id, listed or not, for one you render your own way, such as a purse in the corner.
Render what the player holds
The engine keeps no list of what the player holds. A plugin that keeps things puts its own in the view, such as the inventory's view.plugins.inventory.things, which Render things to use and drop renders, and a game that keeps things in kv keys reads them as any key. view.entities lists each listed kind with its entities, as EntitiesViews, and the React UI's EntityTable renders a listed kind as a table wherever you place it.
import type { RunnerSnapshot } from '@ifloom/engine'
// add, as in the vanilla template's src/main.ts.
import { add } from './runner.ts'
/** Each listed kind, with its entities and the stats the player can see on each. */
export function drawEntities(parent: HTMLElement, { view }: RunnerSnapshot): void {
for (const kind of view.entities) {
if (!kind.entries.length) continue
add(parent, 'h3', kind.plural)
const list = add(parent, 'ul', '', 'entities')
for (const entity of kind.entries) {
const row = add(list, 'li', entity.name)
row.dataset.entity = entity.ref
// A kind's flags, such as { met: true }, to split or style the list by.
for (const [flag, holds] of Object.entries(entity.flags)) if (holds) row.classList.add(flag)
for (const stat of entity.stats) add(row, 'span', `${stat.label} ${stat.text}`, 'stat')
}
}
}
Each entity in view.entities carries the stats the player can see on it and its kind's flags, such as { met: true }. Split or style the list by its flags, such as the people you've met and the rest. The engine has already left out every entity that isn't listed, so your UI hides nothing more.
Render what changed
view.changes lists the stats the last command moved, each a ChangeView with its signed text, the name of what moved, and by, which is positive when it went up. The React UI's Changes renders them as pills, and the default layout leaves it out, so the React tab places it after the scene.
import type { RunnerSnapshot } from '@ifloom/engine'
// add, as in the vanilla template's src/main.ts.
import { add } from './runner.ts'
/** What the last command moved, as pills: `+30 coins`, `-1 Energy`, `+4 Ava`, marked up or down for styling. */
export function drawChanges(parent: HTMLElement, { view }: RunnerSnapshot): void {
if (!view.changes.length) return
const list = add(parent, 'ul', '', 'changes')
for (const change of view.changes) {
const pill = add(list, 'li', change.name ? `${change.text} ${change.name}` : change.text, 'change')
pill.dataset.stat = change.stat
if (change.of) pill.dataset.of = change.of
pill.dataset.dir = change.by > 0 ? 'up' : 'down'
}
}
The list stays empty until the game names the stats to track in its content's changes.stats (ChangesDef). It compares the stats before and after the command, so a stat that went up and back down in one command isn't listed. A change from a plugin's modifier, such as a status starting, is listed too.
Render notices
view.notices.unseen lists every notice the player hasn't dismissed, newest first, each a NoticeView with its title, text, image and tag. The React UI's Notices renders them as toasts in a corner, in the default layout's Overlay, and NoticeStrip renders them as a strip over the story instead.
import type { RunnerSnapshot } from '@ifloom/engine'
// runner and add, as in the vanilla template's src/main.ts.
import { add, runner } from './runner.ts'
/** The notices not yet dismissed, newest first, each with a button that marks it seen. */
export function drawNotices(parent: HTMLElement, { view }: RunnerSnapshot): void {
const toasts = add(parent, 'div', '', 'notices')
toasts.setAttribute('role', 'status')
for (const notice of view.notices.unseen) {
const toast = add(toasts, 'div', '', 'notice')
if (notice.tag) toast.dataset.tag = notice.tag
add(toast, 'strong', notice.title)
if (notice.text) add(toast, 'p', notice.text)
add(toast, 'button', 'Dismiss').onclick = () => runner.apply({ type: 'dismiss', id: notice.id })
}
}
/** A badge with how many are unseen under a tag, such as the post. */
export function drawPostBadge(parent: HTMLElement, { view }: RunnerSnapshot): void {
const count = view.notices.byTag.post ?? 0
if (count) add(parent, 'span', String(count), 'badge')
}
Send { type: 'dismiss', id } when the player has read a notice, or { type: 'dismiss' } to dismiss them all. view.notices.byTag counts the unseen notices by tag, for a badge on the post or the phone.
Render dice rolls
view.rolls lists the rolls the last command made, in order, each a Roll with its dice, each die's face, what was added, the total and, with a target, whether it passed. The React UI's Scene renders a DiceRoll above a block whose dice hint names the roll, and the other rolls over the scene for a moment.
import type { RunnerSnapshot } from '@ifloom/engine'
// add, as in the vanilla template's src/main.ts.
import { add } from './runner.ts'
/** The rolls the last command made, as they landed: `2d6: 4 + 5 + 1 = 10 (passed)`. The next command clears them. */
export function drawRolls(parent: HTMLElement, { view }: RunnerSnapshot): void {
for (const roll of view.rolls) {
const plus = roll.plus ? ` + ${roll.plus}` : ''
const outcome = roll.passed === undefined ? '' : roll.passed ? ' (passed)' : ' (failed)'
const line = add(parent, 'p', `${roll.dice}: ${roll.faces.join(' + ')}${plus} = ${roll.total}${outcome}`, 'roll')
line.dataset.roll = roll.key
if (roll.critical) line.dataset.critical = roll.critical
}
}
The next command clears view.rolls, so each roll shows once. To put the dice beside the line that tells the outcome, find the beat whose block names the roll in its hint, beat.ui?.dice. Put the rolls in an element with aria-live="polite", so a screen reader reads the result.
For more on what these sections render, see:
Last updated on