# Falling crates

> A room whose floor, ledge and four crates are a JSON document, colliders and gravity included. The engine keeps the format but cannot move it: one installPhysics2d() is what makes it fall.

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

A room where nothing is written in code: the floor, the ledge and four crates are a JSON document, and so is the gravity. Each object says what it is made of with a `physics-body-2d` component, and the scene says how hard things fall with a `physics-world-2d` on its root. This is what an editor saves.

**The engine cannot move any of it.** It keeps colliders, writes them down and reads them back without being able to simulate them, and the one line that changes that is `installPhysics2d()`. Take it out and the room still opens, still draws and still saves every collider: it just sits still. That is not a degraded mode, it is the mode a level editor runs in.

A collider carries no position. The object owns that, the same as its picture does, which is why moving an object in an editor moves what it collides with. And it is centred on that position, like a sprite.

## Code

`index.ts`:

```ts
import { createGame } from 'nacatamalon';
import { installPhysics2d } from 'nacatamalon/physics2d';
import { Room } from './scenes/demo';
import * as Settings from './settings';

installPhysics2d();

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

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

`data/room.ts`:

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

const WOOD = {
    r: 0.72,
    g: 0.51,
    b: 0.25,
    a: 1
};

const STONE = {
    r: 0.35,
    g: 0.38,
    b: 0.46,
    a: 1
};

// An object that falls: where it is, what it looks like, and what it is made of. The last one is
// the component this example exists for, and it carries no position at all: the object owns that,
// which is why moving it in an editor moves its collider with it.
const crate = (id: string, x: number, y: number, turn: number) => ({
    id,
    name: 'Crate',
    transform: {
        ...place(x, y),
        rotation: turn
    },
    components: [
        {
            type: 'sprite',
            id: id + '-art',
            texture: null,
            tint: WOOD,
            width: 26,
            height: 26
        },
        {
            type: 'physics-body-2d',
            id: id + '-body',
            body: 'dynamic',
            collider: {
                shape: 'rect',
                width: 26,
                height: 26
            },
            friction: 0.6
        },
    ],
    children: [],
});

export const ROOM = {
    format: 'nacatamalon-scene',
    version: 1,
    name: 'Room',
    assets: [],
    root: {
        id: 'room',
        name: 'Room',
        transform: null,
        // On the root, because gravity is the scene's and not any object's: a room seen from above
        // has none and this one does. Pixels per second squared, +y DOWN, which is the way a
        // placement's y grows here.
        components: [
            {
                type: 'physics-world-2d',
                id: 'world',
                gravity: {
                    x: 0,
                    y: 900
                }
            }
        ],
        children: [
            {
                id: 'floor',
                name: 'Floor',
                transform: place(180, 170),
                components: [
                    {
                        type: 'sprite',
                        id: 'floor-art',
                        texture: null,
                        tint: STONE,
                        width: 300,
                        height: 16
                    },
                    {
                        type: 'physics-body-2d',
                        id: 'floor-body',
                        body: 'static',
                        collider: {
                            shape: 'rect',
                            width: 300,
                            height: 16
                        }
                    },
                ],
                children: [],
            },
            {
                id: 'ledge',
                name: 'Ledge',
                transform: {
                    ...place(110, 120),
                    rotation: -0.25
                },
                components: [
                    {
                        type: 'sprite',
                        id: 'ledge-art',
                        texture: null,
                        tint: STONE,
                        width: 120,
                        height: 12
                    },
                    {
                        type: 'physics-body-2d',
                        id: 'ledge-body',
                        body: 'static',
                        collider: {
                            shape: 'rect',
                            width: 120,
                            height: 12
                        }
                    },
                ],
                children: [],
            },
            crate('c1', 150, -40, 0),
            crate('c2', 168, -90, 0.3),
            crate('c3', 140, -140, -0.2),
            crate('c4', 200, -190, 0.1),
        ],
    },
};
```

`scenes/demo.ts`:

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

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

`settings.ts`:

```ts
export const WIDTH = 360;
export const HEIGHT = 200;
```
