# Sprite animation

> Frame animation from an atlas, as a grid or as named rectangles, with looping and one-shot clips.

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

`createSpriteAtlas` slices a texture into frames: a uniform **grid** (left) or named **JSON** rectangles (middle), both reduced to the same frame table. `useSpriteAnimation` cycles them, writing onto the sprite the part of the picture each moment needs.

The one on the right plays an `attack` clip once on Space, which does not loop: when `playing` turns false it goes back to walking. Any of them can go into `createSpriteTexture` to animate on a 3D screen.

## Code

`index.ts`:

```ts
import { createGame } from 'nacatamalon';
import { Showcase } from './scenes/showcase';
import { ACTIONS, WIDTH, HEIGHT } from './settings';

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

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

`components/fighter.ts`:

```ts
import { createSprite, useAction, useSpriteAnimation, useUpdate } from 'nacatamalon';
import type { TSpriteAtlas } from 'nacatamalon';
import { CHARACTER_Y, CLIPS, SPRITE_SIZE } from '../settings';

type TFighterProps = {
    walkSheet: TSpriteAtlas;
    attackSheet: TSpriteAtlas;
};

export const Fighter = ({ walkSheet, attackSheet }: TFighterProps) => {
    const sprite = createSprite({
        atlas: walkSheet,
        width: SPRITE_SIZE,
        height: SPRITE_SIZE,
        transform: {
            x: 390,
            y: CHARACTER_Y,
            scaleX: -1
        }
    });

    const attack = useAction('attack');

    const animation = useSpriteAnimation(sprite, {
        clips: CLIPS,
        play: 'walk'
    });

    // One player owns both clips. The atlas changes with the clip because these are two images.
    animation.onEnd(() => {
        sprite.atlas = walkSheet;
        animation.play('walk');
    });

    useUpdate(() => {
        if (attack.justPressed() && animation.clip !== 'attack') {
            sprite.atlas = attackSheet;
            animation.play('attack');
        }
    });
};
```

`components/walker.ts`:

```ts
import { createSprite, useSpriteAnimation } from 'nacatamalon';
import type { TSpriteAtlas } from 'nacatamalon';
import { CHARACTER_Y, CLIPS, SPRITE_SIZE } from '../settings';

type TWalkerProps = {
    atlas: TSpriteAtlas;
    x: number;
    speed: number;
};

export const Walker = ({ atlas, x, speed }: TWalkerProps) => {
    const sprite = createSprite({
        atlas,
        width: SPRITE_SIZE,
        height: SPRITE_SIZE,
        transform: {
            x,
            y: CHARACTER_Y
        }
    });

    useSpriteAnimation(sprite, {
        clips: {
            walk: CLIPS.walk
        },
        play: 'walk',
        speed
    });
};
```

`scenes/showcase.ts`:

```ts
import { createScene, createSprite, createSpriteAtlas, useLoadTexture, useSpawn } from 'nacatamalon';
import { Fighter } from '../components/fighter';
import { Walker } from '../components/walker';
import { SHEETS, WALK_SPEEDS, WIDTH } from '../settings';

export const Showcase = () => {
    // Frames are fractions of an image: the sheets can be sliced before the textures arrive.
    const walkSheet = createSpriteAtlas({
        texture: useLoadTexture({
            src: SHEETS.walk
        }),
        columns: SHEETS.columns
    });

    const attackSheet = createSpriteAtlas({
        texture: useLoadTexture({
            src: SHEETS.attack
        }),
        columns: SHEETS.columns
    });

    const spawnWalker = useSpawn(Walker);

    for (const [index, speed] of WALK_SPEEDS.entries()) {
        spawnWalker({
            atlas: walkSheet,
            x: 90 + index * 100,
            speed
        });
    }

    useSpawn(Fighter)({
        walkSheet,
        attackSheet
    });

    // The whole sheet below the characters makes the six source pictures visible.
    createSprite({
        texture: walkSheet.texture,
        width: 288,
        height: 48,
        transform: {
            x: WIDTH / 2,
            y: 250
        }
    });

    return createScene();
};
```

`settings.ts`:

```ts
import type { TActionMap } from 'nacatamalon';

export const WIDTH = 480;
export const HEIGHT = 320;
export const SPRITE_SIZE = 96;
export const CHARACTER_Y = 110;
export const WALK_SPEEDS = [0.6, 1.2, 1.8];

export const SHEETS = {
    walk: '/example-assets/assets/charracters/business_enemies/1/Walk.png',
    attack: '/example-assets/assets/charracters/business_enemies/1/Attack.png',
    columns: 6
};

export const CLIPS = {
    walk: {
        frames: [0, 1, 2, 3, 4, 5],
        fps: 10
    },
    attack: {
        frames: [0, 1, 2, 3, 4, 5],
        fps: 14,
        loop: false
    }
};

export const ACTIONS: TActionMap = [
    {
        name: 'attack',
        bindings: [
            {
                type: 'key',
                key: ' '
            },
            {
                type: 'button',
                button: 'x'
            }
        ]
    }
];
```
