# gizgame/game

The productive layer of gizgame: typed components over the World, one Game builder, and one context that systems and drawing share. It adds no second store: every value lives in the World's columns, its snapshot and its replay.

```gizlang
import * as Game from "gizgame/game";
```

## Types

### `Component`

```gizlang
class Component<T> { name: string; id: i64 = -1; fields: Vec<Field>; failure: string; private initial: T; private template: bytes; private slots: Vec<Vec<Leaf>>; private marker: bool; constructor(name: string, initial: T, marker: bool = false) { const template = Memory.flatten<T>(initial); const layout = marker ? { fields: [], slots: [], failure: "" }: shape(Memory.describe<T>(), name); this.name = name; this.initial = initial; this.template = template; this.marker = marker; this.fields = layout.fields; this.slots = layout.slots; this.failure = layout.failure; } of(value: T): Data { return { component: this.id, fields: this.marker ? []: this.encode(value) }; } on(): Data { return this.of(this.initial); } value(): T { return this.initial; } encode(value: T): Vec<bytes> { const words = Memory.flatten<T>(value); let out: Vec<bytes> = []; for (const slot of this.slots) { let field = b""; for (const leaf of slot) { const at = leaf.word*8; if (leaf.kind === "f32") { field+ = words.slice(at, at+4); } else if (leaf.kind === "bool") { field+ = words.slice(at, at+1); } else if (leaf.kind === "entity") { const index = words.readU64LE(at); field+ = u64Bytes(index === 18446744073709551615u ? 0u: index+1u); field+ = words.slice(at+16, at+24); } else { field+ = words.slice(at, at+8); } } out.push(field); } return out; } decode(fields: Vec<bytes>): T { if (this.marker) { return this.initial; } let words = this.template; for (const index of range(0, this.slots.length)) { const field = fields[index]; let offset: i64 = 0; for (const leaf of this.slots[index]) { const at = leaf.word*8; if (leaf.kind === "f32") { words.copyFrom(at, field, offset, 4); offset+ = 4; } else if (leaf.kind === "bool") { words.copyFrom(at, field, offset, 1); offset+ = 1; } else if (leaf.kind === "entity") { const stored = field.readU64LE(offset); words.copyFrom(at, u64Bytes(stored === 0u ? 18446744073709551615u: stored-1u), 0, 8); words.copyFrom(at+16, field, offset+8, 8); offset+ = 16; } else { words.copyFrom(at, field, offset, 8); offset+ = 8; } } } return Memory.unflatten<T>(words); } }
```

A typed component: a record type T stored in the World's columns. Its value is T; no bytes reach game code.

Opaque: create and use it through this module's functions.

### `Mark`

```gizlang
type Mark = { on: bool; }
```

A component with no data, which only marks an entity.

- `on`: `bool`

### `Pose2D`

```gizlang
type Pose2D = { x: f64; y: f64; angle: f64; }
```

Where an entity stands in 2D: logical units, and radians from +X toward +Y.

- `x`: `f64`
- `y`: `f64`
- `angle`: `f64`

### `Pose3D`

```gizlang
type Pose3D = { x: f64; y: f64; z: f64; yaw: f64; pitch: f64; roll: f64; }
```

Where an entity stands in 3D: Y up; yaw turns about Y, pitch about X, roll about Z, in radians.

- `x`: `f64`
- `y`: `f64`
- `z`: `f64`
- `yaw`: `f64`
- `pitch`: `f64`
- `roll`: `f64`

### `Pointer`

```gizlang
type Pointer = { x: f64; y: f64; width: f64; height: f64; held: bool; pressed: bool; seen: bool; }
```

The pointer in surface pixels, the surface's size, its main button, and whether it has moved over the surface yet.

- `x`: `f64`
- `y`: `f64`
- `width`: `f64`
- `height`: `f64`
- `held`: `bool`
- `pressed`: `bool`
- `seen`: `bool`

