# Fireworks

> Rockets that burst into palms of sparks that crackle as they die: three .particles files setting each other off.

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

**Click** and a rocket bursts where you clicked; **space** is the finale. When nobody clicks, the show goes on by itself.

The explosions are not code. They are three `.particles` files that set each other off with `children`: the rocket's death sets off a burst, and every spark of the burst sets off a crackle as it dies, all of them with trails.

Each colour is a launcher's tint, and `overrides.lifeScale` decides how high a rocket climbs: it flies with no gravity at a fixed speed and life, so its scaled life is exactly its height.

## Code

`index.ts`:

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

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

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

`art/city.ts`:

```ts
// The skyline, painted once: a night sky with stars, and buildings with a few lit windows. The
// sky is see-through at the top so the bursts are drawn over the game's background colour, and
// the buildings cover the rockets as they leave the ground.

export const drawCity = (width: number, height: number) => {
    let seed = 5;
    const rand = () => ((seed = (seed * 16807) % 2147483647) / 2147483647);

    const canvas = document.createElement('canvas');

    canvas.width = width;
    canvas.height = height;

    const g = canvas.getContext('2d')!;

    const glow = g.createLinearGradient(0, height * 0.55, 0, height);

    glow.addColorStop(0, 'rgba(60,40,90,0)');
    glow.addColorStop(1, 'rgba(90,50,80,0.55)');
    g.fillStyle = glow;
    g.fillRect(0, 0, width, height);

    let x = -10;

    while (x < width) {
        const w = 22 + rand() * 38;
        const h = 30 + rand() * 90;
        const top = height - h;

        g.fillStyle = rand() > 0.5 ? '#0b0d1c' : '#0f1224';
        g.fillRect(x, top, w, h);

        for (let wy = top + 6; wy < height - 8; wy += 8) {
            for (let wx = x + 4; wx < x + w - 5; wx += 7) {
                if (rand() > 0.72) {
                    g.fillStyle = rand() > 0.3 ? 'rgba(255,214,120,0.8)' : 'rgba(150,200,255,0.7)';
                    g.fillRect(wx, wy, 3, 4);
                }
            }
        }

        x += w + 1;
    }

    return canvas;
};
```

`components/show.ts`:

```ts
import { createParticles, createSprite, emitParticles, getColor, useGame, useKeyboard, useLoadParticles, useLoadTexture, usePointer, useUpdate } from 'nacatamalon';
import { drawCity } from '../art/city';
import { useRandom } from 'nacatamalon/math';
import { SPEED, COLOURS } from '../settings';

export const Show = () => {
    const game = useGame();
    const random = useRandom();
    const keys = useKeyboard();
    const pointer = usePointer();
    const W = game.getWidth();
    const H = game.getHeight();
    const ground = H - 18;

    createSprite({
        texture: useLoadTexture({
            src: drawCity(W, H).toDataURL(),
            key: 'fireworks-city'
        }),
        width: W,
        height: H,
        transform: {
            x: W / 2,
            y: H / 2
        },
        zIndex: 1
    });

    // One launcher per colour. The tint colours everything a launcher's rockets set off, the burst
    // and the crackle too, which is why the files themselves are white.
    const rocket = useLoadParticles({
        src: '/example-assets/effects/firework_rocket.particles'
    });

    const launchers = COLOURS.map((hex) => createParticles({
        effect: rocket,
        tint: getColor(hex),
        transform: {
            y: ground
        }
    }));

    const launch = (x: number, burstY: number) => {
        const launcher = launchers[Math.floor(random.rand() * launchers.length)];

        launcher.transform.x = x;
        launcher.overrides = {
            lifeScale: Math.max(0.2, (ground - burstY) / SPEED)
        };
        emitParticles(launcher, 1);
    };

    let quiet = 0;

    pointer.onDown((info) => {
        quiet = 4;
        launch(info.screenX + (random.rand() - 0.5) * 30, info.screenY);
    });

    let finale = 0;
    let nextAuto = 0.5;
    let nextFinale = 0;

    useUpdate((delta) => {
        if (keys.justPressed(' ')) {
            finale = 2.5;
        }

        // A slow show of its own when nobody is clicking.
        quiet = Math.max(0, quiet - delta);
        nextAuto -= delta;

        if (quiet === 0 && nextAuto <= 0) {
            nextAuto = 0.5 + random.rand() * 1.1;
            launch(60 + random.rand() * (W - 120), 50 + random.rand() * 110);
        }

        // The finale: as fast as the sky can take them.
        if (finale > 0) {
            finale -= delta;
            nextFinale -= delta;

            if (nextFinale <= 0) {
                nextFinale = 0.08;
                launch(30 + random.rand() * (W - 60), 40 + random.rand() * 140);
            }
        }
    });
};
```

`scenes/demo.ts`:

```ts
import { createScene, useSpawn } from 'nacatamalon';
import { Show } from '../components/show';

export const Night = () => {
    useSpawn(Show)();

    return createScene();
};
```

`settings.ts`:

```ts
export const WIDTH = 480;
export const HEIGHT = 320;
export const SPEED = 300;
export const COLOURS = ['#ff4d6d', '#ffd166', '#4cc9f0', '#80ff72', '#c77dff', '#ff9f1c'];
```
