# Tilemap

> A whole level from one .tilemap file: layers either side of the characters, one draw call each, tiles that cycle and tiles that break.

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

A whole level from one `.tilemap` file: its layers, what its numbers mean, and which sheet they come from. Each layer is **one draw call**, whatever its number of cells.

The walker climbs what `solidAt` says is solid, and never mentions a draw order: the file says `'under'` and `'over'`, so the ground is below it and the treetops above. **Click a plank** to break it, which is writing a number into a cell; the torches cycle a sequence declared in the `.atlas`, the same kind a sprite animation plays.

## Code

`index.ts`:

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

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

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

`components/walker.ts`:

```ts
import { cellAt, createSprite, solidAt, useKeyboard, useUpdate } from 'nacatamalon';

import type { TTilemap } from 'nacatamalon';

export const Walker = ({ map }: { map: TTilemap }) => {
    // Look at what this sprite does NOT say: an order.
    //
    // 'under' and 'over' are the bands either side of zero, and zero is where everything starts.
    // So the ground goes below and the treetops above without this scene and that file ever
    // having agreed on a number, which is the point: a level is drawn without knowing what
    // numbers a game gives its sprites.
    const walker = createSprite({
        tint: '#ffd166',
        width: 20,
        height: 28,
        transform: {
            x: 40,
            y: 150,
            rotation: 0,
            scaleX: 1,
            scaleY: 1
        },
    });

    const keys = useKeyboard();

    // Walking along the ground: step forward, and climb when the next cell is solid. That is the
    // whole of it. solidAt asks about the PLACE, across every layer, because a wall is a wall
    // whichever layer painted it.
    useUpdate((delta) => {
        if (map.status !== 'ready') {
            return;
        }

        const speed = keys.isDown('ArrowLeft') ? -70 : 70;

        walker.transform.x += speed * delta;

        if (walker.transform.x > map.width * map.cell) {
            walker.transform.x = -16;
        }

        if (walker.transform.x < -16) {
            walker.transform.x = map.width * map.cell;
        }

        // The first solid cell under it, from the top: the floor it is standing on.
        const { column } = cellAt(map, walker.transform.x, 0);
        let row = 0;

        while (row < map.height && !solidAt(map, column, row)) {
            row++;
        }

        walker.transform.y = row * map.cell - 14;
    });
};
```

`scenes/demo.ts`:

```ts
import { cellAt, createScene, createTilemap, setTile, tileAt, usePointer, useSpawn } from 'nacatamalon';
import * as Settings from '../settings';

import { Walker } from '../components/walker';

export const Level = () => {
    const map = createTilemap({
        src: Settings.MAP
    });

    useSpawn(Walker)({
        map
    });
    // Click a plank to break it. Tile 4 says "becomes": 0, so what it turns into lives in the map
    // and not in this code, and a tile that says nothing about becoming cannot be broken at all.
    usePointer().onDown((info) => {
        if (map.status !== 'ready') {
            return;
        }

        const { column, row } = cellAt(map, info.worldX, info.worldY);
        const tile = map.tiles[tileAt(map, 0, column, row)];

        if (tile?.becomes !== undefined) {
            setTile(map, 0, column, row, tile.becomes);
        }
    });

    return createScene();
};
```

`settings.ts`:

```ts
export const WIDTH = 480;
export const HEIGHT = 320;
export const MAP = '/example-assets/assets/maps/jungle.tilemap';
```
