# Bunnymark

> The classic stress test: hundreds of sprites bouncing at once to gauge how much the 2D path can take.

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

The classic sprite stress test: 300 sprites sharing one texture, each bouncing off the edges with its own velocity. Every one is an object of its own, made with `useSpawn` and moved by a `useUpdate` that belongs to it, so this measures the whole path and not just the renderer. Click the canvas and **hold Space** to keep adding 50 per frame until it gives.

## Code

`index.ts`:

```ts
import { createGame } from 'nacatamalon';
import { Bunnies } from './scenes/bunnies';
import { WIDTH, HEIGHT, BACKGROUND } from './settings';

const game = createGame('#app', {
    width: WIDTH,
    height: HEIGHT,
    background: BACKGROUND,
    seed: 1
});

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

`components/bunny.ts`:

```ts
import { useRandom } from 'nacatamalon/math';
import { createSprite, useUpdate } from 'nacatamalon';
import type { TTexture } from 'nacatamalon';
import { WIDTH, HEIGHT, BUNNY_SIZE } from '../settings';

// One bunny: its own sprite, its own velocity, its own per-frame code. Made through a spawner,
// so each one is a thing of its own rather than an entry in a list the scene walks.
export const Bunny = (texture: TTexture) => {
    const random = useRandom();

    const sprite = createSprite({
        texture,
        width: BUNNY_SIZE,
        height: BUNNY_SIZE,
        transform: {
            x: random.rand(BUNNY_SIZE / 2, WIDTH - BUNNY_SIZE / 2),
            y: random.rand(BUNNY_SIZE / 2, HEIGHT - BUNNY_SIZE / 2),
        },
    });

    let vx = random.rand(-100, 100);
    let vy = random.rand(-100, 100);

    useUpdate((delta) => {
        sprite.transform.x += vx * delta;
        sprite.transform.y += vy * delta;

        // Placed by its centre, so the wall is half a bunny in from the edge.
        if (sprite.transform.x < BUNNY_SIZE / 2 || sprite.transform.x > WIDTH - BUNNY_SIZE / 2) {
            vx *= -1;
        }

        if (sprite.transform.y < BUNNY_SIZE / 2 || sprite.transform.y > HEIGHT - BUNNY_SIZE / 2) {
            vy *= -1;
        }
    });
};
```

`scenes/bunnies.ts`:

```ts
import { createScene, useKeyboard, useLoadTexture, useSpawn, useUpdate } from 'nacatamalon';
import { Bunny } from '../components/bunny';
import { BUNNY_COUNT } from '../settings';

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

    const spawnBunny = useSpawn(Bunny);
    const keys = useKeyboard();

    const add = (howMany: number) => {
        for (let i = 0; i < howMany; i++) {
            spawnBunny(texture);
        }
    };

    add(BUNNY_COUNT);

    useUpdate(() => {
        if (keys.isDown(' ')) {
            add(50);
        }
    });

    return createScene();
};
```

`settings.ts`:

```ts
export const WIDTH = 480;
export const HEIGHT = 320;
export const BUNNY_SIZE = 16;
export const BUNNY_COUNT = 300;
export const BACKGROUND = '#0f1b2d';
```
