# Pixels and pieces

> An orthographic 3D camera measures in the same pixels a sprite does, so a 3D piece can stand on a 2D tile.

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

An orthographic 3D camera **measures in the same pixels a sprite does**: a model one unit wide is one pixel wide. That is what lets a 3D piece be told to stand on a tile instead of being nudged into place by eye.

With `useCamera3d({ follow2d: true })`, the 3D camera follows the 2D camera's movement and zoom. The piece uses the board's coordinates, including downward Y: there is no need to subtract it from the screen height or move both cameras by hand. **Arrow keys** scroll the board, and the piece stays on its tile.

## Code

`index.ts`:

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

// Arrow keys scroll, Z and X zoom. The 3D piece stays on its 2D tile.
const game = createGame('#app', {
    width: WIDTH,
    height: HEIGHT,
    background: '#0a0e1a',
    actions: CAMERA_ACTIONS
});

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

`components/board.ts`:

```ts
import { createSprite } from 'nacatamalon';
import { TILE } from '../settings';

export const Board = () => {
    // The board: flat sprites on a grid, in the game's pixels.
    for (let row = 0; row < 8; row++) {
        for (let col = 0; col < 14; col++) {
            createSprite({
                tint: (row + col) % 2 === 0 ? '#2b3550' : '#212a40',
                width: TILE,
                height: TILE,
                anchor: {
                    x: 0,
                    y: 0
                },
                transform: {
                    x: col * TILE,
                    y: row * TILE
                },
                zIndex: -10,
            });
        }
    }
};
```

`components/piece.ts`:

```ts
import { createMesh, useCubeGeometry, useUpdate } from 'nacatamalon';
import { TILE } from '../settings';

export const Piece = (column: number, row: number) => {
    // The piece: a model, placed by the same numbers the board uses.
    const cube = useCubeGeometry();

    const piece = createMesh({
        geometry: cube,
        tint: '#ffd166',
        transform: {
            scaleX: TILE * 0.7,
            scaleY: TILE * 0.7,
            scaleZ: TILE * 0.7,
            rotationX: -0.5,
            rotationY: 0.6
        },
    });

    // Stand in the middle of its tile, using the same coordinates as a sprite.
    piece.transform.x = column * TILE + TILE / 2;
    piece.transform.y = row * TILE + TILE / 2;

    useUpdate((delta) => {
        piece.transform.rotationY += delta * 0.7;
    });
};
```

`hooks/use_camera_controls.ts`:

```ts
import { useActions, useDirection, useUpdate } from 'nacatamalon';
import { clamp } from 'nacatamalon/math';
import type { TCamera2d } from 'nacatamalon';

export const useCameraControls = (camera: TCamera2d) => {
    const move = useDirection('left', 'right', 'up', 'down');
    const input = useActions();

    useUpdate((delta) => {
        const direction = move();
        const zoom = input.value('zoom_in') - input.value('zoom_out');

        // The engine normalizes diagonals, so moving diagonally is not faster.
        // Only the 2D camera moves; follow2d keeps the 3D camera aligned.
        camera.transform.x += direction.x * 120 * delta;
        camera.transform.y += direction.y * 120 * delta;
        camera.zoom = clamp(camera.zoom * (1 + zoom * delta), 0.5, 3);
    });
};
```

`scenes/level.ts`:

```ts
import { createScene, useCamera2d, useCamera3d, useDirectionalLight, useSpawn } from 'nacatamalon';
import { Board } from '../components/board';
import { Piece } from '../components/piece';
import { useCameraControls } from '../hooks/use_camera_controls';

export const Level = () => {
    const camera = useCamera2d();

    // Both cameras measure the same pixels. Only the 2D one needs to move or zoom.
    // Stand back far enough that the near plane does not cut through the piece.
    useCamera3d({
        projection: 'orthographic',
        follow2d: true,
        transform: {
            z: 200
        }
    });
    useDirectionalLight({
        intensity: 1.2,
        ambient: 0.35
    });

    useSpawn(Board)();
    useSpawn(Piece)(5, 4);
    useCameraControls(camera);

    return createScene();
};
```

`settings.ts`:

```ts
import { INPUT } from 'nacatamalon';
import type { TActionMap } from 'nacatamalon';

export const WIDTH = 480;
export const HEIGHT = 320;
export const TILE = 32;

// Reuse the engine's arrows, WASD, d-pad and left stick; name the zoom controls separately.
export const CAMERA_ACTIONS: TActionMap = [
    ...INPUT.ACTION_PRESETS.platformer.filter((action) => ['left', 'right', 'up', 'down'].includes(action.name)),
    {
        name: 'zoom_in',
        bindings: [
            {
                type: 'key',
                key: 'x'
            },
            {
                type: 'button',
                button: 'rb'
            }
        ]
    },
    {
        name: 'zoom_out',
        bindings: [
            {
                type: 'key',
                key: 'z'
            },
            {
                type: 'button',
                button: 'lb'
            }
        ]
    }
];
```
