# Sensor gate

> A sensor is saved with the scene; hearing it is the adapter’s job. The gate marks each crate that falls through it, stopping none.

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

The line between the two halves. `sensor: true` is a field the scene writes down, so a saved level keeps its checkpoints and pickups. Hearing one is not: a contact is an event, not a property of an object, so it comes through the adapter, and `onEnter` hands over the other object rather than only saying that something happened.

The gate flashes and adds a tally mark for each crate that falls through it, stopping none of them. **Click** to drop another.

## 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/crate.ts`:

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

export const Crate = (x: number, z: number, tint: string) => {
    useTransform({
        x,
        y: 9,
        z
    });
    createMesh({
        geometry: useCubeGeometry(),
        tint: getColor(tint)
    });
    usePhysicsBody3d({
        body: 'dynamic',
        collider: {
            shape: 'box',
            size: [1, 1, 1]
        },
        density: 30
    });
};
```

`components/gate.ts`:

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

import { getPhysicsBody3d } from 'nacatamalon/physics3d';
import * as Settings from '../settings';

// The gate. It is a collider like any other and it stops nothing: a crate falls through it and
// lands on the floor below, and the gate says so on the way past.
export const Gate = (onPass: (name: string) => void) => {
    useTransform({
        y: 3,
        scaleX: 5,
        scaleY: 0.3,
        scaleZ: 5
    });

    const slab = createMesh({
        geometry: useCubeGeometry(),
        tint: Settings.CALM
    });

    usePhysicsBody3d({
        body: 'static',
        collider: {
            shape: 'box',
            size: [1, 1, 1]
        },
        sensor: true
    });

    // The engine built the body and handed it to the adapter, so the handle never came back here:
    // getPhysicsBody3d is the way to it. Nothing in the editor while the scene is being built.
    let flash = 0;
    const body = getPhysicsBody3d(useSelf());

    if (body) {
        // onEnter hands over the OTHER object, which is how a gate knows WHAT went through it and
        // not merely that something did. Listening is the opt-in: a body nobody listens to never
        // asks the solver to report anything at all. Set up here, it leaves with this gate.
        body.onEnter((other) => {
            flash = 1;
            onPass(other.name);
        });
    }

    useUpdate((dt) => {
        flash = Math.max(0, flash - dt * 2.5);
        slab.material.tint = {
            r: Settings.CALM.r + (Settings.LIT.r - Settings.CALM.r) * flash,
            g: Settings.CALM.g + (Settings.LIT.g - Settings.CALM.g) * flash,
            b: Settings.CALM.b + (Settings.LIT.b - Settings.CALM.b) * flash,
            a: 1,
        };
    });
};
```

`components/ground.ts`:

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

export const Ground = () => {
    useTransform({
        y: -0.5,
        scaleX: 14,
        scaleZ: 14
    });
    createMesh({
        geometry: useCubeGeometry(),
        tint: '#2a2a3a'
    });
    usePhysicsBody3d({
        body: 'static',
        collider: {
            shape: 'box',
            size: [1, 1, 1]
        }
    });
};
```

`components/mark.ts`:

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

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

// A tally: one of these appears each time something goes through.
export const Mark = (i: number) => {
    useTransform({
        x: -6 + i * 0.5,
        y: 6.5,
        scaleX: 0.3,
        scaleY: 0.3,
        scaleZ: 0.3
    });
    createMesh({
        geometry: useCubeGeometry(),
        tint: Settings.LIT
    });
};
```

`scenes/demo.ts`:

```ts
import { createScene, useCamera3d, useDirectionalLight, usePhysicsWorld3d, usePointer, useSpawn } from 'nacatamalon';
import { useRandom } from 'nacatamalon/math';

import * as Settings from '../settings';
import { Ground } from '../components/ground';
import { Mark } from '../components/mark';
import { Gate } from '../components/gate';
import { Crate } from '../components/crate';

export const Scene = () => {
    useCamera3d({
        projection: 'perspective',
        fov: 52,
        near: 0.1,
        far: 100,
        transform: {
            y: 5,
            z: 15,
            rotationX: -0.24
        }
    });
    useDirectionalLight({
        color: '#ffffff',
        intensity: 1.1,
        ambient: 0.32
    });

    const rng = useRandom();

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

    useSpawn(Ground)();

    const spawnMark = useSpawn(Mark);
    let passed = 0;

    useSpawn(Gate)(() => {
        spawnMark(passed);
        passed += 1;
    });

    const spawnCrate = useSpawn(Crate);
    const drop = () => spawnCrate(rng.rand(-1.6, 1.6), rng.rand(-1.6, 1.6), rng.choose(Settings.PALETTE));

    for (let i = 0;i < 3;i++) {
        drop();
    }

    // Click to drop another one through the gate.
    usePointer().onDown(drop);

    return createScene();
};
```

`settings.ts`:

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

export const WIDTH = 520;
export const HEIGHT = 340;
// This example is about the LINE between the two halves.
//
// What the scene writes down: every shape here, what it is made of, and the one field that makes
// the blue slab a gate rather than a floor. A collider marked as noticing settles nothing, so
// things pass straight through it, and a saved level keeps every checkpoint and pickup it has.
//
// What only the adapter can do: HEAR it. A contact is an event, and an event is not a property of
// an object, so there is nothing for a file to hold. The object's own body is reached with
// getPhysicsBody3d(), and the listening is ordinary engine plumbing from there.
//
// Same split as an impulse: what a thing IS goes in the scene, what HAPPENS goes through here.

export const PALETTE = ['#4cff91', '#ff6b6b', '#5db6ff', '#ffd93d', '#c77dff', '#ff9f45'];
export const CALM = getColor('#3a6ea8');
export const LIT = getColor('#eaf4ff');
```
