# A scene inside a scene

> A scene is an object: the one the scene system runs is the one drawn, and with useSpawn it is one more part of another.

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

A scene is an object. The one the scene system runs is the one that is drawn; spawned inside another scene with `useSpawn`, it is one more object in that scene, like a prefab. The first two games run `Room` (written in code) and `Sign` (a scene document, through `sceneFromDoc`) as scenes of their own. The third runs `Town`, which spawns two rooms and a sign as parts of itself.

One rule to know: **a camera belongs to the scene that is running**, and a second one replaces the first. A scene meant to be reused should not declare a camera, or spawning it would take over the host's view.

## Code

`index.ts`:

```ts
import { Room } from './scenes/room';
import { Sign } from './scenes/sign';
import { Town } from './scenes/town';
import { startPanel } from './host/start_panel';

const root = document.querySelector<HTMLElement>('#app');

if (root === null) {
    throw new Error('The example needs an #app container.');
}

root.style.cssText = 'display:flex;flex-wrap:wrap;gap:12px';
startPanel(root, 'Room, as a scene', {
    Room
});
startPanel(root, 'Sign (a document), as a scene', {
    Sign
});
startPanel(root, 'Town: two Rooms and a Sign inside it', {
    Town
});
```

`data/sign.ts`:

```ts
// The same idea as a scene DOCUMENT, the kind the editor saves: a signpost with a board, a post
// and a picture. Nothing in it knows whether it will be a scene or a part of one.
export const SIGN = {
    format: 'nacatamalon-scene',
    version: 1,
    name: 'Sign',
    assets: [
        {
            type: 'texture',
            key: 'gammix',
            src: '/example-assets/assets/gammix.png'
        }
    ],
    root: {
        id: 'sign',
        name: 'Sign',
        transform: {
            x: 100,
            y: 75
        },
        components: [
            {
                type: 'sprite',
                id: 'post',
                texture: null,
                width: 6,
                height: 40,
                tint: {
                    r: 0.36,
                    g: 0.25,
                    b: 0.18,
                    a: 1
                },
                transform: {
                    y: 12
                }
            },
            {
                type: 'sprite',
                id: 'board',
                texture: null,
                width: 64,
                height: 24,
                tint: {
                    r: 0.85,
                    g: 0.66,
                    b: 0.35,
                    a: 1
                }
            },
        ],
        children: [
            {
                id: 'face',
                name: 'Face',
                transform: {
                    x: 0,
                    y: 0
                },
                components: [
                    {
                        type: 'sprite',
                        id: 'picture',
                        texture: 'gammix',
                        width: 16,
                        height: 16,
                        tint: {
                            r: 1,
                            g: 1,
                            b: 1,
                            a: 1
                        }
                    },
                ],
                children: [],
            },
        ],
    },
};
```

`host/start_panel.ts`:

```ts
import { createGame, type TSceneFn } from 'nacatamalon';
import { WIDTH, HEIGHT, BACKGROUND } from '../settings';

export const startPanel = (root: HTMLElement, label: string, scenes: Record<string, TSceneFn>) => {
    const wrap = document.createElement('div');

    wrap.style.cssText = 'display:flex;flex-direction:column;gap:4px';

    const tag = document.createElement('div');

    tag.textContent = label;
    tag.style.cssText = 'font:11px ui-monospace,monospace;color:#8ee6a0;letter-spacing:.08em';

    const host = document.createElement('div');

    wrap.append(tag, host);
    root.appendChild(wrap);

    const game = createGame(host, {
        width: WIDTH,
        height: HEIGHT,
        background: BACKGROUND
    });

    // Declare Scenes
    game(scenes);
};
```

`scenes/room.ts`:

```ts
import { ROOM } from '../settings';
import { createScene, createSprite, useLoadTexture, useTransform, useUpdate } from 'nacatamalon';

// A room, written in code. It is a whole scene (it ends in createScene) and it is also what Town
// spawns twice. Everything below is placed from the room's own box, so wherever the box goes, the
// room goes with it.
//
// Note what it does NOT do: declare a camera. A camera belongs to the scene that is running, and a
// second one replaces the first, so a scene spawned inside another would take over the host's
// view (it says so in the console). A scene meant to be reused leaves the camera to its host.
export const Room = ({ x = ROOM.x, y = ROOM.y }: { x?: number; y?: number } = {
}) => {
    useTransform({
        x,
        y
    });

    createSprite({
        width: 76,
        height: 56,
        tint: '#6b4a3a'
    });
    createSprite({
        width: 64,
        height: 36,
        tint: '#e8d8b0',
        transform: {
            y: -4
        }
    });
    createSprite({
        width: 76,
        height: 8,
        tint: '#3a2a20',
        transform: {
            y: 24
        }
    });
    createSprite({
        width: 14,
        height: 14,
        tint: '#7fc8f8',
        transform: {
            x: -20,
            y: -8
        }
    });
    createSprite({
        width: 14,
        height: 14,
        tint: '#7fc8f8',
        transform: {
            x: 20,
            y: -8
        }
    });

    const hero = createSprite({
        texture: useLoadTexture({
            src: '/example-assets/assets/gammix.png'
        }),
        width: 18,
        height: 18,
        transform: {
            y: 10
        }
    });

    useUpdate((_delta, time) => {
        hero.transform.x = Math.sin(time * ROOM.speed) * ROOM.travel;
    });

    return createScene();
};
```

`scenes/sign.ts`:

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

export const Sign = sceneFromDoc(parseSceneDoc(SIGN, 'sign.scene'));
```

`scenes/town.ts`:

```ts
import { createScene, useSpawn } from 'nacatamalon';
import { Room } from './room';
import { Sign } from './sign';

export const Town = () => {
    // A scene body is a component body, so useSpawn takes it as it is. Room takes its place as
    // props; run as a scene it gets none and uses its defaults, the middle of the screen.
    const spawnRoom = useSpawn(Room);

    spawnRoom({
        x: 45,
        y: 95
    });
    spawnRoom({
        x: 155,
        y: 95
    });

    // A document too: sceneFromDoc builds its root on whichever box is being built, which here is
    // the new one. The document says where its root goes; moving that box moves all of it.
    const sign = useSpawn(Sign)();

    if (sign.transform) {
        sign.transform.y = 34;
    }

    return createScene();
};
```

`settings.ts`:

```ts
export const WIDTH = 200;
export const HEIGHT = 150;
export const BACKGROUND = '#141826';
export const ROOM = {
    x: WIDTH / 2,
    y: HEIGHT / 2,
    speed: 1.4,
    travel: 22
};
```
