# Arcade high scores

> The classic high-score table, with name entry from the keyboard.

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

A high-score table and a name entry, all text. Titles are centred and scores right-aligned with `anchor`, so nobody has to measure a string.

Typing uses `justPressed`, true for one frame per press, and the name field is a text whose `.text` is rewritten every frame with a blinking cursor. The hint line is written in lowercase and drawn with the font's capitals.

## Code

`index.ts`:

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

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

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

`components/name_entry.ts`:

```ts
import { createText, useKeyboard, useLoadBitmapFont, useUpdate } from 'nacatamalon';
import { ARCADE, LETTERS, MAX_NAME_LENGTH } from '../settings';

export const NameEntry = () => {
    const font = useLoadBitmapFont(ARCADE);
    const keys = useKeyboard();

    createText({
        text: 'ENTER YOUR NAME',
        font,
        style: {
            fontSize: 16
        },
        transform: {
            x: 16,
            y: 168
        }
    });

    const input = createText({
        text: '_',
        font,
        style: {
            fontSize: 24
        },
        tint: '#4cff91',
        transform: {
            x: 16,
            y: 198
        }
    });

    createText({
        text: 'Click the canvas, then type A to Z',
        font,
        tint: '#35557a',
        transform: {
            x: 16,
            y: 278
        }
    });

    let name = '';

    useUpdate((_delta, time) => {
        // justPressed is true for one frame per press, so holding a key types it once.
        for (const letter of LETTERS) {
            if (keys.justPressed(letter) && name.length < MAX_NAME_LENGTH) {
                name += letter.toUpperCase();
            }
        }

        if (keys.justPressed('Backspace')) {
            name = name.slice(0, -1);
        }

        // The cursor blinks twice a second.
        const cursor = Math.sin(time * Math.PI * 2) >= 0 ? '_' : ' ';

        input.text = name + cursor;
    });
};
```

`components/scoreboard.ts`:

```ts
import { createText, createSprite, getColor, useLoadBitmapFont } from 'nacatamalon';
import { ARCADE, WIDTH } from '../settings';
import { SCORES } from '../data/scores';

export const Scoreboard = () => {
    const font = useLoadBitmapFont(ARCADE);

    // Centred on the middle of the screen by its anchor, whatever its width turns out to be.
    createText({
        text: 'HALL OF FAME',
        font,
        style: {
            fontSize: 16
        },
        tint: '#f5c518',
        anchor: {
            x: 0.5,
            y: 0
        },
        transform: {
            x: WIDTH / 2,
            y: 12
        }
    });

    createSprite({
        tint: '#1e3a5f',
        width: WIDTH - 24,
        height: 1,
        transform: {
            x: WIDTH / 2,
            y: 36
        }
    });

    SCORES.forEach(({ name, pts }, i) => {
        const y = 44 + i * 22;
        const isYou = i === SCORES.length - 1;
        const accent = getColor(isYou ? '#4cff91' : '#ffffff');
        const dim = getColor(isYou ? '#4cff91' : '#667799');

        createText({
            text: String(i + 1),
            font,
            tint: dim,
            transform: {
                x: 16,
                y
            }
        });
        createText({
            text: name,
            font,
            tint: accent,
            transform: {
                x: 40,
                y
            }
        });
        // Right-aligned by its anchor: the scores line up on their last digit.
        createText({
            text: String(pts),
            font,
            tint: getColor(isYou ? '#4cff91' : '#ff6b6b'),
            anchor: {
                x: 1,
                y: 0
            },
            transform: {
                x: WIDTH - 16,
                y
            }
        });
    });

    createSprite({
        tint: '#1e3a5f',
        width: WIDTH - 24,
        height: 1,
        transform: {
            x: WIDTH / 2,
            y: 158
        }
    });
};
```

`data/scores.ts`:

```ts
export const SCORES = [
    {
        name: 'LINK',
        pts: 99900
    },
    {
        name: 'SAMUS',
        pts: 84700
    },
    {
        name: 'MARIO',
        pts: 72500
    },
    {
        name: 'SONIC',
        pts: 61200
    },
    {
        name: '???',
        pts: 50000
    },   // that is you
];
```

`scenes/demo.ts`:

```ts
import { createScene, useSpawn } from 'nacatamalon';
import { Scoreboard } from '../components/scoreboard';
import { NameEntry } from '../components/name_entry';

export const Scene = () => {
    useSpawn(Scoreboard)();
    useSpawn(NameEntry)();

    return createScene();
};
```

`settings.ts`:

```ts
export const WIDTH = 480;
export const HEIGHT = 300;
export const LETTERS = 'abcdefghijklmnopqrstuvwxyz';
export const MAX_NAME_LENGTH = 8;
export const ARCADE = {
    json: '/example-assets/fonts/bitmaps/nacatamal-arcade/nacatamal-arcade.json',
    atlas: '/example-assets/fonts/bitmaps/nacatamal-arcade/nacatamal-arcade.png'
};
```
