# Particles

> A particle pool simulated on the CPU and drawn at once per emitter: fire, smoke and a burst on every click.

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

A pool of short-lived quads, simulated on the CPU and drawn at once: one draw call per emitter, whatever the count. The look lives in a `.particles` file: how they are born, colour and size over life, gravity, and whether they blend `alpha` (smoke) or `additive` (fire and sparks).

**Click** the game to fire a burst. The rockets add the other two things a file can declare: a `trail` of past positions, and a `children` effect fired when each one dies.

## Code

`index.ts`:

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

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

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

`components/click_sparks.ts`:

```ts
import { createParticles, emitParticles, useLoadParticles, usePointer } from 'nacatamalon';
import * as Settings from '../settings';

export const ClickSparks = () => {
    // A burst, fired by the game rather than running on its own. 'autoplay: false' leaves it inert,
    // and emitParticles is what a script means by "now".
    const burst = createParticles({
        effect: useLoadParticles({
            src: Settings.FILES.sparks
        }),
        autoplay: false,
        zIndex: 2
    });

    usePointer().onDown((info) => {
        burst.transform.x = info.worldX;
        burst.transform.y = info.worldY;
        // Moved and fired in the same breath, and the sparks appear where it was just put. They are
        // born on the next step rather than inside this call, which is exactly what makes that
        // work: a particle is born where its emitter ENDS UP, and that is not known until the tree
        // has been walked later in this same frame.
        emitParticles(burst, Settings.BURST);
    });
};
```

`components/fires.ts`:

```ts
import { createParticles, useLoadParticles } from 'nacatamalon';
import * as Settings from '../settings';

export const Fires = () => {
    const smoke = useLoadParticles({
        src: Settings.FILES.smoke
    });

    const fire = useLoadParticles({
        src: Settings.FILES.fire
    });

    // Smoke first so it draws behind the flame. There is no depth in the plane, so zIndex is the
    // whole of layering, exactly as it is for sprites.
    createParticles({
        effect: smoke,
        transform: {
            x: 150,
            y: 250
        },
        zIndex: 0
    });
    createParticles({
        effect: fire,
        transform: {
            x: 150,
            y: 250
        },
        zIndex: 1
    });

    // The same fire again, half the size and twice as quick, WITHOUT a second file. That is what
    // overrides are for: every game eventually wants the same explosion a bit smaller, and making
    // that a second file is what forks an effect into six near-copies nobody can keep in step.
    createParticles({
        effect: fire,
        transform: {
            x: 330,
            y: 250
        },
        overrides: {
            sizeScale: 0.5,
            speedScale: 1.6,
            rateScale: 0.7
        },
        zIndex: 1,
    });
};
```

`scenes/demo.ts`:

```ts
import { createScene, useSpawn } from 'nacatamalon';
import { Fires } from '../components/fires';
import { ClickSparks } from '../components/click_sparks';

export const Game = () => {
    useSpawn(Fires)();
    useSpawn(ClickSparks)();

    return createScene();
};
```

`settings.ts`:

```ts
export const WIDTH = 480;
export const HEIGHT = 320;
export const FILES = {
    smoke: '/example-assets/effects/smoke.particles',
    fire: '/example-assets/effects/fire.particles',
    sparks: '/example-assets/effects/sparks.particles'
};
export const BURST = 40;
```
