# Retro and full physics

> The same pile in a retro scene and a full one, both installed in one game: square stacking against tumbling, click to switch.

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

The same pile, twice. In the retro scene the crates never turn: they land square, slide off the step straight and stack pixel on pixel. In the full one they tumble, roll and settle at angles. Neither is better: the retro one is what the old games did, nothing to load and the same on every machine; the full one is a real simulation, with joints, polygons and turning.

A game can install both: a scene declares one world, retro or full, and its bodies are of that kind. **Click** to switch scenes.

## Code

`index.ts`:

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

import * as Settings from './settings';
import { Retro, Full } from './scenes/demo';

// Both installed, side by side. Each takes only its own kind of world and body, so each scene is
// moved by the world it declares.
installPhysics2d();
installRetroPhysics2d();

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

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

`components/pile.ts`:

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

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

type TKind = 'retro' | 'full';

// One body of either kind, the same size as its picture.
const body = (kind: TKind, moving: boolean, width: number, height: number) => {
    const collider = { shape: 'rect' as const, width, height };
    if (kind === 'retro') {
        useRetroBody2d({ body: moving ? 'dynamic' : 'static', collider });
    } else {
        usePhysicsBody2d({ body: moving ? 'dynamic' : 'static', collider, friction: 0.5 });
    }
};

export const Block = (kind: TKind, x: number, y: number, w: number, h: number) => {
    createSprite({
        tint: '#2a2a3a',
        width: w,
        height: h,
        transform: {
            x,
            y
        }
    });
    body(kind, false, w, h);
};

export const Crate = (kind: TKind, x: number, y: number, size: number, color: string) => {
    createSprite({
        tint: color,
        width: size,
        height: size,
        transform: {
            x,
            y
        }
    });
    body(kind, true, size, size);
};

export const layout = (kind: TKind, block: (...args: Parameters<typeof Block>) => unknown, crate: (...args: Parameters<typeof Crate>) => unknown) => {
    block(kind, Settings.WIDTH / 2, Settings.HEIGHT - Settings.WALL / 2, Settings.WIDTH, Settings.WALL);
    block(kind, Settings.WALL / 2, Settings.HEIGHT / 2, Settings.WALL, Settings.HEIGHT);
    block(kind, Settings.WIDTH - Settings.WALL / 2, Settings.HEIGHT / 2, Settings.WALL, Settings.HEIGHT);
    // A step, for crates to land half on.
    block(kind, 170, Settings.HEIGHT - Settings.WALL - 20, 60, 40);
    Settings.CRATES.forEach(([x, y, size], i) => crate(kind, x, y, size, Settings.PALETTE[i % Settings.PALETTE.length]));
};
```

`scenes/demo.ts`:

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

import * as Settings from '../settings';
import { Block, Crate, layout } from '../components/pile';

const caption = (text: string) => createText({
    text,
    transform: {
        x: Settings.WIDTH / 2,
        y: 24
    },
    anchor: {
        x: 0.5,
        y: 0
    }
});

export const Retro = () => {
    useRetroWorld2d({ gravity: { x: 0, y: 700 } });
    layout('retro', useSpawn(Block), useSpawn(Crate));
    caption('RETRO PHYSICS - CLICK FOR THE FULL ONE');
    const scene = useScene();
    usePointer().onDown(() => scene.change('Full'));
    return createScene();
};

export const Full = () => {
    usePhysicsWorld2d({ gravity: { x: 0, y: 700 } });
    layout('full', useSpawn(Block), useSpawn(Crate));
    caption('FULL PHYSICS - CLICK FOR THE RETRO ONE');
    const scene = useScene();
    usePointer().onDown(() => scene.change('Retro'));
    return createScene();
};
```

`settings.ts`:

```ts
// The same pile, twice. In the retro scene the crates never turn: they land square, slide off the
// step straight, and stack pixel on pixel. In the full one they tumble, roll and settle at angles.
// Neither is better: the retro one is what the old games did, nothing to load and the same on
// every machine; the full one is a real simulation, with joints, polygons and turning.
//
// A game can use both: a scene declares one world, retro or full, and its bodies are of that kind.

export const WIDTH = 480;
export const HEIGHT = 320;
export const WALL = 12;
export const CRATES: Array<[number, number, number]> = [
    // x, y, size
    [150, -20, 26], [170, -80, 22], [140, -140, 30], [190, -200, 18],
    [300, -40, 24], [320, -110, 26], [290, -170, 20], [330, -240, 28],
];
export const PALETTE = ['#4cff91', '#ff6b6b', '#5db6ff', '#ffd93d', '#c77dff'];
```
