# Particles written in code

> createParticles takes the effect written in code, with the fields of a .particles file: dust the colour of the ground, built per stage with lerpColor.

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

`createParticles({ effect })` takes the effect itself, written in code with the same fields as a `.particles` file. It is ready at once and can depend on the game.

Here the dust behind the car is the colour of the ground it drives on: each stage builds its effect from its palette, with `lerpColor` for the colour it fades into, and the puff it shows is painted with `createPixelTexture`. Click to drive to the next stage.

There is one emitter per stage and only the current one is lit, so the old dust settles on its own while the new one starts. Handing **the same object** to several emitters makes them share one effect, as if they named the same file.

## Code

`index.ts`:

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

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

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

`art/puff.ts`:

```ts
import { createPixels, mapPixels } from 'nacatamalon/pixels';

export const puff = () => mapPixels(createPixels(16, 16), (_, x, y) => {
    const d = Math.min(1, Math.hypot(x - 7.5, y - 7.5) / 8);

    return {
        r: 1,
        g: 1,
        b: 1,
        a: (1 - d) * (1 - d)
    };
});
```

`components/car.ts`:

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

import { ROAD } from '../settings';

export const Car = () => {
    const car = createSprite({
        width: 34,
        height: 12,
        tint: '#d23c3c',
        transform: {
            x: 240,
            y: ROAD - 6
        },
        zIndex: 2
    });

    const cabin = createSprite({
        width: 16,
        height: 8,
        tint: '#2a2a3a',
        transform: {
            x: 244,
            y: ROAD - 16
        },
        zIndex: 2
    });

    return {
        car,
        cabin
    };
};
```

`components/drive.ts`:

```ts
import { createParticles, createSprite, createText, getColor, playParticles, stopParticles, usePointer, useUpdate, useSpawn } from 'nacatamalon';
import { createPixelTexture } from 'nacatamalon/pixels';

import { WIDTH, HEIGHT, ROAD, STAGES, DRIVE } from '../settings';
import { dustFor } from '../effects';
import { puff } from '../art/puff';
import { Car } from './car';

export const Drive = () => {
    const sky = createSprite({
        width: WIDTH,
        height: ROAD,
        anchor: {
            x: 0,
            y: 0
        }
    });

    const ground = createSprite({
        width: WIDTH,
        height: HEIGHT - ROAD,
        anchor: {
            x: 0,
            y: 0
        },
        transform: {
            x: 0,
            y: ROAD
        }
    });

    const { car, cabin } = useSpawn(Car)().api;
    // One emitter per stage, built from that stage's colours; only the current one is lit. When the
    // stage changes the old dust settles on its own while the new one starts.
    const texture = createPixelTexture(puff());

    const emitters = STAGES.map((stage) => createParticles({
        effect: dustFor(getColor(stage.dust), getColor(stage.ground), texture),
        autoplay: false,
        zIndex: 1,
    }));

    const label = createText({
        text: '',
        tint: '#1a1030',
        transform: {
            x: 8,
            y: 8
        }
    });

    createText({
        text: 'CLICK: NEXT STAGE',
        tint: '#1a1030',
        transform: {
            x: 8,
            y: 20
        }
    });

    let stage = -1;

    const enter = (next: number) => {
        if (stage >= 0) {
            stopParticles(emitters[stage]);
        }

        stage = next;
        sky.tint = getColor(STAGES[stage].sky);
        ground.tint = getColor(STAGES[stage].ground);
        label.text = 'STAGE ' + (stage + 1) + ': ' + STAGES[stage].name;
        playParticles(emitters[stage]);
    };

    enter(0);
    usePointer().onDown(() => enter((stage + 1) % STAGES.length));

    useUpdate((_delta, time) => {
        const x = 240 + Math.sin(time * DRIVE.speed) * DRIVE.travel;
        const bump = Math.abs(Math.sin(time * DRIVE.bumpSpeed)) * DRIVE.bump;

        car.transform.x = x;
        car.transform.y = ROAD - 6 - bump;
        cabin.transform.x = x + 4;
        cabin.transform.y = ROAD - 16 - bump;

        // Every emitter follows the rear wheel, lit or not, so the next one starts in the right place.
        for (const emitter of emitters) {
            emitter.transform.x = x - 16;
            emitter.transform.y = ROAD;
        }
    });
};
```

`effects.ts`:

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

import type { TColor, TParticlesEffect2d, TTexture } from 'nacatamalon';

export const dustFor = (dust: TColor, ground: TColor, texture: TTexture): TParticlesEffect2d => ({
    kind: 'particles-2d',
    texture,
    max: 200,
    emission: {
        rate: 80
    },
    shape: {
        kind: 'rect',
        width: 8,
        height: 2
    },
    direction: Math.PI + 0.3,
    spread: 0.8,
    life: [0.8, 1.6],
    speed: [40, 90],
    size: [14, 26],
    gravity: {
        x: 0,
        y: -25
    },
    damping: 1.2,
    sizeOverLife: [
        {
            t: 0,
            scale: 0.5
        },
        {
            t: 1,
            scale: 2.2
        }
    ],
    colorOverLife: [
        {
            t: 0,
            color: dust,
            alpha: 1
        },
        {
            t: 1,
            color: lerpColor(dust, ground, 0.6),
            alpha: 0
        },
    ],
});
```

`scenes/demo.ts`:

```ts
import { createScene, useSpawn } from 'nacatamalon';
import { Drive } from '../components/drive';

export const RoadTrip = () => {
    useSpawn(Drive)();

    return createScene();
};
```

`settings.ts`:

```ts
export const WIDTH = 480;
export const HEIGHT = 270;
export const ROAD = 200;
export const DRIVE = {
    speed: 0.6,
    travel: 150,
    bumpSpeed: 12,
    bump: 1.5
};
export const STAGES = [
    {
        name: 'DESERT',
        sky: '#f2a65a',
        ground: '#d9a066',
        dust: '#f1d3a1'
    },
    {
        name: 'MEADOW',
        sky: '#7ec8e3',
        ground: '#4f8a3c',
        dust: '#a8c98a'
    },
    {
        name: 'SNOW',
        sky: '#b9c6d6',
        ground: '#e8eef5',
        dust: '#ffffff'
    },
    {
        name: 'SWAMP',
        sky: '#55606a',
        ground: '#4a3b2a',
        dust: '#7a6a4a'
    },
];
```
