# A 2D game on a 3D screen

> A mini 2D game written as ordinary components, knowing nothing about where it is shown, drawn on a panel's screen and a box's skin.

Example `game-to-mesh-screen`, running at https://nacatamalon.com/en/examples/game-to-mesh-screen. The whole catalogue: https://nacatamalon.com/llms/examples.md.

A mini 2D game written as ordinary components, `PetGame` and its children, that knows nothing about where it is shown. `createSpriteTexture` draws it into a 160×120 picture every frame, and that picture is the screen of a panel and the skin of a box.

The same picture sits flat in the corner to compare against.

## Code

`index.ts`:

```ts
import { WIDTH, HEIGHT } from './settings';
import { createGame } from 'nacatamalon';
import { Device } from './scenes/device_scene';

const game = createGame('#app', {
    width: WIDTH,
    height: HEIGHT,
    background: '#11131a',
});

// Declare Scenes
game({
    Device
});
```

`scenes/device_scene.ts`:

```ts
import { createMesh, createScene, createSprite, createSpriteTexture, useCamera3d, useCubeGeometry, useDirectionalLight, usePlaneGeometry, useUpdate } from 'nacatamalon';
import { PetGame } from '../systems/pet_game_2d';

// Two layers of intent:
// 1) A complete mini 2D game, written as ordinary components, drawn into a picture instead of
//    the screen.
// 2) That picture on 3D models, the way a device shows its screen.
export const Device = () => {
    useCamera3d({
        projection: 'perspective',
        fov: 48,
        near: 0.1,
        far: 100,
        transform: {
            y: 0.08,
            z: 4.25
        }
    });
    // A light shines along its own facing, so it is aimed by turning it: down and a little left.
    useDirectionalLight({
        intensity: 1.1,
        ambient: 0.6,
        transform: {
            rotationX: -0.55,
            rotationY: 0.35
        }
    });

    // The whole game lives in its 160x120 pixels. Everything PetGame makes, and everything its
    // children make, ends up here and not on the screen.
    const screen = createSpriteTexture({
        width: 160,
        height: 120,
        background: '#9bbc0f'
    }, PetGame);

    // The same picture flat on the screen, pinned to a corner: the reference the models are
    // compared against. With no 2D camera, a sprite is placed in screen pixels.
    createSprite({
        texture: screen,
        smooth: false,
        width: 80,
        height: 60,
        transform: {
            x: 48,
            y: 38
        },
        zIndex: 100
    });

    // The main screen. A plane lies like a floor with the top of its picture on its far edge, so a
    // quarter turn around X stands it up facing the camera, the right way up and unmirrored.
    const panel = createMesh({
        geometry: usePlaneGeometry({
            width: 1.6,
            depth: 1.2
        }),
        texture: screen,
        smooth: false,
        transform: {
            x: -0.72,
            y: -0.05,
            z: 0.3,
            rotationX: Math.PI / 2
        },
    });

    // The same live picture on a box: every face shows it, so the game wraps the whole device.
    const device = createMesh({
        geometry: useCubeGeometry(),
        texture: screen,
        smooth: false,
        transform: {
            x: 1.18,
            y: -0.38,
            scaleX: 0.64,
            scaleY: 0.48,
            scaleZ: 0.48,
            rotationY: 0.42
        },
    });

    let t = 0;

    useUpdate((delta) => {
        t += delta;
        // The panel only sways side to side, so it stays upright while the perspective shifts.
        panel.transform.rotationY = Math.sin(t * 0.9) * 0.22;

        device.transform.rotationY = 0.42 + Math.sin(t * 0.8) * 0.22;
        device.transform.rotationX = Math.sin(t * 1.05) * 0.14;
        // The turn in the picture's own plane is spelled rotation, as in 2D.
        device.transform.rotation = Math.sin(t * 0.7) * 0.08;
    });

    return createScene();
};
```

`settings.ts`:

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

`systems/pet_game_2d.ts`:

```ts
import { createSprite, getColor, useSpawn, useTransform, useUpdate } from 'nacatamalon';

// A 2D game that knows nothing about where it is shown: nothing here mentions a texture, a mesh or
// a camera. Its coordinates are the pixels of its screen, 160x120, and it would run the same drawn
// straight onto the canvas.

const DARK = getColor('#0f380f');
const MID = getColor('#306230');
const LIGHT = getColor('#8bac0f');
const PALE = getColor('#9bbc0f');

const Frame = () => {
    createSprite({
        tint: MID,
        width: 148,
        height: 108,
        transform: {
            x: 80,
            y: 60
        }
    });
    createSprite({
        tint: LIGHT,
        width: 140,
        height: 100,
        transform: {
            x: 80,
            y: 60
        }
    });
};

// The pet is one object with its face as parts of it: move the object and the eyes and mouth come
// along, so the loop below moves one thing, not four.
const Pet = () => {
    const place = useTransform({
        x: 80,
        y: 63
    });

    createSprite({
        tint: DARK,
        width: 28,
        height: 26,
        zIndex: 1
    });
    createSprite({
        tint: PALE,
        width: 5,
        height: 8,
        transform: {
            x: -5.5,
            y: -3
        },
        zIndex: 2
    });
    createSprite({
        tint: PALE,
        width: 5,
        height: 8,
        transform: {
            x: 5.5,
            y: -3
        },
        zIndex: 2
    });
    createSprite({
        tint: PALE,
        width: 8,
        height: 4,
        transform: {
            x: 0,
            y: 5
        },
        zIndex: 2
    });

    let t = 0;

    useUpdate((delta) => {
        t += delta;
        place.x = 80 + Math.sin(t * 1.1) * 10;
        place.y = 63 + Math.sin(t * 3.2) * 4;
    });
};

const Hud = () => {
    // Two bars and a ticker, growing from their left end.
    const left = {
        x: 0,
        y: 0.5
    };

    createSprite({
        tint: DARK,
        width: 42,
        height: 8,
        transform: {
            x: 35,
            y: 18
        },
        zIndex: 1
    });
    createSprite({
        tint: DARK,
        width: 42,
        height: 8,
        transform: {
            x: 125,
            y: 18
        },
        zIndex: 1
    });
    createSprite({
        tint: DARK,
        width: 132,
        height: 10,
        transform: {
            x: 80,
            y: 99
        },
        zIndex: 1
    });

    const hunger = createSprite({
        tint: MID,
        width: 40,
        height: 6,
        anchor: left,
        transform: {
            x: 15,
            y: 18
        },
        zIndex: 2
    });

    const mood = createSprite({
        tint: MID,
        width: 40,
        height: 6,
        anchor: left,
        transform: {
            x: 105,
            y: 18
        },
        zIndex: 2
    });

    const ticker = createSprite({
        tint: PALE,
        width: 26,
        height: 4,
        anchor: left,
        transform: {
            x: 16,
            y: 99
        },
        zIndex: 2
    });

    let t = 0;

    useUpdate((delta) => {
        t += delta;
        hunger.width = 10 + 30 * (0.5 + 0.5 * Math.sin(t * 0.9));
        mood.width = 10 + 30 * (0.5 + 0.5 * Math.sin(t * 1.25 + 1.1));
        ticker.transform.x = 16 + (t * 22) % 102;
    });
};

export const PetGame = () => {
    useSpawn(Frame)();
    useSpawn(Pet)();
    useSpawn(Hud)();
};
```
