Declared, or in code
Anything a game can say as data, it can also decide in code. Start with the field; use code for the one case the field can't cover.
ts
skeleton: {
height: 1.4,
shapes: [/* … */],
// a skeleton a cannon killed blows apart harder
onDeath: (_skeleton, death) => (death.killerType === 'cannonTower' ? { debris: bones(1, 1.5), strength: 2.4 } : 'debris')
}Code takes one of three shapes:
- A function in the field's place, asked each time:
onDeathnext todebris,modelFornext tomodel. - A hook the engine runs while it decides:
targetScore,canPlace,beforeDamage. - An API to change it while the game runs:
world.sides.setTeam,world.areas.add,client.theme.set,client.looks.set.
In the rules
| Declared | In code |
|---|---|
an ability's range, castTime | rangeOf, castTimeOf; the hooks canCast, canTarget |
| attack stats | the hooks canAttack, beforeAttack; RuleSettings.combat.splash |
a buff's stats | change(world, carrier, instance), the stats hook |
| teams on the map | world.sides.setRelation, world.sides.setTeam, world.sides.shareVision |
areas | world.areas.add, world.areas.remove |
onError: 'end' | onError: (error, where) => (where.mod ? 'disable' : 'end') |
saves: { version, migrate } | migrate: (save, from, to) => …, onMismatch: (save, hash) => … |
log: { level: 'warn' } | world.log.level = 'debug', world.log.to(sink) |
ts
// no building on the road: a rule no field says
const keepTheRoadFree = defineHook({
hook: 'canPlace',
id: 'keepTheRoadFree',
run(world, event) {
if (world.map.path('road').contains(event.site)) event.cancelled = true
}
})Four ways to change any piece of the interface
From least work to most control:
- Declared: a field of the view: words, colours, what's hidden.
- In code: a function, hook or API.
- A component (Solid) in the client's place, given what the client's own gets, so it can wrap it.
- An element of your own:
mount(element, …)with any HTML or framework, returning what cleans up.mountwins overcomponent.
tsx
// 1. declared
outcome: { victory: 'The road holds', defeat: 'The gate has fallen' }
// 2. in code: buttons of the game's own, and the client's left out
outcome: { buttons: [{ label: 'Next mission', run: (client) => goNext(client), when: 'victory' }], standard: false }
// 3. a component, given what the client's own is given (`OutcomeScreen` to wrap)
outcome: { component: (props) => <MyOutcome outcome={props.outcome} restart={props.restart} /> }
// 4. an element of your own, with any framework
outcome: {
mount(element, client, outcome) {
element.innerHTML = `<h1>${outcome === 'victory' ? 'Won' : 'Lost'}</h1>`
return () => (element.innerHTML = '')
}
}| Way | A HUD part |
|---|---|
| Declared | hud: { hide: ['minimap'] } |
| In code | client.hud.hide('minimap'); back with client.hud.hide('minimap', false) |
| A component | hud: { widgets: [{ id: 'card', component: MyCard }] }, the client's card as children |
| An element | hud: { widgets: [{ id: 'card', mount: (element, client) => { /* draw */ return () => {} } }] } |
The same four ways work for loading and error screens, tooltips, toasts, dialogs, menus, the lobby, the pause menu, keys and editor panels. Screens that do something (saves, a campaign map, the lobby, replays) split into a controller and a view: your view gets the same controller, so you never rewrite what the screen does. See The interface.