### `Stick`

```gizlang
type Stick = { x: f64; y: f64; }
```

A stick's direction, each axis in [-1, 1], +Y down as on the screen.

- `x`: `f64`
- `y`: `f64`

### `StickKeys`

```gizlang
type StickKeys = { left: Vec<string>; right: Vec<string>; up: Vec<string>; down: Vec<string>; pad: i64; }
```

The keys and gamepad axes of a stick. PAD is the gamepad's stick: 0 for the left (axes 0 and 1), 1 for the right.

- `left`: `Vec<string>`
- `right`: `Vec<string>`
- `up`: `Vec<string>`
- `down`: `Vec<string>`
- `pad`: `i64`

### `Screen`

```gizlang
type Screen = { width: i64; height: i64; alpha: f64; measure: bool; }
```

What to draw: the surface's size, the fraction toward the next tick, and whether the host measures the GPU.

- `width`: `i64`
- `height`: `i64`
- `alpha`: `f64`
- `measure`: `bool`

### `Game`

```gizlang
class Game { world: World; dt: f64; tick: i64 = 0; alpha: f64 = 1.0; simulation: Simulation | undefined = undefined; private hz: f64; private catchUp: i64; private seed: i64; private failure: string = ""; private rules: Vec<Rule> = []; private setups: Vec<Rule> = []; private actions: Vec<Action> = []; private sticks: Vec<Axis> = []; private sounds: Vec<Cue> = []; private defaults: Vec<Data> = []; private holder: Entity = absent(); private sticksId: i64 = -1; private cuesId: i64 = -1; private heard: Vec<u64> = []; private files = new Map<string, bytes>(); constructor(hz: f64 = 60.0, seed: i64 = 1, catchUp: i64 = 5) { const world = new World(); this.world = world; this.hz = hz; this.dt = 1.0/hz; this.catchUp = catchUp; this.seed = seed; this.use(Transform2D).use(Before2D).use(Transform3D).use(Before3D).use(Engine); } private fail(message: string): void { if (this.failure === "") { this.failure = message; } } use<T>(c: Component<T>): Game { if (c.failure !== "") { this.fail(c.failure); return this; } try { const id = this.world.register({ name: c.name, fields: c.fields }); if (c.id> = 0 && c.id !== id) { throw `Component ${c.name } has another ID in another Game:  register in one order`; } c.id = id; } catch (error: string) { this.fail(error); } return this; } resource<T>(c: Component<T>): Game { this.use(c); if (c.id> = 0) { this.defaults.push(c.on()); } return this; } action(name: string, codes: Vec<string>): Game { if (this.actions.length> = 64 || this.actions.some(a => a.name === name)) { this.fail(`Invalid or duplicate action ${name }`); } this.actions.push({ name, codes }); return this; } stick(name: string, keys: StickKeys): Game { if (this.sticks.length> = STICKS/2 || this.sticks.some(s => s.name === name)) { this.fail(`Invalid or duplicate stick ${name }`); } this.sticks.push({ name, keys }); return this; } sound(name: string, gain: f64 = 1.0): Game { this.sounds.push({ name, gain }); return this; } setup(run: (game: Game) => void throws string): Game { this.setups.push({ name: "setup", run }); return this; } system(name: string, run: (game: Game) => void throws string): Game { if (name === "" || name.startsWith("gizgame.") || this.rules.some(r => r.name === name)) { this.fail(`Invalid or duplicate system ${name }`); } this.rules.push({ name, run }); return this; } start(): void throws string { if (this.simulation !== undefined) { return; } if (this.failure !== "") { throw this.failure; } this.sticksId = this.world.register({ name: "gizgame.Sticks", fields: [{ name: "axes", kind: "f64", lanes: STICKS }] }); this.cuesId = this.world.register({ name: "gizgame.Cues", fields: [{ name: "counts", kind: "u64", lanes: Math.maxI64(1, this.sounds.length) }] }); let controls = Engine.value(); controls.dice = u64(Math.maxI64(1, this.seed%4294967296)); let zeros = b""; for (const at of range(0, STICKS)) { writeU64LE(zeros, 0u); } let counts = b""; for (const at of range(0, Math.maxI64(1, this.sounds.length))) { writeU64LE(counts, 0u); } this.holder = this.world.create([Engine.of(controls), { component: this.sticksId, fields: [zeros] }, { component: this.cuesId, fields: [counts] }, ...this.defaults]); let all: Vec<i64> = []; for (const id of range(0, this.world.schema().length)) { all.push(id); } let systems: Vec<System> = [{ name: "gizgame.input", reads: all, writes: all, after: [], incremental: false, structural: true, run: (world: World, tick: i64, seconds: f64, inputs: Vec<Input>): void throws string => { this.enter(tick); this.remember(); this.control(inputs); } }]; let last = "gizgame.input"; for (const rule of this.rules) { systems.push({ name: rule.name, reads: all, writes: all, after: [last], incremental: false, structural: true, run: (world: World, tick: i64, seconds: f64, inputs: Vec<Input>): void throws string => { this.enter(tick); rule.run(this); } }); last = rule.name; } for (const rule of this.setups) { rule.run(this); } this.simulation = new Simulation(this.world, systems, this.hz, this.catchUp); } private enter(tick: i64): void { this.tick = tick; this.alpha = 1.0; } initialize(input: bytes): Screen throws string { this.start(); const options = hostFrame(input); return { width: options.field("width").integer(), height: options.field("height").integer(), alpha: 1.0, measure: false }; } frame(input: bytes): Screen throws string { this.start(); const event = hostFrame(input); const simulation = this.simulation!; simulation.input({ tick: simulation.clock.tick(), sequence: simulation.nextSequence(), payload: input }); const result = simulation.frame(event.field("now").numberOr(0.0), event.field("resumed").boolean()); this.tick = simulation.clock.tick(); this.alpha = result.alpha; return { width: event.field("width").integer(), height: event.field("height").integer(), alpha: result.alpha, measure: event.field("measureGPU").boolean() }; } snapshot(): bytes throws string { this.start(); return this.simulation!.snapshot(); } restore(state: bytes): bytes throws string { this.start(); this.simulation!.restore(state); this.holder = this.first(Engine)!; this.heard = []; this.tick = this.simulation!.clock.tick(); return b""; } cues(): bytes throws string { this.start(); const counts = this.world.read(this.holder, this.cuesId, 0); let out: Vec<string> = []; let now: Vec<u64> = []; for (const at of range(0, this.sounds.length)) { now.push(counts.readU64LE(at*8)); if (this.heard.length> 0 && now[at]> this.heard[at]) { out.push(`{"sound":${quote(this.sounds[at].name) }, "gain": ${ this.sounds[at].gain } }`); } } this.heard = now; return utf8(`[${out.join(",") }]`); } receive(input: bytes): bytes throws string { const end = input.indexOf(b"\n", 0); if (end <1) { throw "A file is NAME, a newline and the bytes"; } this.files.set(string(input.slice(0, end)), input.slice(end+1)); return b""; } file(name: string): bytes throws string { const found = this.files.get(name); if (found === undefined) { throw `The host gave no file ${name }:  is it in the assets lock?`; } return found!; } restart(): void throws string { for (const entity of this.world.entities()) { if (!same(entity, this.holder)) { this.world.destroy(entity); } } for (const value of this.defaults) { for (const at of range(0, value.fields.length)) { this.world.write(this.holder, value.component, at, value.fields[at]); } } for (const rule of this.setups) { rule.run(this); } } time(): f64 { return f64(this.tick)*this.dt; } spawn(data: Vec<Data>): Entity throws string { let all = data; for (const item of data) { if (item.component === Transform2D.id && !data.some(d => d.component === Before2D.id)) { all.push({ component: Before2D.id, fields: item.fields }); } if (item.component === Transform3D.id && !data.some(d => d.component === Before3D.id)) { all.push({ component: Before3D.id, fields: item.fields }); } } return this.world.create(all); } despawn(entity: Entity): void throws string { if (this.world.alive(entity)) { this.world.destroy(entity); } } alive(entity: Entity): bool { return this.world.alive(entity); } has<T>(entity: Entity, c: Component<T>): bool throws string { return this.world.has(entity, c.id); } read<T>(entity: Entity, c: Component<T>): T throws string { if (c.fields.length === 0) { if (!this.world.has(entity, c.id)) { throw `Entity has no ${c.name }`; } return c.value(); } let fields: Vec<bytes> = []; for (const at of range(0, c.fields.length)) { fields.push(this.world.read(entity, c.id, at)); } return c.decode(fields); } write<T>(entity: Entity, c: Component<T>, value: T): void throws string { const fields = c.encode(value); for (const at of range(0, fields.length)) { this.world.write(entity, c.id, at, fields[at]); } } insert<T>(entity: Entity, c: Component<T>, value: T): void throws string { this.world.replace(entity, [...this.world.data(entity).filter(d => d.component !== c.id), c.of(value)]); } remove<T>(entity: Entity, c: Component<T>): void throws string { this.world.replace(entity, this.world.data(entity).filter(d => d.component !== c.id)); } each<T>(c: Component<T>): Vec<Entity> throws string { let out: Vec<Entity> = []; this.world.query([c.id], [], (entities: Vec<Entity>, columns: Vec<ColumnView>): void throws string => { for (const e of entities) { out.push(e); } }); return out; } first<T>(c: Component<T>): Entity | undefined throws string { const all = this.each(c); return all.length> 0 ? all[0]: undefined; } count<T>(c: Component<T>): i64 throws string { let total: i64 = 0; this.world.query([c.id], [], (entities: Vec<Entity>, columns: Vec<ColumnView>): void throws string => { total+ = entities.length; }); return total; } fetch<T>(c: Component<T>): T throws string { return this.read(this.holder, c); } put<T>(c: Component<T>, value: T): void throws string { this.write(this.holder, c, value); } pose2D(entity: Entity): Pose2D throws string { const now = this.read(entity, Transform2D); if (this.alpha> = 1.0) { return now; } const was = this.read(entity, Before2D); const a = this.alpha; return { x: was.x+(now.x-was.x)*a, y: was.y+(now.y-was.y)*a, angle: angleBetween(was.angle, now.angle, a) }; } pose3D(entity: Entity): Pose3D throws string { const now = this.read(entity, Transform3D); if (this.alpha> = 1.0) { return now; } const was = this.read(entity, Before3D); const a = this.alpha; return { x: was.x+(now.x-was.x)*a, y: was.y+(now.y-was.y)*a, z: was.z+(now.z-was.z)*a, yaw: angleBetween(was.yaw, now.yaw, a), pitch: angleBetween(was.pitch, now.pitch, a), roll: angleBetween(was.roll, now.roll, a) }; } teleport2D(entity: Entity, pose: Pose2D): void throws string { this.write(entity, Transform2D, pose); this.write(entity, Before2D, pose); } teleport3D(entity: Entity, pose: Pose3D): void throws string { this.write(entity, Transform3D, pose); this.write(entity, Before3D, pose); } private controls(): Controls throws string { return this.read(this.holder, Engine); } random(): f64 throws string { let state = this.controls(); let x = state.dice; const mask = 4294967295u; x = x ^ ((x <<13u) & mask); x = x ^ (x>> 17u); x = x ^ ((x <<5u) & mask); state.dice = x; this.write(this.holder, Engine, state); return f64(x-1u)/4294967295.0; } between(low: f64, high: f64): f64 throws string { return low+(high-low)*this.random(); } chance(p: f64): bool throws string { return this.random() <p; } private index(name: string): i64 throws string { const at = this.actions.findIndex(a => a.name === name); if (at <0) { throw `Unknown action ${name }`; } return at; } held(name: string): bool throws string { return (this.controls().held>> u64(this.index(name)) & 1u) === 1u; } pressed(name: string): bool throws string { const state = this.controls(); const bit = 1u <<u64(this.index(name)); return (state.held & bit) !== 0u && (state.was & bit) === 0u; } axes(name: string): Stick throws string { const at = this.sticks.findIndex(s => s.name === name); if (at <0) { throw `Unknown stick ${name }`; } const values = this.world.read(this.holder, this.sticksId, 0); return { x: values.readF64LE(at*16), y: values.readF64LE(at*16+8) }; } pointer(): Pointer throws string { const state = this.controls(); const bit = this.actions.findIndex(a => a.codes.includes("Mouse")); return { x: state.x, y: state.y, width: state.width, height: state.height, held: state.buttons !== 0u, seen: state.seen, pressed: bit> = 0 && this.pressed(this.actions[bit].name) }; } play(name: string): void throws string { const at = this.sounds.findIndex(s => s.name === name); if (at <0) { throw `Unknown sound ${name }`; } let counts = this.world.read(this.holder, this.cuesId, 0); counts.copyFrom(at*8, u64Bytes(counts.readU64LE(at*8)+1u), 0, 8); this.world.write(this.holder, this.cuesId, 0, counts); } private control(inputs: Vec<Input>): void throws string { let state = this.controls(); state.was = state.held; if (inputs.length === 0) { this.write(this.holder, Engine, state); return; } let clicked = false; let tapped: Vec<string> = []; for (const input of inputs) { const host = hostFrame(input.payload); state.width = host.field("width").numberOr(1.0); state.height = host.field("height").numberOr(1.0); for (const event of host.field("events").list()) { const kind = event.field("kind").text(); if (kind === "blur") { state.buttons = 0u; continue; } if (kind === "keydown") { tapped.push(event.field("code").text()); continue; } if (!["pointerdown", "pointermove", "pointerup"].includes(kind)) { continue; } state.x = event.field("x").numberOr(0.0)*state.width; state.y = event.field("y").numberOr(0.0)*state.height; state.seen = true; state.buttons = u64(Math.maxI64(0, event.field("buttons").integer())); if (kind === "pointerdown") { clicked = true; } } } const newest = hostFrame(inputs[inputs.length-1].payload); let codes = [...newest.field("keys").list().map(key => key.text()), ...tapped]; const pads = newest.field("gamepads").list(); let sticks: Vec<f64> = []; if (pads.length> 0) { const buttons = pads[0].field("buttons").list(); for (const at of range(0, buttons.length)) { if (buttons[at].field("pressed").boolean() || buttons[at].field("value").numberOr(0.0)> 0.5) { codes.push(`Pad${at }`); } } sticks = pads[0].field("axes").list().map(value => value.numberOr(0.0)); } if (state.buttons !== 0u || clicked) { codes.push("Mouse"); } let held: u64 = 0u; for (const at of range(0, this.actions.length)) { if (this.actions[at].codes.some(code => codes.includes(code))) { held = held | (1u <<u64(at)); } } state.held = held; this.write(this.holder, Engine, state); let axes = b""; for (const at of range(0, STICKS/2)) { let x = 0.0; let y = 0.0; if (at <this.sticks.length) { const keys = this.sticks[at].keys; x = axis(codes, keys.left, keys.right); y = axis(codes, keys.up, keys.down); const lane = keys.pad*2; if (lane+1 <sticks.length) { const px = sticks[lane]; const py = sticks[lane+1]; if (px*px+py*py> 0.0225) { x+ = px; y+ = py; } } const length = Math.sqrt(x*x+y*y); if (length> 1.0) { x/ = length; y/ = length; } } writeF64LE(axes, x); writeF64LE(axes, y); } this.world.write(this.holder, this.sticksId, 0, axes); } private remember(): void throws string { for (const entity of this.each(Transform2D)) { for (const at of range(0, Transform2D.fields.length)) { this.world.write(entity, Before2D.id, at, this.world.read(entity, Transform2D.id, at)); } } for (const entity of this.each(Transform3D)) { for (const at of range(0, Transform3D.fields.length)) { this.world.write(entity, Before3D.id, at, this.world.read(entity, Transform3D.id, at)); } } } }
```

