# Signals

> A named event channel: a button emits with createGameSignal and a far-apart screen reacts with useSignal, with no reference between them.

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

A named event channel. State is something that stays true; a signal is something that happened. Two parts of the game talk through it without knowing each other: the `Shell` (the button) calls `petPoked.emit()`, and the `Screen` (where the pet lives) reacts with `useSignal(petPoked, …)`, so the pet flashes. Press **P** to poke.

`useSignal` stops listening by itself when its part of the scene goes away, so a listener is never left behind.

## Code

`index.ts`:

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

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

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

`components/screen.ts`:

```ts
import { createSprite, getColor, useSignal, useUpdate } from 'nacatamalon';
import { petPoked } from '../signals';

export const Screen = () => {
    const calm = getColor('#5bd75b');
    const flash = getColor('#fff27a');

    const pet = createSprite({
        tint: calm,
        width: 84,
        height: 84,
        transform: {
            x: 96,
            y: 110,
            rotation: 0,
            scaleX: 1,
            scaleY: 1
        },
    });

    let flashLeft = 0;

    // Reacts to a button it never references. useSignal stops listening by itself when this
    // part of the scene goes away, so nothing is left connected to a pet that is gone.
    useSignal(petPoked, () => {
        console.log('[signal] poked');
        flashLeft = 0.25;
    });

    useUpdate((delta) => {
        if (flashLeft <= 0) {
            return;
        }

        flashLeft = Math.max(0, flashLeft - delta);
        pet.tint = flashLeft > 0 ? flash : calm;
    });
};
```

`components/shell.ts`:

```ts
import { createSprite, useKeyboard, useTween, useUpdate } from 'nacatamalon';
import { petPoked } from '../signals';

export const Shell = () => {
    const keys = useKeyboard();
    const tween = useTween();

    const button = createSprite({
        tint: '#e5533b',
        width: 64,
        height: 64,
        transform: {
            x: 250,
            y: 110,
            rotation: 0,
            scaleX: 1,
            scaleY: 1
        },
    });

    // The button sinks and comes back: there and back once. Declared here, played on every poke.
    const press = tween({
        target: button,
        from: {
            transform: {
                y: 110
            }
        },
        to: {
            transform: {
                y: 124
            }
        },
        duration: 0.07,
        yoyo: true,
        repeat: 1,
    });

    useUpdate(() => {
        // justPressed is true for one frame per press, so holding P pokes once.
        if (!keys.justPressed('p')) {
            return;
        }

        press.play();

        // Said to nobody in particular. Whoever listens reacts before this line returns.
        petPoked.emit();
    });
};
```

`scenes/demo.ts`:

```ts
import { createScene, useSpawn } from 'nacatamalon';

import { Shell } from '../components/shell';
import { Screen } from '../components/screen';

export const PetToy = () => {
    useSpawn(Shell)();
    useSpawn(Screen)();

    return createScene();
};
```

`settings.ts`:

```ts
export const WIDTH = 340;
export const HEIGHT = 220;
```

`signals.ts`:

```ts
import { createGameSignal } from 'nacatamalon';

// A poke carries nothing, so the signal has no type: createGameSignal() is enough.
export const petPoked = createGameSignal();
```
