# Scene from a document

> The same room from JSON and by hand, side by side: sceneFromDoc gives back an ordinary scene function.

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

The same room twice: on the left built from a JSON document with `sceneFromDoc`, on the right written by hand. They draw the same thing, and that is the whole claim the format makes.

`sceneFromDoc` gives back an **ordinary scene function**: from that point nothing in the engine knows where it came from, so starting it, changing to it, covering that change with a transition and pausing it are not features the format had to add. And because its body calls the same `createSprite` you would have called, the format cannot express anything the engine's API cannot build.

## Code

`index.ts`:

```ts
import { createGame } from 'nacatamalon';
import * as Settings from './settings';
import { ByHand } from './scenes/by_hand';
import { FromDocument } from './scenes/from_document';

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

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

`data/room.ts`:

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

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

const block = (id: string, x: number, y: number) => ({
    id: id,
    name: 'Block',
    transform: place(x, y),
    components: [
        {
            type: 'sprite',
            id: id + '-art',
            texture: null,
            tint: {
                r: 0.37,
                g: 0.56,
                b: 0.85,
                a: 1
            },
            width: 48,
            height: 32,
            anchor: {
                x: 0,
                y: 0
            },
        }
    ],
    children: [],
});

export const ROOM = {
    format: 'nacatamalon-scene',
    version: 1,
    name: 'FromDocument',
    // Everything to fetch, by the name the tree refers to it by. Walked BEFORE a single box is
    // built, so by the time the text below says 'my font is pac', pac is already on its way.
    assets: [
        {
            type: 'bitmap-font',
            key: 'pac',
            json: Settings.ARCADE.json,
            atlas: Settings.ARCADE.atlas
        }
    ],
    root: {
        id: 'room-root',
        name: 'FromDocument',
        // null means 'not anywhere in particular', which is what a box used only to group things
        // should cost: nothing.
        transform: null,
        components: [],
        children: [
            {
                id: 'floor',
                name: 'Floor',
                transform: place(20, 40),
                components: [
                    {
                        type: 'sprite',
                        id: 'floor-art',
                        texture: null,
                        tint: {
                            r: 0.11,
                            g: 0.17,
                            b: 0.30,
                            a: 1
                        },
                        width: 200,
                        height: 200,
                        anchor: {
                            x: 0,
                            y: 0
                        },
                    }
                ],
                children: [],
            },
            block('b1', 36, 60),
            block('b2', 100, 60),
            block('b3', 36, 110),
            block('b4', 100, 110),
            {
                id: 'label',
                name: 'Label',
                transform: place(36, 190),
                components: [
                    {
                        type: 'text',
                        id: 'label-art',
                        text: 'FROM JSON',
                        font: 'pac',
                        style: {
                            fontSize: 8
                        },
                        tint: {
                            r: 0.56,
                            g: 0.72,
                            b: 1,
                            a: 1
                        },
                    }
                ],
                children: [],
            },
        ],
    },
};
```

`scenes/by_hand.ts`:

```ts
import { createScene, createSprite, createText, useLoadBitmapFont, useScene } from 'nacatamalon';

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

export const ByHand = () => {
    const font = useLoadBitmapFont(Settings.ARCADE);
    const scene = useScene();

    createSprite({
        tint: Settings.FLOOR,
        width: 200,
        height: 200,
        anchor: {
            x: 0,
            y: 0
        },
        transform: {
            x: 260,
            y: 40
        }
    });
    createSprite({
        tint: Settings.BLOCK,
        width: 48,
        height: 32,
        anchor: {
            x: 0,
            y: 0
        },
        transform: {
            x: 276,
            y: 60
        }
    });
    createSprite({
        tint: Settings.BLOCK,
        width: 48,
        height: 32,
        anchor: {
            x: 0,
            y: 0
        },
        transform: {
            x: 340,
            y: 60
        }
    });
    createSprite({
        tint: Settings.BLOCK,
        width: 48,
        height: 32,
        anchor: {
            x: 0,
            y: 0
        },
        transform: {
            x: 276,
            y: 110
        }
    });
    createSprite({
        tint: Settings.BLOCK,
        width: 48,
        height: 32,
        anchor: {
            x: 0,
            y: 0
        },
        transform: {
            x: 340,
            y: 110
        }
    });
    createText({
        text: 'BY HAND',
        font,
        style: {
            fontSize: 8
        },
        tint: Settings.LABEL,
        transform: {
            x: 276,
            y: 190
        }
    });

    // Started alongside, not instead: two scenes stack in the order they were started.
    scene.launch('FromDocument');

    return createScene();
};
```

`scenes/from_document.ts`:

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

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

`settings.ts`:

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

export const WIDTH = 480;
export const HEIGHT = 260;
export const ARCADE = {
    json: '/example-assets/fonts/bitmaps/nacatamal-arcade/nacatamal-arcade.json',
    atlas: '/example-assets/fonts/bitmaps/nacatamal-arcade/nacatamal-arcade.png'
};
export const LABEL = getColor('#8fb7ff');
export const BLOCK = getColor('#5f8fd8');
export const FLOOR = getColor('#1d2b4d');
// A transform in a document is the full nine fields, and any of them may be left out: what is
// missing falls back to identity, so { "x": 40 } is a box forty across and otherwise ordinary.
```
