# Arcade attract screen

> Two .ttf fonts under bloom, an NTSC cable, a CRT tube and a glitch on demand.

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

Two `.ttf` fonts loaded with `useLoadFont`: Bangers for the bouncing logo, Press Start 2P for the score table and the ticker, accents and ñ included. On top, a stack of post effects: `bloom`, an optional `ntsc` cable, `crt` and a `glitch` on demand.

The effects know nothing about the text; the letters reach them already sharp, at the screen's own resolution thanks to `pixelRatio: 'device'`.

**P** changes the tube, **G** glitches, **SPACE** turns the tube off and on.

## Code

`index.ts`:

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

import * as Settings from './settings';
import { Attract } from './scenes/demo';

const game = createGame('#app', {
    width: Settings.WIDTH,
    height: Settings.HEIGHT,
    // The screen's own resolution under the effects: the tube's mask and scanlines need the
    // pixels, and the vector letters are drawn sharp at it.
    pixelRatio: 'device',
    background: '#0b0a1a',
});

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

`components/display.ts`:

```ts
import { createText, getColor, useUpdate, measureText, useWatch } from 'nacatamalon';

import * as Settings from '../settings';

import type { TFont } from 'nacatamalon';

export const Display = (title: TFont, ui: TFont) => {
    // A vector title can be scaled and turned and stays sharp, so it can bounce like a sticker.
    const logo = createText({
        text: '¡NACATAMAL!',
        font: title,
        style: {
            fontSize: 44
        },
        tint: '#ffcc33',
        anchor: Settings.CENTRED,
        transform: {
            x: Settings.WIDTH / 2,
            y: 40
        },
    });

    const shadow = createText({
        text: '¡NACATAMAL!',
        font: title,
        style: {
            fontSize: 44
        },
        tint: '#c0306a',
        anchor: Settings.CENTRED,
        transform: {
            x: Settings.WIDTH / 2 + 3,
            y: 43
        },
        zIndex: -1,
    });

    createText({
        text: 'HIGH SCORES',
        font: ui,
        style: {
            fontSize: 8
        },
        tint: '#ff5a5a',
        anchor: {
            x: 0.5,
            y: 0
        },
        transform: {
            x: Settings.WIDTH / 2,
            y: 78
        }
    });
    Settings.SCORES.forEach(([rank, name, score], i) => {
        const line = `${rank}  ${name.padEnd(6, ' ')} ${String(score).padStart(6, '0')}`;

        createText({
            text: line,
            font: ui,
            style: {
                fontSize: 8
            },
            tint: getColor(Settings.RANK_COLORS[i]),
            transform: {
                x: 72,
                y: 96 + i * 14
            }
        });
    });

    const coin = createText({
        text: 'INSERT COIN',
        font: ui,
        style: {
            fontSize: 8
        },
        tint: '#ffffff',
        anchor: {
            x: 0.5,
            y: 0
        },
        transform: {
            x: Settings.WIDTH / 2,
            y: 176
        }
    });

    const ticker = createText({
        text: '★ ACENTOS, Ñ Y ¿PREGUNTAS? SIN UNA LISTA DE CARACTERES ★ NÍTIDO A CUALQUIER TAMAÑO ★',
        font: ui,
        style: {
            fontSize: 8
        },
        tint: '#8ee6a0',
        transform: {
            x: Settings.WIDTH,
            y: 196
        },
    });

    const tubeName = createText({
        text: '',
        font: ui,
        style: {
            fontSize: 8
        },
        tint: '#8c82a6',
        anchor: {
            x: 0.5,
            y: 0
        },
        transform: {
            x: Settings.WIDTH / 2,
            y: 212
        }
    });

    createText({
        text: 'P TUBE  G GLITCH  SPACE POWER',
        font: ui,
        style: {
            fontSize: 8
        },
        tint: '#5a5470',
        anchor: {
            x: 0.5,
            y: 0
        },
        transform: {
            x: Settings.WIDTH / 2,
            y: 226
        }
    });

    let tickerWidth = 0;

    useWatch(() => {
        if (ui.status === 'ready') {
            tickerWidth = measureText(ticker).width;
        }
    }, [ui]);
    useUpdate((delta, time) => {
        const bounce = 1 + 0.08 * Math.abs(Math.sin(time * 3));

        for (const text of [logo, shadow]) {
            text.transform.scaleX = bounce;
            text.transform.scaleY = bounce;
            text.transform.rotation = 0.06 * Math.sin(time * 1.7);
        }

        coin.visible = Math.floor(time * 2) % 2 === 0;

        ticker.transform.x -= delta * 40;

        // Its block's width is known once laid out, so it loops right after leaving the screen.
        if (tickerWidth > 0 && ticker.transform.x < -tickerWidth) {
            ticker.transform.x = Settings.WIDTH;
        }
    });

    return tubeName;
};
```

