The world
The World is a game in progress: its ground, players, every unit and building, and the rules that move them. It steps 30 times a second and plays the same game from the same seed.
import { createWorld, createWorldFromMap, Terrain } from '@v12-engine/engine'
// on a map: its ground, its players and everything placed on it
const world = await createWorldFromMap({ map, rules, seed: 1 })
// or by hand: flat ground and two players
const bare = await createWorld({
terrain: new Terrain(48, 48, new Float32Array(49 * 49)),
rules,
players: [
{ id: 1, name: 'Red', team: 1, color: [1, 0, 0] },
{ id: 2, name: 'Blue', team: 2, color: [0, 0, 1] }
]
})Keep nothing of a game in progress in a module variable: one set of rules can run many worlds at once (a server, a test). Keep it in the world, in components.
Find and spawn entities
Every unit, building, tree and mine is an Entity with an id, owner, pos, health and orders.
const knight = world.spawn('knight', { at: { x: 10, z: 24 }, owner: 1 })
world.get(knight.id) // the same entity, by id
world.find({ owner: 2, type: 'knight' }) // the living knights of player 2
world.named('gate') // one named on the map
world.query({ near: knight, radius: 8, is: 'enemy', sort: 'nearest', limit: 3 }) // the three nearest enemiesChange synced fields only through the world: world.setOwner, world.place, world.damage, world.kill. world.entities holds everything; world.units holds what acts each tick. Querying the world has every way to find things.
Run something every tick: systems
import { earn, type System } from '@v12-engine/engine'
const trickle: System = {
name: 'goldTrickle',
every: 30, // once a second, at 30 ticks a second
update(world, dt) {
for (const player of world.players.values()) earn(player.resources, 'gold', 1)
}
}Each tick runs systems in phases (PHASES): commands, decide, move, resolve, cleanup. See Systems.
What a tick does, in order:
- The commands sent since the last tick are carried out.
- Units are filed by where they stand, for "what is near here" (
world.spatial). They stay filed there for the whole tick, so a unit that walks into range duringmovemay be found by a search only the next tick. - The systems run, phase by phase.
- The dead are taken out of the world.
world.tickgoes up by one.- The tick's events go out to listeners.
So a listener runs once the tick is over: world.tick is already the next one, and a unit that died this tick is no longer in world.entities (the event still carries it; a fallen hero waiting to be brought back stays). Hooks run at once, inside the tick.
Order units around
knight.order('move', { x: 24, z: 24 })
knight.order('attack', enemy, { queue: true }) // after what it was told, as Shift does
knight.order('cast', { ability: 'holyLight', target: footman.id })For many units, or a player who isn't the owner:
world.issue({ type: 'order', player: 1, entities: [knight.id], order: { type: 'move', point: { x: 24, z: 24 } } })Players change the world only through commands: plain data, checked and carried out at the start of the next tick. Add your own with defineCommand (Orders and commands).
React to what happens: hooks and listeners
- Hooks run mid-decision and can change or cancel it:
beforeDamage,canPlace,targetScore,death… The engine's own rules are hooks too; give yours the same id to replace one. - Listeners hear events at the end of the tick:
entityDied,enteredArea, or your own (defineEvent).
See Hooks and Events and listeners.
import { defineHook, defineListener, earn, entityDied } from '@v12-engine/engine'
// a kill pays lumber: the same id as the engine's bounty, so it takes its place
const lumberBounty = defineHook({
hook: 'death',
id: 'bounty',
run(world, { entity, killer }) {
if (killer && world.areEnemies(killer.owner, entity.owner)) earn(world.player(killer.owner).resources, 'lumber', 5)
}
})
const said = defineListener({
event: entityDied,
id: 'mygame.said',
run(world, { entity }) {
world.log.info('deaths', 'a unit died', { entity: entity.id })
}
})Do something later: timers
Timers count ticks, never the clock, and are saved with the world:
import { defineTimer } from '@v12-engine/engine'
const waveComes = defineTimer({ id: 'td.waveComes', run: (world) => {
/* send the next wave */
} })
world.timers.after(30, waveComes)List them in Rules.timers so a loaded save finds their code. For scripts that wait and talk, use sequences (Timers and sequences).
Components
State the engine has no field for goes in a component, on an entity, a player or the world. It's plain data, saved, and can be sent to screens (replicate). See Components and world state.
import { defineComponent } from '@v12-engine/engine'
const waves = defineComponent<{ wave: number }>({ id: 'td.waves', default: () => ({ wave: 0 }) })
world.state(waves).wave++ // the world's own copy, made from the default the first timeThe same game every time
- Draw every random number from
world.rng, seeded when the world is made. NeverMath.randomor the clock. - Use the engine's
exactMath(sin,cos,atan,atan2,hypot) in rules.Math.sinand friends can differ in the last bit between Node and browsers. A view need not. checkDeterminismchecks a game (Testing and balancing rules).
Then the same seed and commands play the same game in Node, Chromium and WebKit: tests repeat, balance runs replay, bugs reproduce.
Reference
| Name | What it is |
|---|---|
world.registry | Your definitions by id: units, abilities, buffs, items, upgrades, races, prefabs, orders, formations, commands, effects, each with get, find, has, define, all. registry.unit(id) is short for units.get. |
registry.settings | The rules' knobs. |
registry.hash | Names the exact rules (mods and code included). Saves carry it. |
world.tickRate, world.dt | Ticks a second (30) and seconds a tick. |
world.step() | Runs one tick. A session does it in a page; a test does it itself. |
SpawnOptions | facing, name, lifetime, data… |
OrderBehavior | An order type: accepts, start, update, end (Add an order). |
Core engine covers every query, system, hook, event, timer, component and order.