# First scene

> A scene with a sprite and a useUpdate, spread across files: the anatomy of every scene.

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

The anatomy of every scene, spread across files. `index.ts` creates the game with the scenes it knows, by name. `scenes/demo.ts` is the scene: a function that runs once. And `components/player.ts` is the player.

The scene creates the player with `useSpawn(Player)()`. The player has its own object: its sprite and `useUpdate` belong to it, and the scene composes that object. `settings.ts` holds the dimensions and speed. A game is built this way, from small pieces that can move from one file to another.

## Code

`index.ts`:

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

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

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

`components/player.ts`:

```ts
import { createSprite, useUpdate } from 'nacatamalon';
import { WIDTH, HEIGHT, PLAYER } from '../settings';

export const Player = () => {
    const player = createSprite({
        width: PLAYER.size,
        height: PLAYER.size,
        transform: {
            x: PLAYER.startX,
            y: HEIGHT / 2
        }
    });

    useUpdate((delta) => {
        // Delta is seconds: the speed stays the same on different displays.
        player.transform.x += PLAYER.speed * delta;

        if (player.transform.x > WIDTH + PLAYER.size / 2) {
            player.transform.x = -PLAYER.size / 2;
        }
    });
};
```

`scenes/demo.ts`:

```ts
import { createScene, useSpawn } from 'nacatamalon';
import { Player } from '../components/player';

export const Game = () => {
    // The component gets its own object, including the sprite and update it registers.
    useSpawn(Player)();

    return createScene();
};
```

`settings.ts`:

```ts
export const WIDTH = 480;
export const HEIGHT = 320;

export const PLAYER = {
    size: 32,
    speed: 100,
    startX: 32
};
```
