# Camera follow

> A 2D camera tracking a moving target by moving its position every frame.

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

`useCamera2d()` gives the scene a camera, and from then on every position is a place in a world that can be bigger than the screen. The camera's `x` and `y` are the world point at the screen's top-left corner.

So following the player means subtracting half the screen: the player stays in the middle while the marker grid scrolls underneath.

## 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/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';

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

    const player = useSpawn(Player)();

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

    return createScene();
};
```

`settings.ts`:

```ts
export const WIDTH = 480;
export const HEIGHT = 320;
export const GRID = {
    rows: 9,
    columns: 16,
    spacing: 80,
    size: 24,
    opacity: 0.22
};
export const PLAYER = {
    x: 600,
    y: 300,
    radiusX: 360,
    radiusY: 200,
    speedX: 0.6,
    speedY: 0.9,
    size: 48,
    rotationSpeed: 0
};
export const FOLLOW = {
    smooth: 0,
    round: false
};
export const TEXTURE = {
    src: '/example-assets/assets/gammix.png',
    key: 'gammix'
};
export const TOP_LEFT = {
    x: 0,
    y: 0
};
```
