Rules and their look
A game is two halves: its rules (Rules, what happens) and its look (GameView, how it appears). createGame makes them one object. They meet only by id.
import { createGame } from '@v12-engine/engine'
import { rules } from './rules'
export const game = createGame({ ...rules, checkView: 'throw' })
// how it looks, fetched as a page first plays it: a server and a test take the game without the client
game.view(() => import('./view'))The rules name a unit type 'knight'; the look says what a 'knight' looks like. The rules send { key: 'waveComing' }; the look has the words.
| Type | Holds | Imports | Runs | |
|---|---|---|---|---|
| Rules | Rules | units, abilities, buffs, resources; hooks, orders, systems, timers, victory | @v12-engine/engine only | page, worker, server, test |
| Look | GameView | models, parts, sounds, icons, words, the HUD | engine, client, its rules | a player's screen only |
The rules
import { defineIds, type Rules, type IdOf } from '@v12-engine/engine'
export const UNIT = defineIds('knight')
export type Unit = IdOf<typeof UNIT>
export const rules: Rules = {
id: 'my-game',
name: 'My game',
units: [
{
id: UNIT.knight,
name: 'Knight',
kind: 'unit',
radius: 0.5,
stats: { health: 400, attackDamage: 25, attackSpeed: 1, attackRange: 0.6, moveSpeed: 4 },
sight: 12,
attacks: [{ damagePoint: 0.4, acquisitionRange: 8 }]
}
]
}Everything a game defines is listed here: abilities, buffs, resources, orders, commands, hooks, listeners, timers, components, events, systems, victory, maps, saves. What happens once a world is made goes in onStart(world, { map }).
The look
import type { GameView } from '@v12-engine/client'
import type { Unit } from './rules'
export const view: GameView<Unit> = {
units: {
looks: {
knight: { height: 1.6, shapes: [{ shape: 'box', size: [0.7, 1.4, 0.7], offset: [0, 0.7, 0], color: 'team' }] }
}
}
}GameView<Unit> makes the compiler ask for a look for every unit type and refuse one for a type that doesn't exist. From there a view adds models, sounds, effects, icons, words, the HUD, and code where data isn't enough (Declared, or in code).
Put it on a page
import { launch } from '@v12-engine/client'
import '@v12-engine/client/styles.css'
import { game } from './game'
// on its first map, with its start (`map` and `onStart` say others)
const launched = launch({ target: '#game', game, debug: true })
// launched.ready: the GameClient once loaded; launched.restart(); launched.dispose()Give the target a size. Instead of a map, launch takes a save (from), a world made in code, a session to join a game elsewhere, or a worker. For menus and more than one game, use startApp (Add a scene).
Why keep them apart
- A server runs the rules alone.
startServer({ game })never fetches the view. - Tests run the rules alone.
testWorld(game)steps exactly as a page does, in milliseconds. - A worker runs the rules alone.
serveWorld(game.toRules(), { onStart: game.toStart() }). - The look can't cheat. It sees only snapshots and acts only through commands.
- Each changes alone. New models or words don't change the rules' hash, so saves and replays still work.
Reference
Game member | What it is |
|---|---|
game.toRules() | The Rules underneath. |
game.view(load) | Sets the look, fetched when a page first plays. |
game.toView() | The look, fetched. |
game.maps(…), game.onStart(…) | Its maps and its start. |
game.unit(…), game.on(event, …), game.mission(id, …) | Register parts one by one. |
The page checks that rules and look fit as it loads the view (loadGame): anything the view names that the rules don't have is a warning, or an error with checkView: 'throw', or nothing with checkView: false. checkGame({ rules, view }) gives the same list in code. checkLooks(view, modelFactsOf(manifest)) also reports clips, parts and nodes a look names that its model lacks. Whatever is passed game takes it whole: launch, startApp, openEditor, startServer({ game }), testWorld(game).