# Shared data

> One useData handed to three parts of the scene: a HUD, a bar and the generator that changes it. Each watches it on its own.

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

One `useData`, made where it belongs and handed to three parts of the scene. The `Hud` shows it as a number, the `Bar` shows the same number as a width, and the `Generator` is the only one that changes it. Each watches on its own and none of them knows the others. Press **←** and **→**. A signal says something happened; this is something that stays true, so a part that arrives later still reads the value it missed.

**And where a store would go instead.** A store belongs to the whole game: it outlives a scene change and can be saved to the browser, which is right for coins and settings and wrong for this ship's shield. `useData` belongs to the object, is saved with its scene and goes away with it. The other difference shows in a pause menu: `useStore` and `useSignal` keep hearing while a scene is paused, and a watch does not, because it runs with the updates.

## Code

`index.ts`:

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

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

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

`components/bar.ts`:

```ts
import type { TDataRecord } from 'nacatamalon';
import { FULL } from '../settings';
import { createSprite, getColor, useWatch } from 'nacatamalon';

export const Bar = ({ shield }: { shield: TDataRecord<number> }) => {
    const back = createSprite({
        tint: '#2a2a3c',
        width: FULL,
        height: 18,
        anchor: {
            x: 0,
            y: 0.5
        },
        transform: {
            x: 24,
            y: 80,
            rotation: 0,
            scaleX: 1,
            scaleY: 1
        },
    });

    const fill = createSprite({
        tint: '#5bd75b',
        width: FULL,
        height: 18,
        anchor: {
            x: 0,
            y: 0.5
        },
        transform: {
            x: 24,
            y: 80,
            rotation: 0,
            scaleX: 1,
            scaleY: 1
        },
    });

    useWatch(() => {
        const part = Math.max(0, Math.min(1, shield.value / 100));

        fill.width = FULL * part;
        fill.visible = part > 0;
        fill.tint = part > 0.3 ? getColor('#5bd75b') : getColor('#e5533b');
        back.visible = true;
    }, [shield]);
};
```

`components/generator.ts`:

```ts
import type { TDataSetter, TBitmapFont } from 'nacatamalon';
import { createText, useKeyboard, useUpdate } from 'nacatamalon';

export const Generator = ({ setShield, font }: { setShield: TDataSetter<number>; font: TBitmapFont }) => {
    const keys = useKeyboard();

    createText({
        text: 'LEFT/RIGHT: DRAIN AND CHARGE',
        font,
        style: {
            fontSize: 8
        },
        tint: '#8fa4c4',
        transform: {
            x: 24,
            y: 150,
            rotation: 0,
            scaleX: 1,
            scaleY: 1
        },
    });

    useUpdate(() => {
        if (keys.justPressed('ArrowLeft')) {
            setShield((n) => Math.max(0, n - 20));
        }

        if (keys.justPressed('ArrowRight')) {
            setShield((n) => Math.min(100, n + 20));
        }
    });
};
```

`components/hud.ts`:

```ts
import type { TDataRecord, TBitmapFont } from 'nacatamalon';
import { createText, useWatch } from 'nacatamalon';

export const Hud = ({ shield, font }: { shield: TDataRecord<number>; font: TBitmapFont }) => {
    const label = createText({
        text: '',
        font,
        style: {
            fontSize: 16
        },
        tint: '#f2f6fa',
        transform: {
            x: 24,
            y: 24,
            rotation: 0,
            scaleX: 1,
            scaleY: 1
        },
    });

    useWatch(() => {
        label.text = 'SHIELD ' + shield.value;
    }, [shield]);
};
```

`scenes/demo.ts`:

```ts
import { createScene, useData, useLoadBitmapFont, useSpawn } from 'nacatamalon';
import * as Settings from '../settings';

import { Hud } from '../components/hud';
import { Bar } from '../components/bar';
import { Generator } from '../components/generator';

export const Ship = () => {
    const font = useLoadBitmapFont(Settings.FONT);

    // Made here, where it belongs to the whole ship, and handed down. The three below each watch
    // it on their own and none of them knows the others exist.
    const [shield, setShield] = useData(100);

    useSpawn(Hud)({
        shield,
        font
    });
    useSpawn(Bar)({
        shield
    });
    useSpawn(Generator)({
        setShield,
        font
    });

    return createScene();
};
```

`settings.ts`:

```ts
export const WIDTH = 340;
export const HEIGHT = 180;
export const FONT = {
    json: '/example-assets/fonts/bitmaps/nacatamal-arcade/nacatamal-arcade.json',
    atlas: '/example-assets/fonts/bitmaps/nacatamal-arcade/nacatamal-arcade.png',
};

export const FULL = 200;
```
