# Preload bar

> A loading scene asks for 78 images and tracks them with useLoadProgress, then hands off to a level that uses a few by key.

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

`Loading` asks for 78 images (48 icons and 30 enemy sheets) and hands them to `useLoadProgress`, the one place that knows how the batch is doing. `Level` then asks for the few it shows by key: no paths repeated and no second download.

The bar is made of untextured quads, which is the whole trick: a loading screen cannot depend on the images it is waiting for. Its fill is anchored to its left edge, so growing it does not move it.

## Code

`index.ts`:

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

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

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

`components/gallery.ts`:

```ts
import { createSprite } from 'nacatamalon';
import * as Settings from '../settings';

export const Gallery = () => {
    for (let i = 0; i < Settings.ICONS; i++) {
        createSprite({
            key: 'icon-' + (i + 1),
            width: 40,
            height: 40,
            transform: {
                x: 60 + (i % 8) * 52,
                y: 60 + Math.floor(i / 8) * 42,
                rotation: 0,
                scaleX: 1,
                scaleY: 1
            },
        });
    }
};
```

`components/progressbar.ts`:

```ts
import { createSprite, getColor, useUpdate } from 'nacatamalon';

import type { TLoadProgress } from 'nacatamalon';

export const ProgressBar = ({ loader, onComplete }: { loader: TLoadProgress; onComplete: () => void }) => {
    createSprite({
        width: 248,
        height: 14,
        tint: {
            r: 1,
            g: 1,
            b: 1,
            a: 0.18
        },
        transform: {
            x: 240,
            y: 170,
            rotation: 0,
            scaleX: 1,
            scaleY: 1
        },
    });

    // Anchored to its left edge, so growing it does not move it: without an anchor the bar would
    // have to be pushed right by half of everything it gains.
    const fill = createSprite({
        width: 0,
        height: 10,
        tint: '#4ade80',
        anchor: {
            x: 0,
            y: 0.5
        },
        transform: {
            x: 118,
            y: 170,
            rotation: 0,
            scaleX: 1,
            scaleY: 1
        },
    });

    // Five ticks that light up as the batch advances: the closest thing to a percentage until
    // there is text.
    const ticks = [0, 1, 2, 3, 4].map((i) => createSprite({
        width: 8,
        height: 8,
        tint: {
            r: 1,
            g: 1,
            b: 1,
            a: 0.2
        },
        transform: {
            x: 118 + i * 60,
            y: 196,
            rotation: 0,
            scaleX: 1,
            scaleY: 1
        },
    }));

    let done = 0;

    useUpdate((delta) => {
        fill.width = loader.progress * 244;
        ticks.forEach((tick, i) => {
            tick.tint = loader.progress >= (i + 1) / 5 ? getColor('#4ade80') : {
                r: 1,
                g: 1,
                b: 1,
                a: 0.2
            };
        });

        if (!loader.done) {
            return;
        }

        // Held full for a moment, or the batch finishing would look like a glitch.
        done += delta;

        if (done > 0.6) {
            onComplete();
        }
    });
};
```

`hooks/use_preload_assets.ts`:

```ts
import { useLoadTexture } from 'nacatamalon';
import * as Settings from '../settings';

export const usePreloadAssets = () => {
    const icons = Array.from({
        length: Settings.ICONS
    }, (_, i) =>
        useLoadTexture({
            src: '/example-assets/assets/mushroom/Icon' + (i + 1) + '.png',
            key: 'icon-' + (i + 1)
        }));

    const sheets = [];

    for (let enemy = 1; enemy <= 6; enemy++) {
        for (const action of Settings.ENEMY_ACTIONS) {
            sheets.push(useLoadTexture({
                src: '/example-assets/assets/charracters/business_enemies/' + enemy + '/' + action + '.png'
            }));
        }
    }

    return [...icons, ...sheets];
};
```

`scenes/level.ts`:

```ts
import { createScene, useSpawn } from 'nacatamalon';
import { Gallery } from '../components/gallery';

export const Level = () => {
    useSpawn(Gallery)();

    return createScene();
};
```

`scenes/loading.ts`:

```ts
import { createScene, useLoadProgress, useScene, useSpawn } from 'nacatamalon';
import { usePreloadAssets } from '../hooks/use_preload_assets';
import { ProgressBar } from '../components/progressbar';

export const Loading = () => {
    const scene = useScene();
    const loader = useLoadProgress(usePreloadAssets());

    useSpawn(ProgressBar)({
        loader,
        onComplete: () => scene.change('Level')
    });

    return createScene();
};
```

`settings.ts`:

```ts
export const WIDTH = 480;
export const HEIGHT = 320;
export const ICONS = 48;
export const ENEMY_ACTIONS = ['Attack', 'Death', 'Hurt', 'Idle', 'Walk'];
```
