# Camera and HUD

> A 2D camera scrolls the world while useScreenSpace nails the HUD to the screen: one scene, two spaces.

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

One scene, two spaces. A trailing camera chases the player, so the player swings across the screen while the grid scrolls.

The HUD calls `useScreenSpace()`, which pins it and everything created under it to the screen: the bar and the badge stay nailed to the canvas however the camera moves, and one call covers all of them. The other way to get a fixed HUD is a scene of its own started with `useScene().launch()`.

## Code

`index.ts`:

```ts
import { createGame } from 'nacatamalon';
import { Level } from './scenes/demo';
import { WIDTH, HEIGHT } from './settings';

const game = createGame('#app', {
    width: WIDTH,
    height: HEIGHT,
    background: '#0c1018'
});

// Declare Scenes
game({
    Level
});
```

`components/hud.ts`:

```ts
import { createSprite, useScreenSpace, useUpdate } from 'nacatamalon';
import { TOP_LEFT, WIDTH, HEIGHT } from '../settings';

export const Hud = () => {
    // One call covers the three sprites below, and would still cover them if they were created
    // several objects deeper.
    useScreenSpace();

    createSprite({
        tint: 'rgba(255, 255, 255, 0.3)',
        anchor: TOP_LEFT,
        width: 184,
        height: 16,
        zIndex: 100,
        transform: {
            x: 12,
            y: 12,
            rotation: 0,
            scaleX: 1,
            scaleY: 1
        },
    });

    // Anchored on its left edge, so it grows to the right without moving.
    const fill = createSprite({
        tint: 'rgba(74, 222, 128, 0.9)',
        anchor: TOP_LEFT,
        width: 60,
        height: 12,
        zIndex: 101,
        transform: {
            x: 14,
            y: 14,
            rotation: 0,
            scaleX: 1,
            scaleY: 1
        },
    });

    createSprite({
        tint: 'rgba(255, 204, 51, 0.6)',
        anchor: TOP_LEFT,
        width: 38,
        height: 18,
        zIndex: 100,
        transform: {
            x: WIDTH - 50,
            y: HEIGHT - 30,
            rotation: 0,
            scaleX: 1,
            scaleY: 1
        },
    });

    // The bar keeps moving, to show the HUD is alive and not a frozen picture: it changes, it just
    // never drifts with the world.
    useUpdate((_delta, time) => {
        fill.width = 20 + (Math.sin(time * 2) * 0.5 + 0.5) * 160;
    });
};
```

`components/player.ts`:

```ts
import { createSprite, useLoadTexture, useUpdate } from 'nacatamalon';
import { PLAYER, TEXTURE } from '../settings';

export const Player = () => {
    const player = createSprite({
        texture: useLoadTexture(TEXTURE),
        width: PLAYER.size,
        height: PLAYER.size,
        transform: {
            x: PLAYER.x,
            y: PLAYER.y
        }
    });

    useUpdate((delta, time) => {
        player.transform.x = PLAYER.x + Math.cos(time * PLAYER.speedX) * PLAYER.radiusX;
        player.transform.y = PLAYER.y + Math.sin(time * PLAYER.speedY) * PLAYER.radiusY;
        player.transform.rotation += delta * PLAYER.rotationSpeed;
    });

    return player;
};
```

`components/world.ts`:

```ts
import { createSprite, getColor } from 'nacatamalon';
import { GRID } from '../settings';

export const World = () => {
    const marker = getColor(`rgba(255, 255, 255, ${GRID.opacity})`);

    for (let gy = 0; gy < GRID.rows; gy++) {
        for (let gx = 0; gx < GRID.columns; gx++) {
            createSprite({
                tint: marker,
                width: GRID.size,
                height: GRID.size,
                transform: {
                    x: gx * GRID.spacing,
                    y: gy * GRID.spacing,
                    rotation: 0,
                    scaleX: 1,
                    scaleY: 1
                },
            });
        }
    }
};
```

`scenes/demo.ts`:

```ts
import { createScene, useCamera2d, useCameraFollow, useSpawn } from 'nacatamalon';
import { World } from '../components/world';
import { Player } from '../components/player';
import { FOLLOW } from '../settings';
import { Hud } from '../components/hud';

export const Level = () => {
    useCamera2d();
    useSpawn(World)();

    const player = useSpawn(Player)();

    // The scene owns its camera; the player owns its movement.
    useCameraFollow(player.api, FOLLOW);
    useSpawn(Hud)();

    return createScene();
};
```

`settings.ts`:

```ts
export const WIDTH = 480;
export const HEIGHT = 320;
export const GRID = {
    rows: 11,
    columns: 18,
    spacing: 80,
    size: 22,
    opacity: 0.2
};
export const PLAYER = {
    x: 640,
    y: 400,
    radiusX: 320,
    radiusY: 220,
    speedX: 1.1,
    speedY: 1.6,
    size: 56,
    rotationSpeed: 2
};
export const FOLLOW = {
    smooth: 0.5,
    round: false
};
export const TEXTURE = {
    src: '/example-assets/assets/gammix.png',
    key: 'gammix'
};
export const TOP_LEFT = {
    x: 0,
    y: 0
};
```
