# Pause menu

> Launch a menu and pause the world: a paused scene keeps drawing, which is what makes this possible. Plus slow motion.

Example `pause-menu`, running at https://nacatamalon.com/en/examples/pause-menu. The whole catalogue: https://nacatamalon.com/llms/examples.md.

`scene.launch('PauseMenu')` then `scene.pause()`. The engine has two pauses and only one of them is any use here: `setPaused` freezes the whole loop and **stops drawing**, which would leave the menu floating over a black screen. Pausing a scene freezes it and **keeps drawing it**: the world stops mid-bounce and stays visible underneath.

Press **Space** to pause and resume, and **S** for slow motion, which is a different thing again: at `timeScale: 0` the updates still run, they just run over no time at all.

## Code

`index.ts`:

```ts
import { createGame } from 'nacatamalon';
import * as Settings from './settings';
import { World } from './scenes/world';
import { PauseMenu } from './scenes/menu';

const game = createGame('#app', {
    width: Settings.WIDTH,
    height: Settings.HEIGHT,
    background: '#05080f'
});

// Declare Scenes
game({
    World,
    PauseMenu
}, 'World');
```

`components/mover.ts`:

```ts
import { createSprite, getColor, useScene, useUpdate } from 'nacatamalon';

export const Mover = (index: number) => {
    const speed = 40 + index * 26;
    const size = 26 + index * 6;
    const colour = ['#5f8fd8', '#d8905f', '#7fd88f', '#d85f8f', '#d8cf5f'][index];

    // A sprite is a record: you change it by changing its fields, and the renderer reads
    // them live. There is no setter to go through.
    const sprite = createSprite({
        anchor: {
            x: 0,
            y: 0
        },
        transform: {
            x: 30 + index * 84,
            y: 70
        },
        width: size,
        height: size,
        tint: getColor(colour),
    });

    let dir = index % 2 === 0 ? 1 : -1;

    useUpdate((dt) => {
        sprite.transform.y += speed * dir * dt;

        if (sprite.transform.y > 190) {
            sprite.transform.y = 190;
            dir = -1;
        }

        if (sprite.transform.y < 60) {
            sprite.transform.y = 60;
            dir = 1;
        }
    });

    // The engine does not pause audio and does not guess what else should stop: it SAYS the
    // scene paused, and anything that cares listens. Here it drains the block's colour so you
    // can see it reach every depth of the tree: a block is a piece of the scene, and its
    // useScene() is the scene's. Deliberately a mid grey and not a dark one: a block that dims
    // into the background reads as "it disappeared", which is the opposite of what this example
    // is trying to show.
    const world = useScene();

    world.onPaused(() => {
        sprite.tint = getColor('#8a93a6');
    });
    world.onResumed(() => {
        sprite.tint = getColor(colour);
    });
};
```

`scenes/menu.ts`:

```ts
import { createScene, createSprite, createText, getColor, useKeyboard, useLoadBitmapFont, useScene, useUpdate } from 'nacatamalon';
import * as Settings from '../settings';

export const PauseMenu = () => {
    const font = useLoadBitmapFont(Settings.FONT);
    const scene = useScene();
    const keys = useKeyboard();

    // Drawn over the world, which is still there — the frozen blocks are visible around it.
    // That only works because a paused scene keeps producing its passes.
    const scrim = getColor('#101a2e');

    createSprite({
        anchor: {
            x: 0,
            y: 0
        },
        transform: {
            x: 60,
            y: 90
        },
        width: 360,
        height: 90,
        // Opacity is the tint's own fourth channel: a sprite has no separate alpha.
        tint: {
            ...scrim,
            a: 0.92
        },
    });

    createText({
        text: 'PAUSED',
        font,
        style: {
            fontSize: 16
        },
        tint: '#f2f6fa',
        transform: {
            x: 176,
            y: 112
        }
    });

    const sub = createText({
        text: 'SPACE TO RESUME',
        font,
        style: {
            fontSize: 8
        },
        tint: '#8fa4c4',
        transform: {
            x: 92,
            y: 150
        }
    });

    // The proof: this runs every frame while the world runs none. If the menu were frozen too,
    // the label would sit there dead instead of blinking.
    let blink = 0;

    useUpdate((dt) => {
        blink += dt;
        sub.text = blink % 1 < 0.6 ? 'SPACE TO RESUME' : '';

        if (keys.justPressed(' ')) {
            // Resume the world by NAME, then close this scene. stop() is the other half of
            // launch: without it a HUD could only be taken down by changing the scene that
            // owned it, which took the gameplay with it.
            scene.resume('World');
            scene.stop();
        }
    });

    return createScene();
};
```

`scenes/world.ts`:

```ts
import { createScene, createSprite, createText, useGame, useKeyboard, useLoadBitmapFont, useScene, useSpawn, useUpdate } from 'nacatamalon';
import * as Settings from '../settings';
import { Mover } from '../components/mover';

export const World = () => {
    const font = useLoadBitmapFont(Settings.FONT);
    const scene = useScene();
    const keys = useKeyboard();
    const game = useGame();

    createSprite({
        anchor: {
            x: 0,
            y: 0
        },
        transform: {
            x: 0,
            y: 0
        },
        width: 480,
        height: 270,
        tint: '#0d1424'
    });

    for (let i = 0; i < 5; i++) {
        useSpawn(Mover)(i);
    }

    createText({
        text: 'SPACE: PAUSE   S: SLOW-MO',
        font,
        style: {
            fontSize: 8
        },
        tint: '#8fa4c4',
        transform: {
            x: 16,
            y: 230
        }
    });

    let slow = false;

    useUpdate(() => {
        // Polled, not a pointer subscription: a poll lives in an update callback, so it stops
        // with the scene. A push handler (usePointer().onDown) is not on the frame clock and
        // would still fire at a paused world — which is a fine way to write a bug.
        if (keys.justPressed(' ')) {
            scene.launch('PauseMenu');
            scene.pause();
        }

        // Slow motion is NOT a pause: at timeScale 0 the updates still run, with dt === 0.
        if (keys.justPressed('s')) {
            slow = !slow;
            game.setTimeScale(slow ? 0.15 : 1);
        }
    });

    return createScene();
};
```

`settings.ts`:

```ts
export const WIDTH = 480;
export const HEIGHT = 270;
export const FONT = {
    json: '/example-assets/fonts/bitmaps/nacatamal-arcade/nacatamal-arcade.json',
    atlas: '/example-assets/fonts/bitmaps/nacatamal-arcade/nacatamal-arcade.png',
};
// ————— the world: things that move, so "frozen" is visible —————
```
