# Asking the world

> isRetroAreaFree2d to place crates where they fit, findRetroBodies2d for a blast, castRetroRay2d for a laser.

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

Asking the retro world about a part of it, from an object that is never part of the answer. `isRetroAreaFree2d`: is anything solid there? The ghost crate under the pointer is green where a crate fits and red where it does not, and a **click** drops one only where it fits. `findRetroBodies2d`: what is in this circle, nearest first? **Space** sets off a blast at the pointer that throws everything within reach away from it, harder the closer. `castRetroRay2d`: the first thing along a line, where the turret's laser stops.

## Code

`index.ts`:

```ts
import { createGame } from 'nacatamalon';
import { installRetroPhysics2d } from 'nacatamalon/retro-physics2d';

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

// The one line that makes retro bodies move. Nothing to load: no WebAssembly, no waiting.
installRetroPhysics2d();

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

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

`components/crate.ts`:

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

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

export const Crate = (x: number, y: number) => {
    createSprite({
        tint: '#df7126',
        width: Settings.CRATE,
        height: Settings.CRATE,
        transform: {
            x,
            y
        }
    });
    useRetroBody2d({
        body: 'dynamic',
        collider: {
            shape: 'rect',
            width: Settings.CRATE,
            height: Settings.CRATE
        },
        bounce: 0.2,
    });
};
```

`components/solid.ts`:

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

// A wall or the floor: a body that never moves, the size of its picture.
export const Solid = (x: number, y: number, w: number, h: number) => {
    createSprite({
        tint: '#2a2a3a',
        width: w,
        height: h,
        transform: {
            x,
            y
        }
    });
    useRetroBody2d({
        body: 'static',
        collider: {
            shape: 'rect',
            width: w,
            height: h
        }
    });
};
```

`components/tools.ts`:

```ts
import { createShapes, getColor, useKeyboard, usePointer, useSelf, useSpawn, useUpdate } from 'nacatamalon';
import { castRetroRay2d, findRetroBodies2d, getRetroBody2d, isRetroAreaFree2d } from 'nacatamalon/retro-physics2d';
import type { TShapeLine, TShapeRect } from 'nacatamalon';

import * as Settings from '../settings';
import { Crate } from './crate';

const FITS = getColor('#4cff91');
const BLOCKED = getColor('#ff6b6b');

