Systems
A system is code that runs every tick, or every few ticks: an income, a victory check, a king-of-the-hill score. The engine itself is made of systems (movement, combat, production), and a game adds its own or replaces the engine's.
Quick start
ts
import { earn, type System } from '@v12-engine/engine'
export const trickle: System = {
name: 'goldTrickle',
every: 30, // once a second, at 30 ticks a second
update(world) {
for (const player of world.players.values()) earn(player.resources, 'gold', 1)
}
}Put it in Rules.systems.
Common tasks
Decide who won
ts
const kingOfTheHill: System = {
name: 'kingOfTheHill',
phase: 'resolve',
every: 10,
update(world, dt) {
// look at the world, and when someone has won:
// world.winner = team; world.events.emit(gameOver, { winner: team })
}
}
export const mode: Rules = { id: 'king-of-the-hill', name: 'King of the Hill', units: [knight], victory: kingOfTheHill }packages/engine/tests/hello-mode.test.ts is this whole mode in about fifty lines.
Look at units each tick
Iterate world.units (what acts each tick; scenery is left out), or ask for just what you need with world.query: world.query({ near: tower, radius: 9, is: 'enemy' }).
Replace or drop an engine system
A system with the same name as a core system (coreSystems) replaces it. Rules.withoutSystems drops some. world.addSystem and world.removeSystem change them while the game runs. See Add a system.
Reference
- Systems run in phases, in this order:
commands,decide,move,resolve,cleanup(PHASES). - A system says its
phase,beforeorafterother systems, andevery: nticks (withoffset). - A system with state of its own for each world is a function,
(world) => System. Keep the state in the world otherwise (Components). update(world, dt)is told the seconds since it last ran.