# The window.__game convention
https://paprika3d.com/docs/convention

Any three.js, React Three Fiber or WebGL page can be opened, looked at and driven without it: `open`, `look`, `observe` (which falls back to the page's named three.js objects), `eval`, `interact` with keys, mouse and touch, `feel` and `perf` work on a page that knows nothing about the harness. To read and set a game's exact state, the game also exposes `window.__game`.

Once the page has loaded, it exposes:

```js
window.__game = {
  describe(),        // a short description: name, controls, units
  observe(),         // the state, as plain JSON
  act(input),        // performs one input object: { key, down? } or { action, ...args }
  setState(partial), // sets any of observe()'s keys; keys left out keep their values
  subject,           // optional: the name of the object the game is about
  intentional,       // optional: what look should not flag again
};
```

`observe`, `look` and `perf` work on any three.js page; `window.__game` makes the state exact. The optional keys and each rule below in full: `references/tools.md`.

## Rules

- **Start.** The game plays as soon as the page loads: no title screen, no click to start, nothing over the first frame (loading text, a dim veil, a fade). `open` pauses at frame 0. A title screen the user asks for puts `started` in `observe()` and `setState`, so a `{ state: { started: true } }` step skips it.
- **Time.** The harness steps frames of 1/60 s on a fake clock (`requestAnimationFrame`, `performance.now`, `Date`, timers, CSS animations and transitions). Advance the game by each frame's elapsed time, at most 0.1 s a frame, and stay stable at 0.1 s: springs and other physics stepped once a frame can blow up there though every 1/60 s frame passes. A fixed step (physics, rewind) runs in an accumulator and draws between its last two steps (`prev + (cur - prev) * acc / STEP`), or movement stutters on a 100 or 120 Hz display. The clock reads 60 s at frame 0. Never read real time some other way.
- **Randomness.** A game-state game uses `state.random()`; a game that keeps its own state uses `Math.random`, which the harness seeds.
- **setState** takes effect at once, even before the first frame, and the game's start-up must not undo it. Objects merge; lists are replaced whole.
- **observe()** returns plain JSON, never three.js objects. Lists keep their order.
- **Keys** listen on `window` and match `event.code` (`KeyW`, `Space`). One-shot controls act on `keydown` and skip `event.repeat`; held controls last from `keydown` to `keyup`. Real key events and `act({ key })` go through the same function.
- **Names.** Give meshes a `name` (`player`, `coin`): `look` lists what is on screen by name. Put the main object's named parts under one named group and set `subject` to it.

`templates/plain.html` is a complete small game in this shape. In React, keep the state in a plain module and set `window.__game` in the Canvas's `onCreated` (`templates/r3f.tsx`).

The harness sends keys, mouse and touch as real browser events, so the game's own listeners receive them. An `{ action, ...args }` object reaches `__game.act` whole.

Updated 2026-11-16.
