# Pachinko

> Dynamic balls falling through static pegs: the same collider with another body type. Click to send them up.

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

Dynamic balls pinball down through a grid of static pegs: the same collider, another body type. A touch of `bounce` makes each peg hit deflect, and thin front and back walls keep them in one plane so it reads as a board. **Click** to send them back up.

## Code

`index.ts`:

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

import { installPhysics3d } from 'nacatamalon/physics3d';
import * as Settings from './settings';
import { Scene } from './scenes/demo';

installPhysics3d();

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

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

`components/ball.ts`:

```ts
import { createMesh, getColor, usePhysicsBody3d, useTransform, useSphereGeometry } from 'nacatamalon';

import * as Settings from '../settings';

export const Ball = (x: number, y: number, tint: string) => {
    useTransform({
        x,
        y
    });
    createMesh({
        geometry: useSphereGeometry({
            radius: Settings.RADIUS
        }),
        tint: getColor(tint)
    });
    usePhysicsBody3d({
        body: 'dynamic',
        collider: {
            shape: 'sphere',
            radius: Settings.RADIUS
        },
        density: 30,
        bounce: 0.35,
    });
};
```

`components/glass.ts`:

```ts
import { usePhysicsBody3d, useTransform } from 'nacatamalon';

// Invisible, and that is all that separates it from a wall.
export const Glass = (z: number) => {
    useTransform({
        z
    });
    usePhysicsBody3d({
        body: 'static',
        collider: {
            shape: 'box',
            size: [11, 11, 0.4]
        }
    });
};
```

`components/peg.ts`:

```ts
import { createMesh, usePhysicsBody3d, useTransform, useSphereGeometry } from 'nacatamalon';

import * as Settings from '../settings';

export const Peg = (x: number, y: number) => {
    useTransform({
        x,
        y
    });
    createMesh({
        geometry: useSphereGeometry({
            radius: Settings.PEG
        }),
        tint: '#5db6ff'
    });
    usePhysicsBody3d({
        body: 'static',
        collider: {
            shape: 'sphere',
            radius: Settings.PEG
        }
    });
};
```

`components/wall.ts`:

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

export const Wall = (x: number, y: number, sx: number, sy: number) => {
    useTransform({
        x,
        y,
        scaleX: sx,
        scaleY: sy,
        scaleZ: 2
    });
    createMesh({
        geometry: useCubeGeometry(),
        tint: '#242436'
    });
    usePhysicsBody3d({
        body: 'static',
        collider: {
            shape: 'box',
            size: [1, 1, 1]
        }
    });
};
```

`scenes/demo.ts`:

```ts
import { createScene, useCamera3d, useDirectionalLight, usePhysicsWorld3d, usePointer, useSpawn } from 'nacatamalon';
import { useRandom } from 'nacatamalon/math';
import { getPhysicsBody3d } from 'nacatamalon/physics3d';
import * as Settings from '../settings';
import { Wall } from '../components/wall';
import { Glass } from '../components/glass';
import { Peg } from '../components/peg';
import { Ball } from '../components/ball';

export const Scene = () => {
    useCamera3d({
        projection: 'perspective',
        fov: 50,
        near: 0.1,
        far: 100,
        transform: {
            y: 0.5,
            z: 13,
            rotationX: -0.05
        }
    });
    useDirectionalLight({
        color: '#ffffff',
        intensity: 1.1,
        ambient: 0.3
    });

    const rng = useRandom();

    usePhysicsWorld3d({
        gravity: {
            x: 0,
            y: -9.81,
            z: 0
        }
    });

    const spawnWall = useSpawn(Wall);

    spawnWall(0, -5, 11, 1);
    spawnWall(-5, 0, 1, 11);
    spawnWall(5, 0, 1, 11);

    // Front and back, keeping every ball in the board's own plane so it reads as a board.
    const spawnGlass = useSpawn(Glass);

    spawnGlass(0.8);
    spawnGlass(-0.8);

    const spawnPeg = useSpawn(Peg);

    for (let row = 0; row < 6; row++) {
        const y = 3 - row * 1.15;
        const offset = row % 2 === 0 ? 0 : 0.6;

        for (let x = -3.6 + offset; x <= 3.6; x += 1.2) {
            spawnPeg(x, y);
        }
    }

    const spawnBall = useSpawn(Ball);
    const balls: ReturnType<typeof spawnBall>[] = [];

    for (let i = 0; i < 8; i++) {
        balls.push(spawnBall(rng.rand(-3, 3), 5 + i * 0.9, rng.choose(Settings.PALETTE)));
    }

    // Click to fling them all back to the top for another run through the pegs.
    const pointer = usePointer();

    pointer.onDown(() => {
        for (const ball of balls) {
            getPhysicsBody3d(ball)?.applyImpulse(rng.rand(-20, 20), 120, 0);
        }
    });

    return createScene();
};
```

`settings.ts`:

```ts
export const WIDTH = 480;
export const HEIGHT = 320;
// A pachinko board: balls rain down through an offset grid of fixed pegs, pinballing left and
// right on every hit. The pegs are STATIC spheres and never move; the balls are dynamic and a
// little bouncy, so each contact deflects them.
//
// The two panes of glass are the interesting objects here: they have a collider and draw
// NOTHING. An object is a place in the scene first and a picture second, so a thing that is only
// there to be hit is written exactly like the rest, minus the line that draws it.

export const PALETTE = ['#4cff91', '#5db6ff', '#ffd93d', '#c77dff', '#ff9f45', '#ff6b6b'];
export const PEG = 0.22;
export const RADIUS = 0.3;
```
