# Using a pack

> A pack loaded like any asset (useLoadPack) and placed twice (createPack), each copy with its own place and settings.

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

A `.nacapack` is what one project offers another, with everything it needs. Installed, it is a folder (`packs/signpost/`), and a game loads it the way it loads a texture or a model: `useLoadPack` hands it back at once and fills it in when it lands, and `createPack` places a copy of what it offers.

Each copy has its own place and its own settings (`props`), and the pack's input action is added to the game for you: press **E** and both signposts jump. Its scripts are code, so they come in with one import of `packs/signpost/signpost.pack.ts`.

## Code

`index.ts`:

```ts
import { createGame } from 'nacatamalon';
import { WIDTH, HEIGHT } from './settings';
import { Plaza } from './scenes/demo';
import './packs/signpost/signpost.pack';

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

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

`components/content.ts`:

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

export const Content = () => {
    // Loaded like a texture or a model: handed back at once, filled in when it lands.
    const signpost = useLoadPack({
        src: '/example-assets/packs/signpost'
    });

    createSprite({
        width: 320,
        height: 68,
        tint: '#48733f',
        transform: {
            x: 160,
            y: 190
        }
    });

    // Two copies of the one thing it offers. Each has its own place and its own settings: the
    // second one bobs twice as high. Press E and both jump: the pack brought its 'talk' action.
    createPack({
        pack: signpost,
        transform: {
            x: 100,
            y: 120
        }
    });
    createPack({
        pack: signpost,
        transform: {
            x: 220,
            y: 120
        },
        props: {
            bob: 6
        }
    });
};
```

`packs/signpost/scripts/signpost_talk.ts`:

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

/**
 * Makes a signpost bob gently, and jump when the player presses `talk`.
 *
 * It is the script the `signpost` pack carries, and it is written to show the two things a pack
 * has to get right about a script: it names an **input action** (`talk`), which is the project's
 * and so travels as a requirement rather than a file, and it keeps a **path in a constant**, which
 * the analyzer finds by reading the string and the import rewrites to the pack's folder.
 */
const PICTURE = 'packs/signpost/assets/textures/gammix.png';

export default (self: TGameObject, props: {
    /** @label Bob height (px) @default 3 */
    bob: number;
}) => {
    const talk = useAction('talk');
    const home = self.transform?.y ?? 0;
    let elapsed = 0;
    let jump = 0;

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

        elapsed += delta;

        if (talk.justPressed()) {
            jump = 0.25;
        }

        jump = Math.max(0, jump - delta);
        self.transform.y = home + Math.sin(elapsed * 2) * props.bob - jump * 40;
    });

    // Kept so the path is part of the script, the way a real one keeps its art in a constant.
    void PICTURE;
};
```

`packs/signpost/signpost.pack.ts`:

```ts
// Registers the scripts of the "signpost" pack. Import it once in a game written in code:
//   import './packs/signpost/signpost.pack';
// Written when the pack was made. The editor and an exported game do not need it.
import { registerScript } from 'nacatamalon';
import script_signpost_talk from './scripts/signpost_talk';

registerScript('signpost:signpost_talk', script_signpost_talk);
```

`scenes/demo.ts`:

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

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

    return createScene();
};
```

`settings.ts`:

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