# Plane Demo and Car Demo

These solo demos let you try Flight and Wheel controls without a race or a timer. Plane Demo flies a propeller plane through an endless trail of rings over hills, a river and clouds. Car Demo drives across a desert, with a winding road, dunes, cacti, rocks and dust.

## Run locally

Install Node.js 22 or later, then start either demo from the unpacked SDK archive's folder (a source checkout needs `npm ci` first):

```sh
node examples/games/demo-host.mjs plane-demo
# Or, after stopping that process:
node examples/games/demo-host.mjs car-demo
```

The host prints the TV URL and controller link. It listens on the first free loopback port from 49200 to 49299. Open the TV URL for keyboard play; open its controller link in another browser context to use the browser controller. A phone joining takes over the one seat and starts a fresh trip. Reconnecting that same phone keeps its trip.

This local host is for same-computer development and automated browser tests. Its loopback QR does not connect a separate physical phone. For physical phones, run the modules through your existing Controller+ host on a reachable HTTPS origin. Phone browsers require a secure context, sensor support and, where requested, motion permission. Loopback is treated as secure by Chrome. A native app's permissions depend on its platform.

## Fly

The phone switches to **Flight**. Tap **TAP FOR TILT** to enable motion. Tilt sideways to turn; pull the top of the phone toward you to climb, push it away to dive. The plane levels itself when you hold the phone still. Tilt all the way to one side and hold it to roll; pull all the way back and hold it to loop. The game names each loop and roll. The throttle lever sets the speed.

The first ring waits in a bank of cloud straight ahead and is passed within about four seconds even with no input. After that a new ring always waits a few hundred metres on, following the river valley loosely. A ring you are already heading for draws the plane in; a missed ring gives way to a fresh one just ahead, and the TV says so. Each ring adds a burst of speed; a streak raises the chime.

The plane is assisted, so clumsy hands still fly well. It pulls up on its own before the ground or a rock, rolls its wings level to do it, and only then hands control back. It does not start a loop lower than 45 metres. In the tests, a minute of full dives, wild tilting or rolling toward the ground, on several worlds, touched nothing.

| Phone key | What it does |
| --- | --- |
| FIRE | Launches a firework above the nose. It bursts into sparks a second later, with a firm rumble kick at launch. |
| BOOST | Full power and more while held. Releasing it returns to the lever setting. |
| BRAKE | Slows the plane while held. |
| STEADY | Levels the wings and nose while held, from any attitude. |
| Small LEVEL key beside START | Centres tilt for the way you are holding the phone. |
| SELECT / START | Changes the camera / pauses. |

With motion off, drag the attitude instrument to fly. In Gamepad mode the left stick flies; pull it all the way back to loop, and L or R rolls. The right stick up or R2 sets the speed, L2 brakes, pressing the left stick levels the wings and pressing the right stick changes the view. On a keyboard the arrows fly at a steady bank; tap an arrow twice to roll, or the down arrow twice to loop. Space launches a firework, E gives full power, Y boosts, Shift brakes and X levels the wings. Enter pauses; R changes the camera.

The phone LCD shows the speed and the ring count, then the tilt state, the streak and where the next ring is (RING LEFT, RING AHEAD and so on), or the moment's news (RING 3!, LOOP!, PULLING UP). The attitude instrument shows the plane's actual bank and pitch, including upside down.

The TV keeps its words to one line at a time: the controller's state when it needs a hand (for example "Tap TAP FOR TILT on your phone, or drag the dial."), otherwise the next thing to try, from flying through the ring to turning, looping, rolling, boosting and fireworks. Each hint gives way after a few seconds. A small picture of the phone in the corner turns as far as the phone is tilted, with an arrow for a climb or dive, the speed lever and the keys being pressed. An arrow at the edge of the view points to the next ring when it is off screen. Ground, water and rocks still bounce and scrape the plane if it does reach them; hard hits tumble it briefly before it recovers. There is no game over.

## Drive

The phone switches to **Wheel**. Tap **TAP FOR TILT**, then tilt the phone to steer; dragging the wheel also works with motion off. Hold the phone comfortably and tap **CENTER** to make that hold straight ahead. Hold **GAS** to drive: a boost pad and then a jump ramp are straight ahead, both within the first ten seconds.

