Skip to content

Make a mission ​

A mission is a map plus objectives (what the player must do) and a script: areas, timers and sequences that wait and talk. All of it lives in your rules.

ts
// in src/rules.ts
import { createGame, enteredArea, objectiveVictory, wait } from '@v12-engine/engine'

export const game = createGame({ id: 'my-game', name: 'My game', victory: objectiveVictory })

export const bridge = game.mission('bridge', (m) => {
	// `my-game.bridge.cross`, and `my-game.bridge.briefing` below
	const cross = m.objective({ id: 'cross', text: 'objective.cross' })
	const briefing = m.sequence({ id: 'briefing', steps: [wait(1)] })
	m.on(enteredArea, (world, { area, entity }) => {
		if (area === 'bridge' && entity.owner === 1) world.objectives.complete(1, cross.id)
	})
	// played on the map `bridge`: a world made from it starts the mission, after the game's own `onStart`
	m.plays('bridge').onStart((world, { fresh }) => {
		if (!fresh) return
		world.objectives.add(1, cross)
		world.sequences.start(briefing)
	})
})

Done: on the map bridge, the player gets the objective, and walking a unit into the bridge region completes it. await game.check() reports any id given twice.

A whole mission: objectives, a briefing, raiders, a way to lose ​

ts
// in src/rules.ts
import {
	defineIds,
	defineListener,
	defineObjective,
	defineSequence,
	defineTimer,
	enteredArea,
	type Rules,
	objectiveVictory,
	talk,
	wait,
	waitUntil,
	type World
} from '@v12-engine/engine'

export const UNIT = defineIds('scout', 'raider')
const PLAYER = 1
const ENEMY = 2

/** What the player is set to do: the first must be done, the second is for the keen */
const holdTheBridge = defineObjective({ id: 'mygame.hold', text: 'objectiveHold' })
const saveTheScout = defineObjective({ id: 'mygame.saveScout', text: 'objectiveSaveScout', required: false })

/** Raiders come from the east every so often, for a while */
const raidersCome = defineTimer({ id: 'mygame.raiders', run: (world) => {
	for (let i = 0; i < 3; i++) world.spawn(UNIT.raider, ENEMY, { x: 60, z: 30 + i })
} })

/** The briefing: a word, a wait, the objectives, and the end once the bridge is held long enough */
const briefing = defineSequence({ id: 'mygame.briefing', steps: [
	wait(1),
	...talk(PLAYER, [{ speaker: 'commander', text: 'briefingHold', seconds: 4 }]),
	(world) => world.objectives.add(PLAYER, holdTheBridge, { progress: { value: 0, max: 120 } }),
	(world) => world.objectives.add(PLAYER, saveTheScout),
	(world) => void world.timers.every(40, raidersCome, { times: 3, first: 20 }),
	waitUntil((world) => world.time >= 120),
	(world) => world.objectives.complete(PLAYER, holdTheBridge.id)
] })

/** The bridge is an area: a raider that crosses it fails the mission */
const bridgeCrossed = defineListener({
	id: 'mygame.bridgeCrossed',
	event: enteredArea,
	run(world, { area, entity }) {
		if (area === 'bridge' && entity.owner === ENEMY) world.objectives.fail(PLAYER, holdTheBridge.id, 'bridgeLost')
	}
})

export const rules: Rules = {
	id: 'my-game',
	name: 'My game',
	units: [
		{ id: UNIT.scout, name: 'Scout', kind: 'unit', radius: 0.4, sight: 10, stats: { health: 100, moveSpeed: 5 } },
		{ id: UNIT.raider, name: 'Raider', kind: 'unit', radius: 0.4, sight: 8, stats: { health: 60, moveSpeed: 4 } }
	],
	areas: [{ id: 'bridge', shape: { kind: 'rect', x: 30, z: 28, width: 4, depth: 6 } }],
	timers: [raidersCome],
	sequences: [briefing],
	listeners: [bridgeCrossed],
	victory: objectiveVictory
}

/** Once the world is made: the scout at the start, and the briefing under way */
export function onStart(world: World): void {
	world.spawn(UNIT.scout, PLAYER, { x: 10, z: 30 })
	world.sequences.start(briefing)
}

A sequence is saved mid-way, so a loaded game resumes the script. That's why it is a list of steps, not an async function.

String missions into a campaign ​

ts
// in src/main.ts
import { defineCampaign } from '@v12-engine/client/app'
import { createGame } from '@v12-engine/engine'

const campaign = defineCampaign({
	id: 'my-campaign',
	name: 'The Long Road',
	// the game (`createGame`), its view given as what fetches it (`game.view(() => import('./view'))`): it (and the renderer)
	// stays out of the start screen's code, fetched as the campaign's menu opens
	game: createGame(rules).view(view),
	missions: [
		{ id: 'bridge', name: 'The bridge', map: () => bridgeMap(), onStart: (world) => start(world) },
		{ id: 'siege', name: 'The siege', map: () => siegeMap(), heroesAt: { x: 8, z: 8 } }
	]
})
// campaign.scenes among the app's scenes; campaign.menu is the campaign's own

In the editor ​

  1. Open the Code panel and click New with code to write a mission stub (objective, sequence, start). It's added to the campaign file if your game has one.
  2. Draw regions, points and paths with the Markers panel, and name them in the Inspector. A region is an area of the world by its name, so areas can stay empty.
  3. Check Used by in the Inspector to see where a name is read.
  4. Make the intro and outro in the Cinematic panel (Make a cinematic).

Reference ​

CallWhat it does
game.mission(id, (m) => …)A mission whose parts get ids prefixed by the game's and its own.
m.plays(map).onStart(…)Starts it on worlds made from that map.
world.objectives.add(player, objective, { progress })Shows an objective, with an optional progress bar.
world.objectives.complete(player, id)Marks it done.
world.objectives.fail(player, id, reason)Fails it; the reason is what the defeat screen says.
world.timers.every(seconds, timer, { times, first })Runs a timer repeatedly.
world.sequences.start(sequence)Runs a script of steps: wait, waitUntil, talk, functions.
objectiveVictoryWin when required objectives are done, lose when one fails.

An objective's text is a message key: give it words in GameView.hud.messages, in any language. Missions, dialog and objectives covers dialog, speakers and checkpoints.