Scenes
One game on a page is launch. A game that is more (a start screen, menus, a campaign map between missions, an editor) is an app of scenes: it shows one scene at a time and moves between them without a blank page.
Quick start
import { appScenes, sceneAsked, startApp } from '@v12-engine/client/app'
import { editorScene } from '@v12-engine/editor'
const scenes = appScenes({ game, editor: editorScene, server: { rooms: true } })
startApp({ target: '#game', game, scenes, start: sceneAsked(scenes, 'home') })You get a start screen, the game, settings, a random map (when the rules have a generator), the map editor and online play. /?editor opens the editor directly.
Pages
| Page | What |
|---|---|
| The start screen | appScenes: the usual scenes, the home screen's buttons |
| Menus | menuScene: titles, buttons, a game playing behind, music |
| The campaign menu | the campaign between missions, briefings and debriefs |
| Loading and error screens | what shows while a game loads, and when it fails |
| Replays | recording and playing back games |
| A desktop app | offline play and an Electron window |
| Your own menus, and starting scenes | your start screen and campaign menu; opening a scene or a mission from code |
Common tasks
Add a scene of your own
const credits: Scene = {
id: 'credits',
name: 'Credits',
names: ['home'], // the scenes it leads to, checked as the app starts
open: (element, context) => {
element.textContent = 'Made by you'
element.onclick = () => context.goTo('home')
return () => element.replaceChildren()
}
}
const scenes = appScenes({ game, extra: [credits] })context.render(element, component) draws a Solid component under the app's tree, with its overlays. While running: app.scenes.register(scene, { label }), find, remove. See How do I add a scene.
Load a scene's code only when it opens
lazyScene('dev', 'Dev sandbox', () => import('./dev').then((module) => module.devScene))Its code starts coming when the pointer is over its button. sceneLoaded(scene) fetches it from code. The example's menu is 0.3 MB of script instead of 7.5 MB.
Go to another scene with arguments
context.goTo(campaignMission, { mission: 'first' })
context.goTo(sceneRef('replay'), { id, at: 30 })Args are typed (SceneRef<A>, SceneArgs<A>): shared ones (map, mods, from, generated, editing: SharedSceneArgs) plus the scene's own.
Change how scenes cross over
const iris = ({ to, done }: SceneTransitionProps) => {
to.animate([{ clipPath: 'circle(0%)' }, { clipPath: 'circle(100%)' }], 500).onfinish = done
return null
}
startApp({ target: '#game', scenes, transition: iris }) // or 'fade' (default), 'slide', 'cut'A scene can say its own (Scene.transition, on gameScene, menuScene and lazyScene).
Fetch the next scene ahead
const home = menuScene({ id: 'home', name: 'Home', title: 'My game', buttons: [{ label: 'Play', scene: play }], warm: [play] })
const app = startApp({ target: '#game', scenes: [home, play], start: 'home' })
await app.warm('campaign', { mission: 'first' })Once the menu is drawn, the code and starting files of the scenes it names are fetched with nothing drawn. In a scene's own screen: context.warm.
Reference
A scene (Scene)
| Field | What |
|---|---|
id, name | its id and its name in lists |
open(element, context) | draws it; returns what takes it away |
names | scenes it leads to, checked by startApp before anything is fetched |
transition | how it comes in |
warmFiles | files to fetch when it is warmed |
SceneOpenContext: args, goTo, scenes, runtime, render(element, component), hold() (call what it returns once the scene is ready), warm.
Kinds of scene
| Kind | Made with |
|---|---|
| a game | gameScene, cinematicScene, mapPlayScene, a campaign's missions, a replay, a server seat (GameScene); your own with asGameScene(def); isGameScene(scene) |
| a menu | menuScene (Menus) |
| the editor | editorScene of @v12-engine/editor (The map editor) |
| your own | a Scene written out |
| lazy | lazyScene(id, name, fetch, { names }) |
| online | serverScene({ game, url }) (joinServer), findGameScene, onlineScene |
A game scene's load puts a world, a session and a GameClient together; open plays it with its HUD (playScene); exit says where the HUD's leave button goes.
How a scene changes to the next
goTo builds the next scene out of sight over the current one, which stays until the next is ready; then the next comes in and the old one ends (its game disposed). A scene is ready:
- your own: when
openreturns, or whencontext.hold()'s callback is called; - a menu: when drawn;
- a lazy scene: once its code came and it is ready;
- a game: once it is on (its files there, first frames prepared), or on its error screen. A game with a
loadingscreen shows it at once; - any scene: after
startApp({ patience })seconds (8), as it is.
The first scene shows at once. Until the next comes in, the old one takes no pointer (inert) but keeps running. Layers say where they are in data-scene-layer (held, fade, slide, own, shown); the leaving one has data-scene-leaving, and its vg-leaves elements slide off. 'fade' is 240 ms and 'slide' 320 ms, times --vg-motion; at 0 every transition is a cut.
The page behaves like a game
startApp asks full screen for the whole app (app.fullscreen(), app.fullscreen(false); fullscreenButton: false removes the menus' button), stops page zoom, overscroll, touch panning, the browser's context menu and the back swipe (guards: false leaves them), and keeps the pointer as keepPointer says on the menus too. A browser in full screen still shows its top bar at the screen's top edge; a desktop build has none.
What is kept between scenes
A game a menu plays behind it is kept by scene id: going home → campaign → home makes it once; to a menu without it, it sleeps (nothing drawn, ticked or heard) for up to a minute. client.rest(), client.wake() do the same in code.
Workers
startApp({ navigationWorker, groundWorker }) build navigation and ground textures off the page's thread; both can be functions made at first use (navigationWorker: () => new Worker(…)). A project from npm create @v12-engine has both (src/navigation.worker.ts).
startApp options
| Option | What |
|---|---|
target | the element |
scenes | the scenes |
entries | the scene list's buttons |
start | the first scene (or a scene of your own) |
transition | how scenes cross over |
patience | seconds before a scene comes in as it is (8) |
fullscreenButton, fullscreenHost | full screen button; your own full screen hook |
guards | false: leave the browser's behaviour alone |
keepPointer | pointer lock on menus |
sounds, overlays, languages | interface sounds, overlay looks, languages |
stores | where saves and settings go |
onGame | told of each game played; returns what runs as it ends |
navigationWorker, groundWorker | workers |