Skip to content

Add a scene ​

An app is a list of scenes: a game, a menu, a cinematic, the editor, or your own. appScenes gives you the usual ones as a plain array; push yours onto it.

ts
// in src/main.ts
import { appScenes, cinematicScene, gameScene, type Scene, menuScene, sceneAsked, sceneRef, startApp } from '@v12-engine/client/app'
import { readCinematic } from '@v12-engine/client'
import { editorScene } from '@v12-engine/editor'
import introFile from './cinematics/intro.json'
import { game } from './game'
import { mapNamed, maps } from './maps'
import { onStart } from './rules'

const scenes = appScenes({ game, maps, onStart, editor: editorScene, server: true, settings: true })

// the scenes appScenes made, named by their ids: startApp says as it starts if one is not there
const home = sceneRef('home')
const play = sceneRef('play')
// a cinematic before the game, then the home screen
scenes.push(cinematicScene({ id: 'intro', game, map: () => mapNamed('field'), cinematic: readCinematic(introFile), then: { scene: home } }))
// a game on a map of its own, at dusk, with replays kept
const siege = gameScene({ id: 'siege', name: 'The siege', game, map: () => mapNamed('siege'), onStart, environment: 'dusk', replays: true })
scenes.push(siege)
// a menu: data, drawn by the app
const chapters = menuScene({
	id: 'chapters',
	name: 'Chapters',
	title: 'The Long Road',
	buttons: [
		{ label: 'The bridge', scene: play, args: { map: 'field' } },
		{ label: 'The siege', scene: siege }
	]
})
scenes.push(chapters)
// a scene of your own: any element
const credits: Scene = {
	id: 'credits',
	name: 'Credits',
	open(element, { goTo }) {
		element.innerHTML = '<h1>Made with care</h1><button>Back</button>'
		element.querySelector('button')!.onclick = () => goTo(home)
		return () => (element.innerHTML = '')
	}
}
scenes.push(credits)

startApp({ target: '#game', title: 'My game', scenes, start: sceneAsked(scenes, 'intro') })

Done: the app opens on the intro cinematic, then the home screen, with a Chapters menu, a siege at dusk and a credits page.

Add a credits screen ​

ts
appScenes({
	game,
	rules,
	maps,
	credits: {
		sections: [
			{ title: 'Design', names: ['Ada'] },
			{ title: 'Music', names: ['Clara'] }
		],
		music: '/music/credits.ogg'
	}
})
// or on its own
creditsScene({ sections, back: { scene: home } })

Reference ​

CallWhat it does
gameScene({ id, game, map, onStart, … })A game on a map. environment, replays and more.
menuScene({ id, title, buttons })A menu from data.
cinematicScene({ id, game, map, cinematic, then })A cinematic, then another scene.
creditsScene({ sections, back })Scrolling credits.
SceneYour own: open(element, { goTo }) returns what cleans up.
sceneRef(id)Names a scene made elsewhere. startApp reports any it can't find (missingScenes).
sceneAsked(scenes, fallback)The scene the address asks for (/?credits), else the fallback.
goTo(scene, args)Opens another scene and ends this one. Args are type-checked through SceneRef.

See Scenes and the screen, and A campaign of missions for defineCampaign.