Render timers, minigames and panels
Render timer countdowns, a running minigame and an open panel in a UI of your own, in plain JS, React or Vue.
Render the countdowns of running timers, a minigame while the player plays it, and a panel open over the story. Set up the runner first, with startClock beside it, as Start your own UI shows. The engine keeps no clock, so timers and minigames stand still until something sends ticks.
Render timers
view.timers lists each running timer in the order they started, as a TimerView with its id, its label, and the milliseconds left of its total. The React UI's Scene renders a Countdown under a block whose timer hint names a timer, Timers renders them all, and useTimer gives you one to render your own way.
import type { RunnerSnapshot } from '@ifloom/engine'
// add, as in the vanilla template's src/main.ts, which runs startClock(runner) to send the ticks.
import { add } from './runner.ts'
/** A countdown for each running timer: its label, a bar that empties, and the seconds left. */
export function drawTimers(parent: HTMLElement, { view }: RunnerSnapshot): void {
for (const timer of view.timers) {
const box = add(parent, 'div', '', 'timer')
box.dataset.timer = timer.id
// A quarter or less left: render it as urgent.
if (timer.left <= timer.total / 4) box.dataset.urgent = ''
if (timer.label) add(box, 'span', timer.label, 'timer-label')
Object.assign(add(box, 'progress'), { max: timer.total, value: timer.left })
add(box, 'span', `${Math.ceil(timer.left / 1000)}s`, 'timer-left')
}
}
Render the countdown from left / total, and the seconds from left. A timer is in the view only while it runs, so its countdown goes when it runs out or the game stops it. To put a countdown under the block that names it, read the block's hint from each beat, beat.ui?.timer. A <progress> moves in steps with each tick, so for a smooth bar scale a div with transform: scaleX(left / total) and a 100 ms linear transition.
Render a minigame
view.minigame holds the minigame being played, as a MinigameView, and view.scene holds its title and the running state's words. The React UI's Scene renders a MinigameCard while one runs, and useMinigame reads it for a component of your own.
import type { RunnerSnapshot } from '@ifloom/engine'
// add and drawChoices, as in the vanilla template's src/main.ts, which runs startClock(runner) to send the ticks.
import { add, drawChoices } from './runner.ts'
/** The minigame being played, under the scene's title and beats (its words): a bar counting down its timer, and a button for each input that does something now. */
export function drawMinigame(parent: HTMLElement, { view }: RunnerSnapshot): void {
const minigame = view.minigame
if (!minigame) return
const box = add(parent, 'section', '', 'minigame')
box.dataset.state = minigame.state
if (minigame.timeLeft !== null && minigame.timeTotal) {
const timer = add(box, 'div', '', 'timer')
timer.setAttribute('role', 'progressbar')
timer.setAttribute('aria-label', 'Time left')
add(timer, 'span').style.transform = `scaleX(${minigame.timeLeft / minigame.timeTotal})`
}
// Each input is a button like any other, labelled by the game's minigame.<input> strings: Up, Space.
drawChoices(add(box, 'div', '', 'choices'), minigame.inputs)
}
Each of the minigame's inputs is a ready button, with its words in label and the { type: 'minigame', input } it sends in command. Render a bar from timeLeft and timeTotal while the state has a timer, and read the minigame's own kv store in kv, such as a score. startKeys presses the inputs on the minigame's keys, or on the arrow keys and space, whatever renders the panel.
To render one minigame your own way, check view.minigame.id, such as the showcase's arrows. In the React UI, give mountGame's panels a component under the minigame's id, such as { arrows: ArrowPad }, and it renders in place of the MinigameCard.
Render a panel
view.panel holds the panel open over the story, as an OpenPanel with its name in panel and what content opened it with in with, or null. The React UI's PanelHost renders the panels it knows over the story in the default layout, and a panel of your own goes in mountGame's panels under its name.
import type { RunnerSnapshot } from '@ifloom/engine'
// runner and add, as in the vanilla template's src/main.ts.
import { add, runner } from './runner.ts'
import { drawShop } from './shop.ts'
/** The panel open over the story, rendered by its name, with a Close button unless a scene holds it open. */
export function drawPanel(parent: HTMLElement, snapshot: RunnerSnapshot): void {
const { view } = snapshot
if (!view.panel) return
const dialog = add(parent, 'section', '', 'panel')
dialog.setAttribute('role', 'dialog')
dialog.setAttribute('aria-label', view.panel.panel)
const box = add(dialog, 'div')
// The shop's counter (see the shop plugin's page); any other panel shows what it was opened with.
if (view.panel.panel === 'shop') drawShop(box, snapshot)
else add(box, 'pre', JSON.stringify(view.panel.with, null, 2))
if (!view.panel.scene) add(box, 'button', 'Close').onclick = () => runner.apply({ type: 'closePanel' })
}
Render the panel by its name, over everything else. A plugin's panel shows what its own view holds for it, such as the open counter's vendor in view.plugins.shop.panel, already filtered by the panel's with. Send { type: 'closePanel' } when the player closes it. A scene's own panel has scene set and stays while the scene is open, so give it no Close button.
The vanilla and vue templates start with a panel that shows its name and with, for you to replace. In the React UI, PanelHost renders inventory and stats itself, and each plugin UI the game lists in plugins brings its own, such as shop and jobs. Render the inventory, shop, jobs and wardrobe renders the shop's and the jobs' panels.
For more on what these sections render, see:
- Timers, Minigames and Panels.
- Browser helpers, for
startClockandstartKeys. - Add a countdown, Build a minigame and Open a shop, a job board or another panel.
Last updated on
Render pictures and play sound
Render a scene's layered picture with its hotspots, and play the game's music, sounds and voices, in a UI of your own.
Render saves, settings and the backlog
Render a save and load screen, a settings screen and the backlog with Undo in a UI of your own, in plain JS, React or Vue.