A game: its components, resources, input actions, sounds and systems, run in fixed ticks over one World. Systems and
drawing receive the Game itself: typed reads and writes, spawning, queries, input, random numbers and sound cues.

- `world`: `Ref<M186.World>`
- `dt`: `f64` — The seconds of one tick.
- `tick`: `i64` — The running tick in a system; the next tick in drawing.
- `alpha`: `f64` — The fraction toward the next tick: 1 in a system, the frame's interpolation in drawing.
- `simulation`: `_gizlang_sdk_core.Option<Ref<M184.Simulation>>`
- `hz`: `f64`
- `catchUp`: `i64`
- `seed`: `i64`
- `failure`: `string`
- `rules`: `Vec<M181.Rule>`
- `setups`: `Vec<M181.Rule>`
- `actions`: `Vec<M181.Action>`
- `sticks`: `Vec<M181.Axis>`
- `sounds`: `Vec<M181.Cue>`
- `defaults`: `Vec<M186.Data>`
- `holder`: `M186.Entity`
- `sticksId`: `i64`
- `cuesId`: `i64`
- `heard`: `Vec<u64>`
- `files`: `_gizlang_sdk_collections.Map<string, bytes>`

## Functions

### `constructor`

```gizlang
constructor(name: string, initial: T, marker: bool = false)
```

