Skip to content

Make a campaign ​

A story over several missions played in order: each has its own map and objectives, people talk, cinematics open and close it, and the hero from one mission comes into the next. defineCampaign (@v12-engine/client/app) gives you the campaign menu and a scene that plays the missions.

Quick start ​

sh
npm create @v12-engine my-campaign -- --starter campaign
cd my-campaign && npm run dev

You get a three-mission campaign with a menu, briefings, cinematics, a carried hero, saves and tests. The tutorial Let's make a basic campaign builds it chapter by chapter.

Build it yourself ​

1. A game that wins by objectives ​

ts
export const campaign: Rules = {
	id: 'my-game',
	name: 'My game',
	units,
	abilities,
	maps: Object.values(maps),
	brains: [redHand], // every brain, listener, timer and sequence a mission uses, listed so a save finds them
	listeners: [...homecomingListeners],
	sequences: [...homecomingSequences],
	victory: objectiveVictory,
	saves: { version: 1 }
}

2. The missions ​

ts
export const theLongRoad = defineCampaign({
	id: 'campaign',
	name: 'The Long Road',
	game: campaignGame,
	menu: { title: 'The Long Road', subtitle: 'A campaign in three missions' },
	missions: [
		{ id: 'homecoming', name: 'Homecoming', description: '…', map: () => campaignMap('homestead'), onStart: (world, { fresh }) => homecoming(world, fresh) },
		{ id: 'siege', name: 'The Siege', description: '…', map: () => campaignMap('fort'), onStart: (world, { fresh }) => siege(world, fresh) }
	]
})

startApp({ target: '#game', scenes: [home, ...theLongRoad.scenes, play, settings, editor], start: 'home' })

The menu shows progress and best times; a won mission is kept at once (in the browser unless store says otherwise).

3. A mission's start ​

ts
export const redHand = missionBrain({ id: 'campaign.redHand', modules: [holdPosition('camp'), waveFrom('camp', { group: 3, every: 90 })] })

export function homecoming(world: World, fresh: boolean): void {
	if (!fresh) return // a loaded save has all of this already
	world.objectives.add(PLAYER, razeCamp, { progress: { value: 0, max: enemyBuildings(world).length } })
	world.objectives.add(PLAYER, keepHall)
	world.spawnPrefab(outpost, { at: world.map.point('outpost'), owner: PLAYER })
	world.computers.seat(ENEMY, { enemyBases: [hallPos(world)], brain: redHand })
	world.timers.every(90, reinforcements, {})
	world.sequences.start(briefing)
}

Objectives, dialog and triggers: Missions. The enemy: a mission brain.

4. Cinematics ​

ts
// the rules
playCinematic(world, 'campaign.parley', { to: PLAYER, hold: true, params: { hero: hero.id, warlord: warlord.id } })
// the view
cinematics: {
	'campaign.intro': { def: readCinematic(introFile), back: true },
	'campaign.parley': { play: parley, back: true }
}

A cinematic is a file from the editor's Cinematic panel or a script given a Cut. Choices come back to the rules as cinematicChoice. See Cinematics.

5. Carry heroes and choices to the next mission ​

Heroes carry over by themselves (carryOut, carryIn): each takes the place of the map's hero of the same name or type. For anything else, type the campaign's data and give each mission's part:

ts
interface RoadData {
	footmen: UnitRecord[]
	caravan: boolean
}

export const theLongRoad = defineCampaign<RoadData>({
	missions: [
		{ id: 'homecoming', /* … */ carry: (world) => ({ footmen: carryUnits(world, PLAYER, isFootman).slice(0, 3), caravan: foundCaravan(world) }) },
		{ id: 'siege', /* … */ onStart: (world, { fresh, map, progress }) => fresh && placeUnits(world, PLAYER, progress.data.footmen, map.point('gate')) }
	]
})

6. Branches, chapters, difficulty ​

ts
defineCampaign<RoadData>({
	difficulties: ['easy', 'normal', 'hard'],
	chapters: [{ id: 'fort', name: 'II. The Fort', missions: ['siege', 'bridge', 'warlord'] }],
	missions: [
		{ id: 'homecoming', /* … */ next: (progress) => (progress.data.caravan ? 'siege' : 'bridge') },
		{ id: 'siege' /* … */ },
		{ id: 'bridge' /* … */ },
		{ id: 'warlord', /* … */ unlock: (progress) => !!progress.done.siege || !!progress.done.bridge }
	]
})

The chosen difficulty reaches each mission (context.difficulty) and its computers (world.computers.defaults).

Editor ​

  1. Name the units, regions and points the rules look for (names on a map).
  2. Write objectives and triggers in the Objectives and Rules panels.
  3. Make cinematics in the Cinematic panel.
  4. Lay out the campaign (missions, chapters, briefings, loading screens) in the Campaign panel, which writes campaign.json (the editor).

Reference ​

MissionDef ​

FieldWhat it does
id, name, description, pictureMenu entry (picture in images/)
mapThe map
onStart(world, { fresh, map, progress, difficulty })Set up the mission; fresh is false for a loaded save
carry(world)What goes on, asked once when won; merged over earlier keys
next(progress, { seconds })Which mission comes next (nothing ends the campaign)
unlock(progress)Whether it is playable from the menu
briefing{ text, picture?, voice?, objectives? }, or { cinematic: 'intro' }
loadingA LoadingLook (else the map's GameMap.loading over its preview)
debrief{ text?, rating?(outcome) }: time, stats and one to three stars
difficultyA difficulty of its own

CampaignDef ​

id, name, game, menu, missions, chapters ({ id, name, picture?, missions }), difficulties (easy, normal, hard by default), store, watch (let another page watch through a relay), worker (false keeps the missions in the page when the app has a simulation worker), file (a campaign.json, with maps and MissionCode by mission id). theLongRoad.scenes are the menu and the mission scene (campaign-mission); briefing and debrief are scenes too.

Carrying ​

Out of a missionInto the nextWhat it carries
carryOut (heroes, automatic)carryIn (or heroesAt)Type, name, level, xp, abilities, items
carryUnits(world, player, filter)placeUnits(world, player, records, at)Units (UnitRecord): type, name, health share, veterancy, items
carryResearch(world, player)giveResearch(world, player, research)Each upgrade's level
carryResources(world, player)giveResources(world, player, purse)Resources, added to the mission's start

A cinematic choice goes into a component of the mission's, and carry reads it from there.

The campaign file ​

readCampaignFile checks it; campaignFromFile gives the CampaignDef; campaignFileText writes one as the editor does (CampaignFile). The file's fields win over the code's, field by field.

In a worker ​

Give startApp a worker whose file calls serveCampaign(game.toRules(), { missions }) (from @v12-engine/client/campaign-worker), and each mission's simulation runs off the page's thread, with onStart, carry, the heroes carried in and a cinematic briefing. See Keep the page smooth with workers. A campaign with watch stays in the page (worker: false).

Saves ​

The mission scene saves from the HUD; the menu lists saves. saves: { version: 1 } keeps old saves loading after a patch (Saving and loading). world.checkpoint({ key, params }) gives the defeat screen a retry point.

The starter's missions ​

Homecoming (missions/homecoming.ts): required and optional objectives, watched areas, a prefab, a mission brain, a reinforcements timer. The Siege (missions/siege.ts, waves.ts): waves by timers and events, a custom Waves panel, a gate, night lighting. The Warlord (missions/warlord.ts): the carried hero, a shop, a cinematic with a choice, prisoners who change sides, the outro.