| Phone key | What it does |
| --- | --- |
| GAS | Drives. Gears change by themselves. |
| BRAKE (pedal or key) | Slows down. Held at a stop, it backs up. It always wins over GAS and the nitro. |
| BOOST | Fires the nitro while held. Boost pads, jumps and drifts refill it, and it slowly refills by itself. |
| DRIFT | Held in a bend, the car slides; sliding refills the nitro. Steering hard at speed slides too. |
| RESET | Puts the car back on the road, facing along it. |
| − / + | Shift down / up by hand. The gearbox takes over again after a moment. |
| SELECT / START | Switches between the chase and hood views / pauses. |

Every 400 m the road has a boost pad and, 120 m later, a jump ramp; the pads alternate lanes. While the wheel is near the centre, the car follows the bends, steers a jump toward the road and comes back to the road from up to 30 m away. The help fades as you steer and is gone once you steer firmly, so you can always leave the road. Cacti break when hit fast; rocks are solid.

Gamepad mode uses the left stick to steer, R2 to drive and L2 or BRAKE to slow down; the face keys keep their labels, and L / R shift gears. On a keyboard, the arrow keys drive: up drives, down slows down and backs up, left and right steer. E also drives, Q or Shift brakes, Space fires the nitro, X drifts and Y resets. R changes the view; Enter pauses.

The TV shows speed, gear and the nitro tank at the top right, the wheel and pedals as the game receives them at the bottom left, at most one line of help, and short pop-ups for boost pads, jumps, drifts and smashed cacti. The phone LCD shows speed, gear, whether tilt reaches the game, the nitro tank and one message. Pads, landings, crashes, drifts and sand send rumble. Whether a browser can vibrate depends on the device and its vibration settings.

## TV sound

Tap the TV or press a play key once to enable its sound. Both demos follow the host's mute setting. Plane Demo has a propeller engine that follows the throttle, wind that rises with speed, a chime for each ring (higher with every ring in a streak), a short fanfare for each loop and roll, a soft note for a miss, a whoosh for boost, firework launches and bursts, and impact thuds. Car Demo has an engine that follows RPM and throttle, a gear click and engine drop, boost, tyre squeal in hard turns, gravel off the road and impact thuds.

The standard `createScene(canvas, {audio: true})` host picks each game's renderer from its definition: `plane-3d` (`examples/games/plane-scene.mjs`) for Plane Demo and `car-3d` (`examples/games/car-scene.mjs`) for Car Demo. Use the scene's `enableAudio()` after a user gesture and `setMuted(on)` for the TV mute. A host using the module's direct `render()` can pass its shared `createSound()` service as `{sound}`; the vehicle synthesizer follows that service's enabled and muted flags. Dispose the renderer when leaving the game to stop its sound and release WebGL resources.

## Use your own host

Both games follow the standard game module contract and are registered in `GAMES` and `GAME_LIST` ahead of the existing games. Each accepts exactly one player. Select the player's connected seat before calling `createGame`:

```js
import {createGame} from '@controller-plus/sdk/games';
const {module, state} = createGame('plane-demo', [{id: playerId, name: 'Pilot'}], 42);
const binding = module.connect(host, state, {autoStart: false});
// In the host's simulation loop:
binding.tick(1 / 60);
// In the TV's browser animation loop, with a 2D canvas context:
module.render(ctx, state.public, {time: performance.now() / 1000});
// On exit:
binding.close();
```

The host can be a `ControllerHost`, `RelayRoom`, or the ordinary public game host interface. Motion status comes from public `status` events and `players.get(id).motion`. `connectGame` supplies input/lifecycle handling, view and instrument updates, state publication and feedback. The shared scene interpolates positions. Plane Demo advances each saved quaternion with its body rates to the interpolated time, without Euler rotation limits.

`state.public.vehicle` holds the vehicle and live input. Publish only `state.public`. Save the whole state privately as JSON, then call `restoreGame(id, saved)` to resume. A disconnect releases input and freezes the vehicle. A removed player stays frozen until the host closes the demo.