NAME is the stable schema name in snapshots; INITIAL is the value of on() and the shape of every value.

### `component`

```gizlang
function component<T>(name: string, initial: T): Component<T>
```

A component of record type T, its schema NAME and its INITIAL value.

### `tag`

```gizlang
function tag(name: string): Component<Mark>
```

A tag: a component that only marks, such as Player or Enemy.

Effects: fail, read, write, alloc.

### `wasd`

```gizlang
function wasd(): StickKeys
```

WASD and the arrow keys, and the gamepad's left stick.

Effects: fail, alloc.

### `pad`

```gizlang
function pad(stick: i64): StickKeys
```

Only the gamepad's stick PAD.

Effects: fail, alloc.

### `constructor`

```gizlang
constructor(hz: f64 = 60.0, seed: i64 = 1, catchUp: i64 = 5)
```

HZ fixed ticks a second, SEED for random(), and at most CATCHUP ticks in one host frame.

Effects: fail, read, write, console, io, alloc.

## Methods

### `Component.of`

```gizlang
of(value: T): Data
```

VALUE as an entity's data, for spawn.

### `Component.on`

```gizlang
on(): Data
```

The initial value as an entity's data: a tag's only form.

### `Component.value`

```gizlang
value(): T
```

The default value.

### `Component.encode`

