Skip to content

The start screen ​

appScenes gives a game its usual scenes in one call: the start screen (what the game is, a button for each scene), the game, the settings, a random map, the map editor and a server. It returns a plain list, so you add or remove scenes.

Quick start ​

ts
const scenes = appScenes({
	game,
	maps,
	onStart,
	home: { buttons: [{ label: 'Campaign', scene: campaign.menu }], behind: livingScene, component: StartScreen },
	extra: campaign.scenes,
	scenes: { play: { name: 'Free play' } }
})
startApp({ target: '#game', game, scenes, start: sceneAsked(scenes, 'home') })

The start screen gets a Campaign button first, a game playing behind it, and your own look; Play is renamed Free play; the campaign's scenes are added.

Common tasks ​

Rename a scene or give it a loading screen ​

ts
appScenes({ game, scenes: { play: { name: 'Free play', label: 'Play now', loading: { text: 'Raising the banners…' } } } })

id replaces a scene's id.

Add Exit for a desktop build ​

ts
appScenes({ game, menu: { buttons: [exitButton(shell)] } })

exitButton(shell) shows only where the shell can quit. See A desktop app.

Load the view and the editor only when needed ​

ts
const game = createGame(rules).view(() => import('./view'))
const scenes = appScenes({
	game,
	editor: (scene) => import('@v12-engine/editor').then((editor) => editor.editorScene(scene)),
	server: true
})

The start screen then loads none of the looks, the client that draws them, or the renderer: an empty project's start screen loads 0.7 MB of script instead of 4.9 MB.

Make the random map's preview in a worker ​

ts
// generate.worker.ts
import { serveMapGenerator } from '@v12-engine/engine'
import { game } from './game'
serveMapGenerator(game)

// main.ts
appScenes({ /* … */ generatorWorker: () => new Worker(new URL('./generate.worker.ts', import.meta.url), { type: 'module' }) })

Fetch Play's code ahead ​

ts
appScenes({ game, menu: { warm: ['play'] } })

Reference ​

appScenes options ​

OptionWhat
gamefrom createGame: its rules, maps and start
maps, onStartother maps and start than the game's
homethe start screen: buttons, after, behind, component
menuthe start screen's menu: buttons, warm
scenesper scene: name, label, loading, id
extrascenes of your own
editorthe editor scene, or a function returning a promise of it
servertrue, or { rooms: true } for rooms
saves{}: the game can be saved from its pause menu
generatorWorkermakes the random map's worker

Every scene but the start screen is lazy. Pushing to the list afterwards (scenes.push, home.buttons.unshift) still works but is the old way, and ids is deprecated.

A project from npm create @v12-engine is the example: its start screen (home, in src/main.ts) has Play, which opens play, whose Home button goes back.