# Tower from a document

> The same physics written as data: a scene document handed to sceneFromDoc. The tower falls because the document says so.

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

The same physics, authored as **data**: a scene document, the exact JSON an editor writes, handed to `sceneFromDoc`. Nothing calls `usePhysicsBody3d`: the tower falls because the document says those objects are colliders, and it is written leaning.

The world sits on the root, because how hard things fall belongs to the scene. Take `installPhysics3d()` out and it still loads and draws; it just never moves.

## Code

`index.ts`:

```ts
import { createGame } from 'nacatamalon';
import { installPhysics3d } from 'nacatamalon/physics3d';
import * as Settings from './settings';
import { Tower } from './scenes/demo';

installPhysics3d();

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

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

`data/room.ts`:

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

import * as Settings from '../settings';
import type { TColor } from 'nacatamalon';

const place = (x: number, y: number, z: number, sx = 1, sy = 1, sz = 1) => ({
    x,
    y,
    z,
    rotation: 0,
    rotationX: 0,
    rotationY: 0,
    scaleX: sx,
    scaleY: sy,
    scaleZ: sz,
});

// The document is verbose because it is a FILE FORMAT and not an authoring API. In an editor you
// would never type this: you would drag an object in and tick "Collider".
const object = (id: string, name: string, transform: ReturnType<typeof place>, geometry: string, tint: TColor, physics: Record<string, unknown>) => ({
    id,
    name,
    transform,
    components: [
        {
            type: 'mesh',
            id: id + '-art',
            geometry,
            material: {
                texture: null,
                tint,
                emissive: '#000000',
                shininess: 16,
                alpha: 1
            },
        },
        physics,
    ],
    children: [],
});

// The shape, how it is driven, and what it is made of. Everything it leaves out is filled in when
// the file is read, so a document written before a field existed still means exactly one thing.
//
// An object's size multiplies its collider the way it multiplies what it draws, so the shape is
// always written at ONE metre and the object stretches the two together.
const collider = (id: string, body: string, shape: string, extra: Record<string, unknown> = {
}) => ({
    type: 'physics-body-3d',
    id: id + '-body',
    name: 'Body',
    body,
    collider: {
        shape,
        ...extra
    },
    offset: [0, 0, 0],
    bounce: 0.1,
    friction: 0.5,
    density: 30,
});

export const doc = {
    format: 'nacatamalon-scene',
    version: 1,
    name: 'Tower',
    // The shapes the scene needs, by name. A collider can name one of these too, which is how a
    // hull or a room's real triangles are declared.
    assets: [
        {
            type: 'geometry',
            key: 'cube',
            source: {
                kind: 'cube'
            }
        },
        {
            type: 'geometry',
            key: 'ball',
            source: {
                kind: 'uvSphere',
                radius: 0.5
            }
        },
    ],
    root: {
        id: 'root',
        name: 'Tower',
        transform: null,
        components: [
            {
                type: 'camera-3d',
                id: 'cam',
                projection: 'perspective',
                transform: {
                    x: 1.5,
                    y: 5,
                    z: 15,
                    rotation: 0,
                    rotationX: -0.26,
                    rotationY: 0,
                    scaleX: 1,
                    scaleY: 1,
                    scaleZ: 1,
                },
                fov: 50,
                near: 0.1,
                far: 100,
                zoom: 1,
            },
            {
                type: 'light',
                id: 'sun',
                kind: 'directional',
                color: '#ffffff',
                intensity: 1.1,
                ambient: 0.26,
                transform: {
                    x: 0,
                    y: 10,
                    z: 6,
                    rotationX: -0.7,
                    rotationY: 0
                },
            },
            // How hard things fall is the SCENE's, so it sits on the root.
            {
                type: 'physics-world-3d',
                id: 'world',
                gravity: {
                    x: 0,
                    y: -9.81,
                    z: 0
                }
            },
        ],
        children: [
            object('ground', 'Ground', place(0, -0.5, 0, 16, 1, 16), 'cube', getColor('#2a2a3a'),
                collider('ground', 'static', 'box', {
                    size: [1, 1, 1]
                })),

            // Eight crates, each set down a little further over than the one below it. Nothing
            // scripts the collapse and nothing pushes it: the tower is written LEANING, and it
            // falls because that is what a leaning tower does. Every bit of that is data.
            ...[0, 1, 2, 3, 4, 5, 6, 7].map((i) =>
                object('crate' + i, 'Crate' + i, place(i * 0.15, 0.5 + i, 0), 'cube',
                    getColor(Settings.PALETTE[i % Settings.PALETTE.length]),
                    collider('crate' + i, 'dynamic', 'box', {
                        size: [1, 1, 1]
                    }))),

            // And a ball dropped on top of it: a different shape, the same component.
            object('ball', 'Ball', place(0.9, 13, 0), 'ball', getColor('#ffffff'),
                collider('ball', 'dynamic', 'sphere', {
                    radius: 0.5
                })),
        ],
    },
};

// parseSceneDoc reads the file and fills in everything it left out; sceneFromDoc turns the result
// into an ordinary scene function. A document IS a scene function, which is why it inherits
// changing, pausing and transitions by not being special.
```

`scenes/demo.ts`:

```ts
import { parseSceneDoc, sceneFromDoc } from 'nacatamalon/files';
import { doc } from '../data/room';

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

`settings.ts`:

```ts
export const WIDTH = 480;
export const HEIGHT = 320;
export const PALETTE = ['#4cff91', '#ff6b6b', '#5db6ff', '#ffd93d', '#c77dff', '#ff9f45'];
```