```gizlang
encode(value: T): Vec<bytes>
```

Each World field's little-endian bytes, cut from the value's flattened words without a per-type writer.

### `Component.decode`

```gizlang
decode(fields: Vec<bytes>): T
```

The value of FIELDS, written into a copy of the initial value's words.

### `Game.use`

```gizlang
use<T>(c: Component<T>): Game
```

Registers component C. Register every component before start; the order is the snapshot's schema.

### `Game.resource`

```gizlang
resource<T>(c: Component<T>): Game
```

Registers C as a resource: one value of the whole game, starting at its initial value.

### `Game.action`

```gizlang
action(name: string, codes: Vec<string>): Game
```

An action held while any of CODES is: keys ("KeyR", "Space"), "Mouse", or gamepad buttons ("Pad0"). At most 64.

Effects: fail, read, write, alloc.

### `Game.stick`

```gizlang
stick(name: string, keys: StickKeys): Game
```

A two-axis stick from KEYS and a gamepad stick. At most four.

Effects: fail, read, write, alloc.

### `Game.sound`

```gizlang
sound(name: string, gain: f64 = 1.0): Game
```

A sound the host plays at GAIN when a system calls play(NAME).

Effects: fail, read, write, alloc.

### `Game.setup`

```gizlang
setup(run: (game: Game) => void throws string): Game
```

