# Sprite features

> flipX mirrors it, anchor moves the pivot, and an atlas shows only part of the picture.

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

Three things a sprite can do beyond being somewhere. `flipX` mirrors it each second with one drawing instead of two, and it mirrors the **picture**: the sprite stays exactly where it is, unlike a negative scale, which turns the quad and drags the sprite (and the place it can be clicked) with it.

`anchor` moves the pivot to the bottom edge so it swings like a sign. And `atlas` with `frame` show one part of a sprite sheet.

## Code

`index.ts`:

```ts
import { createGame } from 'nacatamalon';
import * as Settings from './settings';
import { Game } from './scenes/demo';

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

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

`components/frames.ts`:

```ts
import { createSprite, createSpriteAtlas, useLoadTexture } from 'nacatamalon';

export const Frames = () => {
    const walk = createSpriteAtlas({
        texture: useLoadTexture({
            src: '/example-assets/assets/charracters/business_enemies/1/Walk.png',
            key: 'walk'
        }),
        columns: 6,
    });

    // --- one frame of a sheet ---
    // Two sprites reading the same image, each showing a different sixth of it. No width or
    // height: a sprite is the size of what it shows, which is one frame and not the whole sheet.
    createSprite({
        atlas: walk,
        frame: 0,
        transform: {
            x: 375,
            y: 100,
            rotation: 0,
            scaleX: 1,
            scaleY: 1
        }
    });
    createSprite({
        atlas: walk,
        frame: 3,
        transform: {
            x: 430,
            y: 100,
            rotation: 0,
            scaleX: 1,
            scaleY: 1
        }
    });
};
```

`components/mirrored.ts`:

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

import type { TTexture } from 'nacatamalon';

export const Mirrored = ({ texture }: { texture: TTexture; }) => {
    // --- mirrored ---
    // flipX reads the window into the image backwards, so the picture turns over and the sprite
    // does not move a pixel. A negative scaleX looks the same here and is not the same thing: it
    // turns the QUAD, so with an anchor that is not centred the sprite shifts sideways as it
    // flips, and the place it can be clicked shifts with it.
    const mirrored = createSprite({
        texture,
        width: 64,
        height: 64,
        transform: {
            x: 80,
            y: 100,
            rotation: 0,
            scaleX: 1,
            scaleY: 1
        },
    });

    useUpdate((_delta, time) => {
        mirrored.flipX = Math.floor(time) % 2 === 1;
    });
};
```

`components/pendulum.ts`:

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

import type { TTexture } from 'nacatamalon';

export const Pendulum = ({ texture }: { texture: TTexture; }) => {
    // --- anchor ---
    // Hung from the middle of its bottom edge. Rotation happens around that point, so it swings
    // like a sign in the wind rather than spinning where it stands.
    const pendulum = createSprite({
        texture,
        width: 64,
        height: 64,
        anchor: {
            x: 0.5,
            y: 1
        },
        transform: {
            x: 240,
            y: 90,
            rotation: 0,
            scaleX: 1,
            scaleY: 1
        },
    });

    // The point it hangs from, drawn as a small square so the pivot is visible.
    createSprite({
        width: 6,
        height: 6,
        tint: '#ffe066',
        transform: {
            x: 240,
            y: 90,
            rotation: 0,
            scaleX: 1,
            scaleY: 1
        },
    });

    useUpdate((_delta, time) => {
        pendulum.transform.rotation = Math.sin(time * 2) * 0.8;
    });
};
```

`scenes/demo.ts`:

```ts
import { createScene, createSprite, useLoadTexture, useSpawn } from 'nacatamalon';

import { Mirrored } from '../components/mirrored';
import { Pendulum } from '../components/pendulum';
import { Frames } from '../components/frames';

export const Game = () => {
    const texture = useLoadTexture({
        src: '/example-assets/assets/gammix.png',
        key: 'gammix'
    });

    useSpawn(Mirrored)({
        texture
    });
    useSpawn(Pendulum)({
        texture
    });
    useSpawn(Frames)();
    // Three underlines, untextured quads whose tint carries its own opacity.
    [80, 240, 400].forEach((x) => {
        createSprite({
            width: 96,
            height: 4,
            tint: {
                r: 1,
                g: 1,
                b: 1,
                a: 0.25
            },
            transform: {
                x,
                y: 190,
                rotation: 0,
                scaleX: 1,
                scaleY: 1
            },
        });
    });

    return createScene();
};
```

`settings.ts`:

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