4. Mission two: the siege
Goal: hold the fort through waves at night and keep the hall; losing a tower fails the optional objective. New: waves driven by timers and events, a Waves panel in the editor, a gate, effects from the rules, torches and a bonfire.
Make the map
- Build the fort: hall, barracks, farm, mine, a wall with towers at its ends and a gap; Aldric, footmen and archers inside; a war camp north with a road to the gap.
- Name the hall
hall, the knighthero, and add a pointwarcampfacing the fort. - In Map settings › Day, set night, and keep it there.
- Place a gate model from Content (
rts/castle_gate.glb) in the gap. In the Inspector, name itgate, tick Gate, and set its reach. - Add warm lights with Markers › Light, and effects with Markers › Effect:
torchon the gate posts and towers,campfirein the yard. See A map lit at night.
Put the waves in a file
src/campaign/waves.json, a game file (defineGameFile({ id: 'waves.json', data: wavesData, read: readWaves }) in Rules.files), read with siegeWaves(world):
[
{ "units": [{ "type": "raider", "count": 4 }], "interval": 1.5, "bonus": 60 },
{
"units": [
{ "type": "raider", "count": 5 },
{ "type": "warlock", "count": 2 }
],
"interval": 1.2,
"bonus": 80
}
]Run the waves on timers and events
// src/campaign/waves.ts
export const waves = defineComponent<WaveState>({ id: 'campaign.waves', default: () => ({ wave: 0, phase: 'idle', timer: 0, queue: [] }) })
export const wavesOf = (world: World): WaveState => world.state(waves)
const waveComes = defineTimer({ id: 'campaign.waveComes', run: (world) => {
if (world.winner !== null) return
const state = wavesOf(world)
const { units, interval } = siegeWaves(world)[state.wave]
// where Retry goes back to: the start of this wave
world.checkpoint({ key: MSG.checkpointWave, params: { wave: state.wave + 1 } })
state.phase = 'coming'
state.queue = units.flatMap(({ type, count }) => Array.from({ length: count }, () => type))
world.timers.every(interval, raiderComes, { times: state.queue.length, first: 0 })
const camp = world.map.point(WAR_CAMP)
world.events.emit(waveComing, { wave: state.wave + 1, x: camp.x, z: camp.z })
world.message(PLAYER, { key: MSG.waveComing, params: { wave: state.wave + 1 } }, 'info')
} })
const lastOfTheWave = defineListener({
event: entityDied,
id: 'campaign.lastOfTheWave',
run(world, { entity }) {
const state = wavesOf(world)
if (entity.owner !== ENEMY || state.phase !== 'fighting' || raiders(world) > 0 || world.winner !== null) return
earn(world.player(PLAYER).resources, 'gold', siegeWaves(world)[state.wave].bonus)
state.wave++
world.events.emit(waveBeaten, { wave: state.wave })
if (state.wave >= siegeWaves(world).length) state.phase = 'over'
else waitForWave(world, BETWEEN_WAVES)
}
})Wave state lives in a component in the world, not a module variable, so it's saved with the timers it waits on. waveComing and waveBeaten are events of your own (defineEvent). A nextWave command calls the next wave early for gold.
Shut the gate and fire effects
// src/campaign/missions/siege.ts
export const waveSetsOut = defineListener({
event: waveComing,
id: 'campaign.waveSetsOut',
run(world, { x, z }) {
if (!world.objectives.has(PLAYER, holdOut.id)) return
world.fx('spark-burst', { at: { x, z }, params: { size: 2 } })
if (setGate(world, GATE, false)) world.message(PLAYER, MSG.gateShut, 'info')
}
})
export const waveHeld = defineListener({
event: waveBeaten,
id: 'campaign.waveHeld',
run(world, { wave }) {
const { objectives } = world
if (!objectives.has(PLAYER, holdOut.id)) return
objectives.progress(PLAYER, holdOut.id, wave, { params: { wave } })
if (wave >= siegeWaves(world).length) {
objectives.complete(PLAYER, holdOut.id)
objectives.complete(PLAYER, keepTheHall.id)
return
}
if (setGate(world, GATE, true)) world.message(PLAYER, MSG.gateOpen, 'info')
}
})setGate(world, 'gate', open) shuts and opens the gate for every screen. world.fx fires an effect everyone who sees the place sees, in replays too. The view adds a camera shake with a viewListener on waveComing.
Add a Waves panel to the editor
// src/waves-panel.ts
function change(editor: EditorClient, name: string, make: (waves: Wave[]) => void, merge?: string): void {
const was = copy(siegeWaves(editor))
const next = copy(siegeWaves(editor))
make(next)
const put = (waves: Wave[]) => {
// the editor's game and the game played here: their registries', no other world's
editor.registry.setFile(wavesFile, waves)
editor.world?.registry.setFile(wavesFile, copy(waves))
void editor.files.write(FILE, waves)
}
editor.history.step({ name, merge, do: () => put(next), undo: () => put(was) })
}
export const wavesPanel: EditorPanelDef = {
id: 'campaign.waves',
name: 'Waves',
icon: 'campaign.wave',
side: 'right',
key: 'Shift+KeyW',
badge: (editor) => siegeWaves(editor).length,
prefix: 'w',
search: (editor) => siegeWaves(editor).map((wave, index) => ({ id: String(index), label: `Wave ${index + 1}`, detail: describe(wave) })),
mount
}Each change is one undo step, written to waves.json, and picked up at once by the game played in the editor. List the panel in an editor extension:
// src/campaign-editor.ts
export const campaignEditor = defineEditorExtension({
id: 'campaign',
name: 'The campaign',
panels: [wavesPanel],
icons: editorIcons,
search: editorSearch
})and pass extensions: [campaignEditor] in src/editor.ts. See Make an editor panel.
Try it
Open the fort and click Play here. You get 45 seconds to build; then a horn, a flare at the war camp, the gate shuts and raiders come. Between waves the gate opens; press N to call the next one early for gold.
Next: 5. Mission three: the hero.