RUN once at start and at each restart, to spawn the scene.

Effects: fail, read, write, alloc.

### `Game.system`

```gizlang
system(name: string, run: (game: Game) => void throws string): Game
```

A system: RUN every tick, after the systems added before it.

Effects: fail, read, write, alloc.

### `Game.start`

```gizlang
start(): void throws string
```

Builds the World once: registration errors surface here.

Effects: fail, read, write, console, io, alloc.

### `Game.initialize`

```gizlang
initialize(input: bytes): Screen throws string
```

The host's first call: builds the game and draws its first frame.

Effects: fail, read, write, console, io, alloc.

### `Game.frame`

```gizlang
frame(input: bytes): Screen throws string
```

The host's call each frame: queues its input, runs the fixed ticks it covers, and says what to draw.

Effects: fail, read, write, console, io, alloc.

### `Game.snapshot`

```gizlang
snapshot(): bytes throws string
```

The whole game state: World, tick and queued input.

Effects: fail, read, write, console, io, alloc.

### `Game.restore`

```gizlang
restore(state: bytes): bytes throws string
```

Replaces the state with a snapshot of this game; a snapshot of another schema or rate is refused first.

Effects: fail, read, write, console, io, alloc.

### `Game.cues`

```gizlang
cues(): bytes throws string
```

