# Pack: logic and scenes

> A pack offering a scene with its logic: a room to walk and pick up coins, placed twice inside another scene.

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

A pack that offers a **scene**: a room where you walk and pick up three coins, and the three scripts that make it a game (the player, the coins and the counter). It uses four input actions of its own, which the game gets when the pack lands.

Placed with `createPack`, a scene becomes one part of the scene that places it, like any object: here it is made smaller and placed twice, and each room keeps its own coins. Use the **arrow keys**.

## Code

`index.ts`:

```ts
import { createGame } from 'nacatamalon';
import { WIDTH, HEIGHT } from './settings';
import { Arcade } from './scenes/demo';
import './packs/coin-room/coin-room.pack';

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

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

`components/content.ts`:

```ts
import { createPack, createText, useLoadBitmapFont, useLoadPack } from 'nacatamalon';

export const Content = () => {
    const room = useLoadPack({
        src: '/example-assets/packs/coin-room'
    });

    const font = useLoadBitmapFont({
        json: '/example-assets/fonts/bitmaps/nacatamal-arcade/nacatamal-arcade.json',
        atlas: '/example-assets/fonts/bitmaps/nacatamal-arcade/nacatamal-arcade.png'
    });

    createText({
        text: 'ARROWS TO MOVE',
        font,
        style: {
            fontSize: 8
        },
        anchor: {
            x: 0.5,
            y: 0
        },
        transform: {
            x: 160,
            y: 12
        }
    });

    // A copy is an ordinary object: this one is placed and made smaller like anything else.
    createPack({
        pack: room,
        scene: 'coin-room',
        transform: {
            x: 8,
            y: 50,
            scaleX: 0.72,
            scaleY: 0.72
        }
    });
    createPack({
        pack: room,
        scene: 'coin-room',
        transform: {
            x: 168,
            y: 50,
            scaleX: 0.72,
            scaleY: 0.72
        }
    });
};
```

`packs/coin-room/coin-room.pack.ts`:

```ts
// Registers the scripts of the "coin-room" pack. Import it once in a game written in code:
//   import './packs/coin-room/coin-room.pack';
// Written when the pack was made. The editor and an exported game do not need it.
import { registerScript } from 'nacatamalon';
import script_coin from './scripts/coin';
import script_coin_counter from './scripts/coin_counter';
import script_coin_player from './scripts/coin_player';

registerScript('coin-room:coin', script_coin);
registerScript('coin-room:coin_counter', script_coin_counter);
registerScript('coin-room:coin_player', script_coin_player);
```

`packs/coin-room/scripts/coin.ts`:

```ts
import { findObject, useUpdate, type TGameObject } from 'nacatamalon';

/**
 * A coin of the `coin-room` pack: bobs until the player touches it, then goes.
 *
 * It finds the player by name among its own siblings, the room's other boxes, so the room works the
 * same run as a scene of its own or placed inside another one.
 */
export default (self: TGameObject) => {
    const home = self.transform?.y ?? 0;
    let elapsed = Math.random() * 6;

    useUpdate((delta) => {
        if (!self.transform || !self.visible || self.parent === null) {
            return;
        }

        elapsed += delta;
        self.transform.y = home + Math.sin(elapsed * 3) * 2;

        const player = findObject(self.parent, 'Player');

        if (player?.transform && Math.hypot(player.transform.x - self.transform.x, player.transform.y - self.transform.y) < 12) {
            self.visible = false;
        }
    });
};
```

`packs/coin-room/scripts/coin_counter.ts`:

```ts
import { findObjects, useUpdate, type TGameObject } from 'nacatamalon';

/**
 * The counter of the `coin-room` pack: how many of the room's coins are gone.
 */
export default (self: TGameObject) => {
    const text = self.drawables.find((drawable) => drawable.type === 'text');

    useUpdate(() => {
        if (text === undefined || text.type !== 'text' || self.parent === null) {
            return;
        }

        const coins = findObjects(self.parent, 'Coin');
        const taken = coins.filter((coin) => !coin.visible).length;

        text.text = taken === coins.length ? 'ALL COINS!' : `COINS ${taken}/${coins.length}`;
    });
};
```

`packs/coin-room/scripts/coin_player.ts`:

```ts
import { useAction, useUpdate, type TGameObject } from 'nacatamalon';

/**
 * The player of the `coin-room` pack: walks with the arrow keys inside the room, and no further.
 *
 * It reads four input actions, which are the project's. They travel with the pack as requirements,
 * and a game that loads it gets them added if it lacks them.
 */
export default (self: TGameObject, props: {
    /** @label Speed (px/s) @default 70 */
    speed: number;
}) => {
    const left = useAction('move_left');
    const right = useAction('move_right');
    const up = useAction('move_up');
    const down = useAction('move_down');

    useUpdate((delta) => {
        if (!self.transform) {
            return;
        }

        const dx = (right.isDown() ? 1 : 0) - (left.isDown() ? 1 : 0);
        const dy = (down.isDown() ? 1 : 0) - (up.isDown() ? 1 : 0);

        // The room is 200 by 120, measured from its own corner, wherever the room itself is placed.
        self.transform.x = Math.min(190, Math.max(10, self.transform.x + dx * props.speed * delta));
        self.transform.y = Math.min(110, Math.max(10, self.transform.y + dy * props.speed * delta));
    });
};
```

`scenes/demo.ts`:

```ts
import { createScene, useSpawn } from 'nacatamalon';
import { Content } from '../components/content';

export const Arcade = () => {
    useSpawn(Content)();

    return createScene();
};
```

`settings.ts`:

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