`scenes/demo.ts`:

```ts
import { createScene, useKeyboard, useLoadFont, useUpdate, useSpawn } from 'nacatamalon';
import { bloom, crt, glitch, ntsc, usePostProcess } from 'nacatamalon/effects';
import * as Settings from '../settings';

import { Display } from '../components/display';

export const Attract = () => {
    const title = useLoadFont(Settings.TITLE_FONT);
    const ui = useLoadFont(Settings.UI_FONT);

    const tubeName = useSpawn(Display)(title, ui).api;

    // The look, in this order: the glow, the cable, the tube, and a glitch kept for G.
    usePostProcess(bloom({
        threshold: 0.6,
        intensity: 0.6
    }));

    const cable = usePostProcess({
        ...ntsc(),
        enabled: false
    });

    const tube = usePostProcess(crt(Settings.TUBES[0].preset));

    const burst = usePostProcess({
        ...glitch({
            amount: 0.6
        }),
        enabled: false
    });

    let current = 0;
    let glitchUntil = 0;

    const show = () => {
        tubeName.text = tube.enabled ? Settings.TUBES[current].name : 'NO TUBE';
    };

    show();

    const keys = useKeyboard();

    useUpdate((_delta, time) => {
        if (keys.justPressed('p')) {
            current = (current + 1) % Settings.TUBES.length;
            Object.assign(tube.uniforms, crt(Settings.TUBES[current].preset).uniforms);
            tube.enabled = true;
            cable.enabled = Settings.TUBES[current].cable;
            show();
        }

        if (keys.justPressed(' ')) {
            tube.enabled = !tube.enabled;
            cable.enabled = tube.enabled && Settings.TUBES[current].cable;
            show();
        }

        if (keys.justPressed('g')) {
            glitchUntil = time + 0.5;
        }

        burst.enabled = time < glitchUntil;
    });

    return createScene();
};
```

`settings.ts`:

```ts
import { POST_EFFECTS } from 'nacatamalon/effects';

export const WIDTH = 320;
export const HEIGHT = 240;
export const TITLE_FONT = {
    key: 'title',
    src: '/example-assets/fonts/vector/Bangers-Regular.ttf'
};
export const UI_FONT = {
    key: 'ui',
    src: '/example-assets/fonts/vector/PressStart2P-Regular.ttf'
};

export const TUBES = [
    {
        name: 'ARCADE CABINET',
        preset: POST_EFFECTS.CRT_PRESETS.arcade,
        cable: false
    },
    {
        name: 'CONSUMER TV',
        preset: POST_EFFECTS.CRT_PRESETS.consumer,
        cable: false
    },
    {
        name: 'STUDIO MONITOR',
        preset: POST_EFFECTS.CRT_PRESETS.pvm,
        cable: false
    },
    {
        name: 'COMPOSITE CABLE',
        preset: POST_EFFECTS.CRT_PRESETS.composite,
        cable: true
    },
];

export const SCORES: Array<[string, string, number]> = [['1ST', 'ANA', 98420], ['2ND', 'ÑOÑO', 75100], ['3RD', 'JOSÉ', 61250], ['4TH', 'GAMMA', 40000], ['5TH', 'PACO', 12345]];
export const RANK_COLORS = ['#ffd75a', '#d8d8e8', '#e09060', '#8ee6a0', '#7ad7ff'];

export const CENTRED = {
    x: 0.5,
    y: 0.5
};
```