The sounds since the last call, as a host plays them: `[{"sound","gain"}]`. None right after a start or a restore.

Effects: fail, read, write, console, io, alloc.

### `Game.receive`

```gizlang
receive(input: bytes): bytes throws string
```

The host's call for each file before initialize: NAME, a newline and the bytes (gizapp/play, <giz-surface files>).
Files are the game's assets, not its state: no snapshot holds them.

Effects: fail, write, alloc.

### `Game.file`

```gizlang
file(name: string): bytes throws string
```

The bytes of the file NAME the host gave.

Effects: fail, read, alloc.

### `Game.restart`

```gizlang
restart(): void throws string
```

Ends every entity, puts each resource back to its initial value and runs setup again.

Effects: fail, read, write, console, io, alloc.

### `Game.time`

```gizlang
time(): f64
```

Seconds of simulation since tick zero.

Effects: fail, read.

### `Game.spawn`

```gizlang
spawn(data: Vec<Data>): Entity throws string
```

A new entity with DATA, such as [Transform2D.of(pose), Enemy.on()]. A pose starts with no motion to interpolate.

Effects: fail, read, write, console, io, alloc.

### `Game.despawn`

```gizlang
despawn(entity: Entity): void throws string
```

Ends ENTITY. Ending an entity that already ended does nothing, so two hits in one tick need no check.

Effects: fail, read, write, alloc.

### `Game.alive`

```gizlang
alive(entity: Entity): bool
```

Whether ENTITY still exists.

Effects: fail, read.

### `Game.has`

```gizlang
has<T>(entity: Entity, c: Component<T>): bool throws string
```

Whether ENTITY has C.

### `Game.read`

```gizlang
read<T>(entity: Entity, c: Component<T>): T throws string
```

C's value on ENTITY.

### `Game.write`

```gizlang
write<T>(entity: Entity, c: Component<T>, value: T): void throws string
```