Plane Demo's world (`examples/games/demo-world.mjs`) keeps 49 chunks around the plane, generated from the seed and chunk coordinates; old geometry is disposed as new chunks enter. Its renderer builds chunks a few per frame, nearest first, so crossing into new land never stalls a frame; each chunk owns fixed blocks in the shared tree, rock and cloud meshes, and the plane's soft shadow is a blob under it rather than a shadow map. Rendering uses an origin near the plane so long trips do not exhaust floating point precision on the GPU. Physics and meshes use the same terrain sampler. The collision cache contains only nine nearby chunks; broad checks discard distant objects before testing the plane's probes, and rock shapes are shared by physics and rendering. Fireworks are bounded to eight projectiles, each with at most 16 trail samples.

Car Demo has its own world and renderer: `examples/games/car-drive.mjs` holds the road, ground, pads, ramps and physics, and `examples/games/car-scene.mjs` draws them. Its ground is laid out across the road: the road edges are mesh edges, so the road is flat and the wheels ride on exactly the triangles the TV draws. Chunks stream in within 2.5 ms of work per frame, and the renderer keeps at most 81 of them.

Both demos step their physics no larger than 1/120 second. Each demo's `render()` keeps one WebGL canvas per TV canvas. A host that discards a TV canvas, or switches it from one demo to the other, can release it with `disposePlaneRenderer(canvas)` from `examples/games/plane-scene.mjs` or `disposeCarRenderer(canvas)` from `examples/games/car-scene.mjs`; `createScene` does this itself when the game changes. When a host already has the scene's finished frame (hosts built on `createScene` pass it as `options.fx`), `render()` draws that frame and nothing else, so the 3D world is drawn once per frame.

Pass `{gameplayOnly: true}` to `render` to omit the TV text and input panel when making a game card. Render size follows the destination canvas, up to a 1920×1080 WebGL buffer. All geometry, materials, textures and Three.js files are bundled; no remote assets or account are needed.

## Test

```sh
node --test tests/demo-plane-car.test.mjs tests/demo-plane-real.test.mjs tests/plane-assist.test.mjs tests/car-demo.test.mjs
DEMO_SOFTWARE_WEBGL=1 node --test --test-concurrency=1 tests/demo-plane-car-browser.test.mjs tests/demo-plane-real-browser.test.mjs tests/flight-games-browser.test.mjs tests/car-demo-browser.test.mjs
npm test
```

The browser check uses one headless Chrome for a real local host and phone, sends simulated orientation/motion events through the phone's listeners, presses its actual touch controls, and checks game state, telemetry, calibration, pause/reconnect, rumble messages and bounded chunk storage. Set `DEMO_PLANE_CAR_ARTIFACTS` to a local directory to save TV screenshots at 1280×720 and 1920×1080, phone screenshots in portrait and landscape, and a JSON proof. The Plane Demo's gameplay card is written to `tmp/plane/plane-demo-card.png`.

`tests/plane-assist.test.mjs` flies clumsy pilots (full dives, diving turns, rolling into the ground, random tilting) for a minute on several seeds and checks they touch nothing, that the first ring is passed within five seconds with no input, that a rough aim passes most rings, and that stunts, double taps, missed rings and the phone text behave. `tests/flight-games-browser.test.mjs` plays the first seconds through a real browser phone; set `FLIGHT_GAMES_ARTIFACTS` to keep its pictures. `tests/car-demo.test.mjs` drives the Car Demo's first ten seconds with a straight wheel (a boost pad, then a big jump, on the road) and checks the road help, the gearbox, reversing, nitro, ramps, RESET, cacti and rocks, the ground mesh at road edges, the key names of each mode and a saved trip; `tests/car-demo-browser.test.mjs` plays the first seconds through a real browser phone in Wheel mode; set `CAR_DEMO_ARTIFACTS` to keep its pictures. The added playtest regressions check the plane's rolls, loops, inverted instruments/camera, ballistic fireworks, ground/water/rock contact and recovery. Browser tests render real Web Audio samples with an OfflineAudioContext and check silence under the host mute. Native-shaped iOS/Android sessions negotiate the public feedback capability and receive FIRE and impact frames through the same host output path.

These tests prove software input, transport and rendering. They do not prove a physical phone's sensors, motion permission prompts, sound, vibration, or sustained hardware frame rate.
