Skip to content

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 ​

ts
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 ​

PageWhat
The start screenappScenes: the usual scenes, the home screen's buttons
MenusmenuScene: titles, buttons, a game playing behind, music
The campaign menuthe campaign between missions, briefings and debriefs
Loading and error screenswhat shows while a game loads, and when it fails
Replaysrecording and playing back games
A desktop appoffline play and an Electron window
Your own menus, and starting scenesyour start screen and campaign menu; opening a scene or a mission from code

Common tasks ​

Add a scene of your own ​

ts
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 ​

ts
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 ​

ts
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 ​

tsx
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 ​

ts
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) ​

FieldWhat
id, nameits id and its name in lists
open(element, context)draws it; returns what takes it away
namesscenes it leads to, checked by startApp before anything is fetched
transitionhow it comes in
warmFilesfiles 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 ​

KindMade with
a gamegameScene, cinematicScene, mapPlayScene, a campaign's missions, a replay, a server seat (GameScene); your own with asGameScene(def); isGameScene(scene)
a menumenuScene (Menus)
the editoreditorScene of @v12-engine/editor (The map editor)
your owna Scene written out
lazylazyScene(id, name, fetch, { names })
onlineserverScene({ 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 open returns, or when context.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 loading screen 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 ​

OptionWhat
targetthe element
scenesthe scenes
entriesthe scene list's buttons
startthe first scene (or a scene of your own)
transitionhow scenes cross over
patienceseconds before a scene comes in as it is (8)
fullscreenButton, fullscreenHostfull screen button; your own full screen hook
guardsfalse: leave the browser's behaviour alone
keepPointerpointer lock on menus
sounds, overlays, languagesinterface sounds, overlay looks, languages
storeswhere saves and settings go
onGametold of each game played; returns what runs as it ends
navigationWorker, groundWorkerworkers