# Falling crates, retro

> Boxes that never turn and balls that stay round, piled pixel on pixel with nothing to load: useRetroWorld2d and useRetroBody2d.

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

The starting point of retro physics, `nacatamalon/retro-physics2d`: bodies that never turn, moved in pixels a fixed step at a time and stopped against each other axis by axis, the way the old games did. Boxes stay square to the screen and balls stay round, so a pile comes to rest exactly, pixel on pixel, and the same pile comes out on every machine. There is nothing to load: no WebAssembly, no waiting.

A scene asks for it with `useRetroWorld2d` (gravity in pixels per second squared, down being +y), and each object with `useRetroBody2d`: `'static'` never moves, `'dynamic'` falls and is pushed. **Click** to drop more.

## 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');
```

`art/ball.ts`:

```ts
import { createPixels, createPixelTexture, fillCircle } from 'nacatamalon/pixels';

// A white disc, painted once for the scene and tinted per ball. Odd-sized so it has a middle
// pixel and comes out round.
export const paintBall = (size: number) => {
    const side = size | 1;
    const pixels = createPixels(side, side);
    fillCircle(pixels, (side - 1) / 2, (side - 1) / 2, (side - 1) / 2, '#ffffff');
    return createPixelTexture(pixels);
};
```

`components/crate.ts`:

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

// A crate, or a ball when it is handed the round picture. Its collider is centred on where the
// object is, like its picture, so the two line up with nothing worked out by hand.
export const Crate = (x: number, y: number, size: number, color: string, ball: TTexture | null) => {
    createSprite({
        ...(ball !== null ? { texture: ball } : {}),
        tint: color,
        width: size,
        height: size,
        transform: {
            x,
            y
        }
    });
    useRetroBody2d({
        body: 'dynamic',
        collider: ball !== null
            ? { shape: 'circle', radius: size / 2 }
            : { shape: 'rect', width: size, height: size },
        // A ball keeps some of its speed when it lands; a crate keeps none.
        bounce: ball !== null ? 0.4 : 0,
    });
};
```

`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
        }
    });
};
```

`scenes/demo.ts`:

```ts
import { createScene, createText, usePointer, useRetroWorld2d, useSpawn, useTimer } from 'nacatamalon';
import { useRandom } from 'nacatamalon/math';

import * as Settings from '../settings';
import { Solid } from '../components/solid';
import { Crate } from '../components/crate';
import { paintBall } from '../art/ball';

export const Scene = () => {
    const rng = useRandom();

    useRetroWorld2d({
        gravity: {
            x: 0,
            y: 600
        }
    });

    useSpawn(Solid)(Settings.WIDTH / 2, Settings.HEIGHT - Settings.WALL / 2, Settings.WIDTH, Settings.WALL);
    useSpawn(Solid)(Settings.WALL / 2, Settings.HEIGHT / 2, Settings.WALL, Settings.HEIGHT);
    useSpawn(Solid)(Settings.WIDTH - Settings.WALL / 2, Settings.HEIGHT / 2, Settings.WALL, Settings.HEIGHT);
    // A shelf in the middle, to pile against.
    useSpawn(Solid)(Settings.WIDTH / 2, 220, 120, 10);

    createText({
        text: 'CLICK TO DROP MORE',
        transform: {
            x: Settings.WIDTH / 2,
            y: 20
        },
        anchor: {
            x: 0.5,
            y: 0
        }
    });

    const ball = paintBall(23);
    const spawnCrate = useSpawn(Crate);
    let count = 0;
    const drop = (x: number, y: number) => {
        if (count >= Settings.MAX) {
            return;
        }
        count++;
        // Mostly crates, every third one a ball.
        const color = rng.choose(Settings.PALETTE);
        spawnCrate(x, y, rng.randInt(12, 22), color, count % 3 === 0 ? ball : null);
    };

    // A steady rain at first, then whatever the player drops.
    const timer = useTimer();
    const rain = timer.every(0.25, () => {
        drop(rng.rand(Settings.WALL + 20, Settings.WIDTH - Settings.WALL - 20), -20);
        if (count >= 40) {
            rain.cancel();
        }
    });

    usePointer().onDown((info) => drop(info.worldX, info.worldY));

    return createScene();
};
```

`settings.ts`:

```ts
// Retro physics: bodies that never turn, moved in pixels a fixed step at a time and stopped
// against each other axis by axis, the way the old games did. Boxes stay square to the screen and
// balls stay round, so a pile comes to rest exactly, pixel on pixel, and the same pile comes out
// on every machine.
//
// A scene says it wants it with useRetroWorld2d (gravity in pixels per second squared, +y down),
// and each object with useRetroBody2d: 'static' never moves, 'dynamic' falls and is pushed.

export const WIDTH = 480;
export const HEIGHT = 340;
export const WALL = 12;
export const MAX = 70;
export const PALETTE = ['#4cff91', '#ff6b6b', '#5db6ff', '#ffd93d', '#c77dff'];
```
