7. The campaign menu
Goal: tie the three missions together with defineCampaign: a menu with progress and times, saves from the HUD, and the hero carried from mission to mission.
Describe the campaign in a file
src/campaign/campaign.json holds its name, chapters and each mission's map, menu words, briefing and debrief. The editor's Campaign panel writes it.
{
"id": "campaign",
"name": "The Long Road",
"chapters": [
{ "id": "road", "name": "I. The Way Home", "missions": ["homecoming"] },
{ "id": "fort", "name": "II. The Fort", "missions": ["siege", "warlord"] }
],
"missions": [
{
"id": "homecoming",
"name": "Homecoming",
"map": "homestead",
"description": "Through the woods, and burn the camp of the Red Hand beyond them",
"briefing": { "text": "Aldric is home from the war…", "objectives": ["Burn the camp of the Red Hand", "Aldric must live"] },
"debrief": { "text": "The camp burns. The valley breathes again, for a night: the Red Hand has a fort in mind." }
},
{ "id": "siege", "name": "The Siege", "map": "fort" },
{ "id": "warlord", "name": "The Warlord", "map": "pass" }
]
}Add the code, by mission id
// src/campaign/scenes.ts
import { defineCampaign } from '@v12-engine/client/app'
import campaignFile from './campaign.json'
import { campaignGame } from './game'
import { campaignMap, homecoming, homecomingCarry, type RoadData, siege, siegeCarriedIn, warlord, warlordCarry } from './index'
export const theLongRoad = defineCampaign<RoadData>({
file: campaignFile,
// the game (`./game.ts`, `createGame`): its rules, and its look fetched as the menu opens: the start screen's code
// carries none of the renderer
game: campaignGame,
// a mission's map by the name the file gives it: `homestead` is `src/maps/homestead.vgmap.json`
maps: campaignMap,
missions: [
{
id: 'homecoming',
// its debrief's words are the file's; its rating, by how quick it was, is code
debrief: { rating: ({ seconds }) => (seconds < 600 ? 3 : seconds < 1200 ? 2 : 1) },
onStart: (world, { fresh }) => homecoming(world, fresh),
// the footmen who live through it, and whether the caravan was found
carry: (world) => homecomingCarry(world)
},
{
id: 'siege',
onStart: (world, { fresh, map, progress }) => {
siege(world, fresh)
// the footmen of Homecoming man the fort; a game loaded from a save has them already
if (fresh) siegeCarriedIn(world, map, progress.data)
}
},
{
id: 'warlord',
// harder or easier with the campaign's difficulty, chosen in its menu
onStart: (world, { fresh, difficulty }) => warlord(world, fresh, difficulty),
// the answer at the gate: the last mission, so the campaign keeps it for whatever comes after
carry: (world) => warlordCarry(world)
}
]
})Each mission gets its map (from the file) and its onStart, told fresh: false for a loaded save. carry is what a won mission hands on besides the hero. Give the view as a loader so the start screen doesn't carry the renderer.
The menu offers a Difficulty (difficulties to change them), passed to each mission and its computers. theLongRoad.scenes are the menu (campaign) and the mission scene (campaign-mission). Progress is kept in the browser unless you give a store.
Carry the hero, and more
The hero carries over by itself: type, name, level, experience, abilities and items, placed where the next map's hero of the same name or type stands (or at the start, or heroesAt).
Anything else is your own data (RoadData): survivors, the caravan, the answer at the gate.
// src/campaign/carry.ts
export function homecomingCarry(world: World): Pick<RoadData, 'footmen' | 'caravan'> {
const footmen = carryUnits(world, PLAYER, (unit) => unit.def.id === UNIT.footman)
.sort((a, b) => b.health - a.health)
.slice(0, FOOTMEN_CARRIED)
return { footmen, caravan: world.objectives.has(PLAYER, findCaravan.id) && world.objectives.get(PLAYER, findCaravan.id).status === 'done' }
}carryUnits and placeUnits carry units; carryResearch and carryResources carry research and resources. The next mission reads it from progress.data.
Add it to the page
// src/main.ts
import { gameScene, menuScene, type Scene, sceneRef, settingsScene, startApp } from '@v12-engine/client/app'
import { theLongRoad } from './campaign/scenes'
import { editor } from './editor'
import { game } from './game'
import { mapNamed, maps } from './maps'
const play = gameScene({
id: 'play',
name: 'Free play',
game,
map: () => mapNamed(),
debug: true,
exit: { label: 'Home', scene: sceneRef('home') }
})
const settings = settingsScene({ game, back: { scene: sceneRef('home') } })
const home = menuScene({
id: 'home',
name: rules.name,
title: rules.name,
description: 'A campaign made with V12: three missions, a hero carried from one to the next.',
details: [
{
label: 'Maps',
value: Object.values(maps)
.map((map) => map.name)
.join(', ')
}
],
buttons: [
{ label: 'Campaign', scene: theLongRoad.menu },
{ label: 'Free play', scene: play },
{ label: 'Map editor', scene: editor },
{ label: 'Settings', scene: settings }
]
})
const scenes: Scene[] = [home, ...theLongRoad.scenes, play, settings, editor]
startApp({ target: '#game', title: rules.name, scenes, start: 'home' })Free play plays a map with its mission's rules and nothing kept, the same as Play here.
Saves
The mission scene saves and loads from the HUD; the menu lists the saves. A loaded save calls onStart with fresh: false. saves: { version: 1 } plus saves.migrate keeps old saves loading after a patch. world.checkpoint marks where Retry goes back to.
Try it
Run npm run dev, click Campaign, and begin Homecoming. Win it (the debug panel's cheats help), and the end screen offers The Siege. Aldric arrives with what he earned.
Next: 8. Tests.