# Retro physics from a document

> A scene document with retro-world-2d and retro-body-2d components, built with sceneFromDoc; a scaled body and fields left to their defaults.

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

The editor's path: a scene written down as a document, with a `retro-world-2d` on its root and a `retro-body-2d` on each object, read with `parseSceneDoc` and built with `sceneFromDoc`. The engine keeps, reads and writes these with nothing installed; `installRetroPhysics2d()` is what makes them move.

Every field a body leaves out is filled in when the file is read (bounce 0, drag 0, no speed limit, `gravityScale` 1, mass 1…), so a document means one thing whoever reads it.

## Code

`index.ts`:

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

import * as Settings from './settings';
import { Level as 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');
```

`data/room.ts`:

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

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

const box = (id: string, name: string, x: number, y: number, w: number, h: number, tint: TColor, body: Record<string, unknown>, scale = 1) => ({
    id,
    name,
    transform: {
        x,
        y,
        z: 0,
        rotation: 0,
        rotationX: 0,
        rotationY: 0,
        scaleX: scale,
        scaleY: scale,
        scaleZ: 1
    },
    components: [
        {
            type: 'sprite',
            id: id + '-art',
            texture: null,
            tint,
            width: w,
            height: h
        },
        body,
    ],
    children: [],
});

// A retro body: how it is driven and its shape. Everything else is left to the defaults, except
// where a field says otherwise.
const retro = (id: string, body: string, collider: Record<string, unknown>, extra: Record<string, unknown> = {}) => ({
    type: 'retro-body-2d',
    id: id + '-body',
    name: 'Body',
    body,
    collider,
    ...extra,
});

const GREY = getColor('#2a2a3a');

export const doc = {
    format: 'nacatamalon-scene',
    version: 1,
    name: 'Level',
    assets: [],
    root: {
        id: 'root',
        name: 'Level',
        transform: null,
        // The world is the SCENE's, so it sits on the root.
        components: [
            {
                type: 'retro-world-2d',
                id: 'world',
                gravity: {
                    x: 0,
                    y: 620
                }
            }
        ],
        children: [
            box('ground', 'Ground', Settings.WIDTH / 2, Settings.HEIGHT - Settings.WALL / 2, Settings.WIDTH, Settings.WALL, GREY,
                retro('ground', 'static', { shape: 'rect', width: Settings.WIDTH, height: Settings.WALL })),
            box('left', 'WallL', Settings.WALL / 2, Settings.HEIGHT / 2, Settings.WALL, Settings.HEIGHT, GREY,
                retro('left', 'static', { shape: 'rect', width: Settings.WALL, height: Settings.HEIGHT })),
            box('right', 'WallR', Settings.WIDTH - Settings.WALL / 2, Settings.HEIGHT / 2, Settings.WALL, Settings.HEIGHT, GREY,
                retro('right', 'static', { shape: 'rect', width: Settings.WALL, height: Settings.HEIGHT })),

            // Five crates, all declared, none of them scripted.
            ...[0, 1, 2, 3, 4].map((i) =>
                box('crate' + i, 'Crate' + i, 60 + i * 62 + 13, -40 - i * 70, 26, 26, getColor(Settings.PALETTE[i]),
                    retro('crate' + i, 'dynamic', { shape: 'rect', width: 26, height: 26 }))),

            // A bouncy one, with the fields a file can carry.
            box('ball', 'Ball', 225, -260, 24, 24, getColor('#ffffff'),
                retro('ball', 'dynamic', { shape: 'circle', radius: 12 }, { bounce: 0.7, maxSpeed: { x: 300, y: 500 } })),

            // Scaled x2: the collider is authored in the object's own units and its placement
            // scales it, as it scales the picture. 20 declared, 40 simulated.
            box('big', 'BigCrate', 310, -340, 20, 20, getColor('#ff9f45'),
                retro('big', 'dynamic', { shape: 'rect', width: 20, height: 20 }, { mass: 4 }), 2),
        ],
    },
};
```

`scenes/demo.ts`:

```ts
import { parseSceneDoc, sceneFromDoc } from 'nacatamalon/files';

import { doc } from '../data/room';

export const Level = sceneFromDoc(parseSceneDoc(doc, 'scenes/room.scene'));
```

`settings.ts`:

```ts
// The editor's path: a scene written down as a document, with a `retro-world-2d` on its root and a
// `retro-body-2d` on each object, read with parseSceneDoc and built with sceneFromDoc. The engine
// keeps, reads and writes these with nothing installed; installRetroPhysics2d() is what makes
// them move.
//
// Every field a body leaves out is filled in when the file is read (bounce 0, drag 0, no speed
// limit, gravityScale 1, mass 1...), so a document means one thing whoever reads it.

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