// The turret, and the hand that asks.
export const Tools = () => {
    createShapes({
        shapes: [{ type: 'rect', x: Settings.WALL, y: 60, width: 16, height: 16, fill: '#5a6a8a' }]
    });
    // The questions are asked from this object: it only has to be in the retro scene.
    const self = useSelf();
    const pointer = usePointer();
    const keys = useKeyboard();
    const drop = useSpawn(Crate, { name: 'Crate', parent: 'scene' });

    const overlay = createShapes({ shapes: [] });
    const ghost: TShapeRect = { type: 'rect', x: 0, y: 0, width: Settings.CRATE, height: Settings.CRATE, stroke: { width: 1, color: FITS } };
    const laser: TShapeLine = { type: 'line', points: [{ x: 0, y: 0 }, { x: 0, y: 0 }], stroke: { width: 1, color: '#ff5fd2' } };
    const blast = { type: 'circle' as const, x: 0, y: 0, radius: Settings.BLAST.radius, stroke: { width: 1, color: '#ffd93d' }, visible: false };
    overlay.shapes.push(ghost, laser, blast);
    let flash = 0;

    useUpdate((dt) => {
        const at = pointer.position();
        if (at === null) {
            return;
        }
        // Where a crate would go, and whether it fits there.
        const area = { x: at.worldX - Settings.CRATE / 2, y: at.worldY - Settings.CRATE / 2, width: Settings.CRATE, height: Settings.CRATE };
        const fits = isRetroAreaFree2d(self, area);
        ghost.x = area.x;
        ghost.y = area.y;
        ghost.stroke = { width: 1, color: fits ? FITS : BLOCKED };

        // The laser, from the turret towards the pointer, as far as the first thing it meets.
        const from = { x: Settings.WALL + 8, y: 68 };
        const hit = castRetroRay2d(self, from, { x: at.worldX - from.x, y: at.worldY - from.y });
        laser.points[0] = from;
        laser.points[1] = hit?.point ?? { x: from.x + (at.worldX - from.x) * 10, y: from.y + (at.worldY - from.y) * 10 };

        if (keys.justPressed(' ')) {
            // Everything in reach, nearest first, thrown away from the middle.
            for (const found of findRetroBodies2d(self, { x: at.worldX, y: at.worldY, radius: Settings.BLAST.radius })) {
                const body = getRetroBody2d(found);
                const there = found.drawables[0] as { transform: { x: number; y: number } } | undefined;
                if (body === null || there === undefined) continue;
                const dx = there.transform.x - at.worldX;
                const dy = there.transform.y - at.worldY;
                const far = Math.max(8, Math.hypot(dx, dy));
                const strength = Settings.BLAST.push * (1 - far / Settings.BLAST.radius + 0.2);
                body.setLinearVelocity(dx / far * strength, dy / far * strength - 120);
            }
            blast.x = at.worldX;
            blast.y = at.worldY;
            flash = 0.2;
        }
        flash = Math.max(0, flash - dt);
        blast.visible = flash > 0;
    });

    pointer.onDown((info) => {
        const area = { x: info.worldX - Settings.CRATE / 2, y: info.worldY - Settings.CRATE / 2, width: Settings.CRATE, height: Settings.CRATE };
        if (isRetroAreaFree2d(self, area)) {
            drop(info.worldX, info.worldY);
        }
    });
};
```

`scenes/demo.ts`:

```ts
import { createScene, createText, useRetroWorld2d, useSpawn } from 'nacatamalon';

import * as Settings from '../settings';
import { Solid } from '../components/solid';
import { Crate } from '../components/crate';
import { Tools } from '../components/tools';

export const Scene = () => {
    useRetroWorld2d({
        gravity: {
            x: 0,
            y: 700
        }
    });

    const solid = useSpawn(Solid, 'Wall');
    solid(Settings.WIDTH / 2, Settings.HEIGHT - Settings.WALL / 2, Settings.WIDTH, Settings.WALL);
    solid(Settings.WALL / 2, Settings.HEIGHT / 2, Settings.WALL, Settings.HEIGHT);
    solid(Settings.WIDTH - Settings.WALL / 2, Settings.HEIGHT / 2, Settings.WALL, Settings.HEIGHT);
    solid(300, 220, 120, 10);

    const crate = useSpawn(Crate, 'Crate');
    for (let i = 0; i < 6; i++) {
        crate(140 + (i % 3) * 20, Settings.HEIGHT - Settings.WALL - 9 - Math.floor(i / 3) * 18);
    }
    useSpawn(Tools, 'Tools')();

    createText({
        text: 'CLICK: A CRATE WHERE IT FITS   SPACE: A BLAST',
        transform: {
            x: Settings.WIDTH / 2,
            y: 20
        },
        anchor: {
            x: 0.5,
            y: 0
        }
    });

    return createScene();
};
```

`settings.ts`:

```ts
// Asking the retro world about a part of it, from an object (which is never part of the answer):
//
//   isRetroAreaFree2d: is anything solid there? The ghost crate under the pointer is green where a
//   crate fits, red where it does not, and a click drops one only where it fits.
//   findRetroBodies2d: what is in this circle, nearest first? Space sets off a blast at the pointer
//   that throws everything within reach away from it, harder the closer.
//   castRetroRay2d: the first thing along a line. The turret's laser stops on it.

export const WIDTH = 480;
export const HEIGHT = 320;
export const WALL = 12;
export const CRATE = 18;
export const BLAST = { radius: 70, push: 260 };
```