Writes C's VALUE on ENTITY.

### `Game.insert`

```gizlang
insert<T>(entity: Entity, c: Component<T>, value: T): void throws string
```

Adds C with VALUE to ENTITY, or replaces its value.

### `Game.remove`

```gizlang
remove<T>(entity: Entity, c: Component<T>): void throws string
```

Takes C off ENTITY.

### `Game.each`

```gizlang
each<T>(c: Component<T>): Vec<Entity> throws string
```

The entities with C, in the World's order. Changing them while iterating is safe: the list is a copy.

### `Game.first`

```gizlang
first<T>(c: Component<T>): Entity | undefined throws string
```

The first entity with C, if any.

### `Game.count`

```gizlang
count<T>(c: Component<T>): i64 throws string
```

How many entities have C.

### `Game.fetch`

```gizlang
fetch<T>(c: Component<T>): T throws string
```

The value of resource C.

### `Game.put`

```gizlang
put<T>(c: Component<T>, value: T): void throws string
```

Writes resource C.

### `Game.pose2D`

```gizlang
pose2D(entity: Entity): Pose2D throws string
```

ENTITY's 2D pose, interpolated toward the current tick by alpha.

Effects: fail, read, write, console, io, alloc.

### `Game.pose3D`

```gizlang
pose3D(entity: Entity): Pose3D throws string
```

ENTITY's 3D pose, interpolated toward the current tick by alpha.

Effects: fail, read, write, console, io, alloc.

### `Game.teleport2D`

```gizlang
teleport2D(entity: Entity, pose: Pose2D): void throws string
```

Moves ENTITY to POSE with no interpolation from where it was, as for a wrap or a respawn.

Effects: fail, read, write, console, io, alloc.

### `Game.teleport3D`

```gizlang
teleport3D(entity: Entity, pose: Pose3D): void throws string
```

The 3D form of teleport2D.

Effects: fail, read, write, console, io, alloc.

### `Game.random`

```gizlang
random(): f64 throws string
```

A number in [0, 1) from the World's own generator, so a replay draws the same numbers.

Effects: fail, read, write, console, io, alloc.

### `Game.between`

```gizlang
between(low: f64, high: f64): f64 throws string
```

A number in [LOW, HIGH).

Effects: fail, read, write, console, io, alloc.

### `Game.chance`

```gizlang
chance(p: f64): bool throws string
```

True with probability P.

Effects: fail, read, write, console, io, alloc.

### `Game.held`

```gizlang
held(name: string): bool throws string
```

Whether action NAME is held in this tick.

Effects: fail, read, write, console, io, alloc.

### `Game.pressed`

```gizlang
pressed(name: string): bool throws string
```

Whether action NAME began in this tick.

Effects: fail, read, write, console, io, alloc.

### `Game.axes`

```gizlang
axes(name: string): Stick throws string
```

Stick NAME's direction; its length is at most 1.

Effects: fail, read, alloc.

### `Game.pointer`

```gizlang
pointer(): Pointer throws string
```

The pointer in surface pixels.

Effects: fail, read, write, console, io, alloc.

### `Game.play`

```gizlang
play(name: string): void throws string
```

Plays sound NAME once at this tick. Sounds are World counters, so a replay sounds alike and a load is silent.

Effects: fail, read, write, alloc.

## Constants

### `Transform2D`

```gizlang
const Transform2D = component<Pose2D>("gizgame.Transform2D", { x: 0.0, y: 0.0, angle: 0.0 })
```

The 2D pose. The Game keeps the pose of the last tick beside it, so drawing interpolates without game code.

### `Transform3D`

```gizlang
const Transform3D = component<Pose3D>("gizgame.Transform3D", { x: 0.0, y: 0.0, z: 0.0, yaw: 0.0, pitch: 0.0, roll: 0.0 })
```

The 3D pose, interpolated in the same way.

