# Particles on stairs

> Sparks bounce down a staircase and rain ends on it: each step carries a particle collider.

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

Sparks bounce down a staircase and rain ends on it. Each step is an object with a model and a `useParticleCollider3d`; the effects only say how they react, in the `collision` of their files.

The stairs sway and the particles meet them where they are. **Click** to turn the colliders off and on.

## Code

`index.ts`:

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

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

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

`components/stairs.ts`:

```ts
import { createParticles3d, useLoadParticles, usePointer, useSpawn, useTransform, useUpdate, type TParticleCollider3d } from 'nacatamalon';
import * as Settings from '../settings';
import { Step } from './step';

export const Stairs = () => {
    const place = useTransform({
        y: 1.2,
        z: -2
    });

    const colliders: TParticleCollider3d[] = [];
    const step = useSpawn(Step);

    for (let n = 0; n < Settings.STEPS; n++) {
        colliders.push(step({
            index: n
        }).api);
    }

    // On the top step and part of the stairs, so it sways with them.
    createParticles3d({
        effect: useLoadParticles({
            src: Settings.FILES.sparks
        }),
        transform: {
            y: 0.4
        }
    });

    // Click to take the colliders away and give them back: without them everything falls through.
    usePointer().onDown(() => {
        for (const collider of colliders) {
            collider.enabled = !collider.enabled;
        }
    });

    useUpdate((_delta, time) => {
        const t = time * Settings.SWAY.speed;

        place.rotationY = Math.sin(t) * Settings.SWAY.amount;
    });
};
```

`components/step.ts`:

```ts
import { createMesh, getColor, useCubeGeometry, useParticleCollider3d, useTransform } from 'nacatamalon';

export const Step = ({ index: n }: { index: number }) => {
    useTransform({
        y: -n * 0.5,
        z: n * 0.8,
        scaleX: 3,
        scaleY: 0.5,
        scaleZ: 0.8
    });
    createMesh({
        geometry: useCubeGeometry(),
        tint: getColor(n % 2 === 0 ? '#4c566a' : '#3b4252')
    });

    return useParticleCollider3d({
        shape: {
            kind: 'box',
            size: [1, 1, 1]
        }
    });
};
```

`scenes/demo.ts`:

```ts
import { createParticles3d, createScene, useCamera3d, useDirectionalLight, useLoadParticles, useSpawn } from 'nacatamalon';
import { Stairs } from '../components/stairs';
import { FILES } from '../settings';

export const Level = () => {
    useCamera3d({
        projection: 'perspective',
        fov: 50,
        near: 0.1,
        far: 100,
        transform: {
            x: 6,
            y: 3,
            z: 5,
            rotationY: 0.876,
            rotationX: -0.367
        }
    });
    useDirectionalLight({
        intensity: 0.9,
        ambient: 0.35
    });

    useSpawn(Stairs)();

    // Rain over the stairs, from the scene and not from the stairs: it does not sway.
    createParticles3d({
        effect: useLoadParticles({
            src: FILES.rain
        }),
        transform: {
            y: 4
        }
    });

    return createScene();
};
```

`settings.ts`:

```ts
export const WIDTH = 480;
export const HEIGHT = 320;
export const STEPS = 6;
export const SWAY = {
    speed: 0.5,
    amount: 0.5
};
export const FILES = {
    sparks: '/example-assets/effects/stair_sparks3d.particles',
    rain: '/example-assets/effects/rain3d.particles'
};
```
