# Atlas as a file

> An atlas authored as an .atlas file: the texture, how it is sliced and its named sequences, in one resource.

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

`useLoadAtlas` reads an `.atlas` file: which texture, how it is sliced, and the named **sequences** its frames form. Unlike `createSpriteAtlas`, it needs no texture loaded beforehand: the file names its own image, so a single scene can ask for an atlas and use it at once.

Because the slicing is a file, a scene can save "frame 12 of this atlas" instead of two opaque pairs of coordinates, and one sequence serves both a sprite animation and an animated tile.

## Code

`index.ts`:

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

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

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

`components/chest.ts`:

```ts
import { createSprite, useSpriteAnimation, useUpdate } from 'nacatamalon';
import type { TLoadedAtlas } from 'nacatamalon';
import { WIDTH, CHEST } from '../settings';

export const Chest = (atlas: TLoadedAtlas) => {
    const sprite = createSprite({
        atlas,
        width: CHEST.size,
        height: CHEST.size,
        transform: {
            x: WIDTH / 2,
            y: 165
        }
    });

    const animation = useSpriteAnimation(sprite, {
        clips: atlas.sequences,
        play: 'open'
    });

    let waited = 0;

    // This timer is time since the clip stopped, rather than the scene's total time.
    useUpdate((delta) => {
        if (animation.playing) {
            return;
        }

        waited += delta;

        if (waited > CHEST.waitSeconds) {
            waited = 0;
            animation.play('open');
        }
    });
};
```

`components/coin.ts`:

```ts
import { createSprite, useSpriteAnimation } from 'nacatamalon';
import type { TLoadedAtlas } from 'nacatamalon';
import { COINS } from '../settings';

type TCoinProps = {
    atlas: TLoadedAtlas;
    index: number;
};

export const Coin = ({ atlas, index }: TCoinProps) => {
    const sprite = createSprite({
        atlas,
        width: COINS.size,
        height: COINS.size,
        transform: {
            x: COINS.startX + index * COINS.gap,
            y: COINS.y
        }
    });

    // The artist's file supplies the clips; this instance only chooses playback and speed.
    useSpriteAnimation(sprite, {
        clips: atlas.sequences,
        play: 'spin',
        speed: COINS.speed + index * COINS.speedStep
    });
};
```

`scenes/demo.ts`:

```ts
import { createScene, createSprite, useLoadAtlas, useSpawn } from 'nacatamalon';
import { Chest } from '../components/chest';
import { Coin } from '../components/coin';
import { COINS, TILE_FRAMES } from '../settings';

export const Showcase = () => {
    // Each atlas file names its texture, slicing and animation sequences.
    const coin = useLoadAtlas({
        src: '/example-assets/assets/atlases/coin.atlas'
    });

    const chest = useLoadAtlas({
        src: '/example-assets/assets/atlases/chest.atlas'
    });

    const tiles = useLoadAtlas({
        src: '/example-assets/assets/atlases/jungle_tiles.atlas'
    });

    const spawnCoin = useSpawn(Coin);

    for (let index = 0; index < COINS.count; index++) {
        spawnCoin({
            atlas: coin,
            index
        });
    }

    useSpawn(Chest)(chest);

    // An atlas without sequences also provides individual tiles by frame number.
    TILE_FRAMES.forEach((frame, index) => {
        createSprite({
            atlas: tiles,
            frame,
            width: 64,
            height: 64,
            transform: {
                x: 88 + index * 64,
                y: 270
            }
        });
    });

    return createScene();
};
```

`settings.ts`:

```ts
export const WIDTH = 480;
export const HEIGHT = 320;

export const COINS = {
    count: 5,
    size: 48,
    startX: 90,
    gap: 60,
    y: 70,
    speed: 0.6,
    speedStep: 0.2
};

export const CHEST = {
    size: 96,
    waitSeconds: 1
};

export const TILE_FRAMES = [40, 41, 41, 41, 42];
```
