# Bounce ladder

> Six balls from bounce 0 to 1 on a floor that asks for nothing: a retro body's bounce is its own, never averaged with what it hits.

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

How much of its speed a retro body keeps when something stops it is its own `bounce`, from 0 to 1, and it is not mixed with what it hits. Every ball lands on the same floor, which asks for no bounce at all, and each one goes back up exactly as high as its own number says.

A ball pressed into the floor with too little speed left to leave it stops bouncing, so the low ones come to rest instead of trembling for ever. With `bounce: 1` nothing is lost, and that ball goes back to where it started, every time. **Click** to drop them again.

## Code

`index.ts`:

```ts
import { createGame } from 'nacatamalon';
import { installRetroPhysics2d } from 'nacatamalon/retro-physics2d';

import * as Settings from './settings';
import { Scene } from './scenes/demo';

// The one line that makes retro bodies move. Nothing to load: no WebAssembly, no waiting.
installRetroPhysics2d();

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

// Declare Scenes
game({
    Scene
}, 'Scene');
```

`art/ball.ts`:

```ts
import { createPixels, createPixelTexture, fillCircle } from 'nacatamalon/pixels';

// A white disc, painted once for the scene and tinted per ball. Odd-sized so it has a middle
// pixel and comes out round.
export const paintBall = (size: number) => {
    const side = size | 1;
    const pixels = createPixels(side, side);
    fillCircle(pixels, (side - 1) / 2, (side - 1) / 2, (side - 1) / 2, '#ffffff');
    return createPixelTexture(pixels);
};
```

`components/ball.ts`:

```ts
import { createSprite, createText, useRetroBody2d, useSelf } from 'nacatamalon';
import { getRetroBody2d } from 'nacatamalon/retro-physics2d';
import type { TTexture } from 'nacatamalon';

import * as Settings from '../settings';

export const Ball = (x: number, bounce: number, color: string, picture: TTexture) => {
    const sprite = createSprite({
        texture: picture,
        tint: color,
        width: Settings.R * 2,
        height: Settings.R * 2,
        transform: {
            x,
            y: Settings.START_Y
        }
    });
    useRetroBody2d({
        body: 'dynamic',
        collider: {
            shape: 'circle',
            radius: Settings.R
        },
        bounce,
    });
    createText({
        text: bounce.toFixed(1),
        transform: {
            x,
            y: 24
        }
    });

    const self = useSelf();

    // Back to the top, still: moving the object moves its body, with nothing else to tell.
    return {
        drop: () => {
            sprite.transform.y = Settings.START_Y;
            getRetroBody2d(self)?.setLinearVelocity(0, 0);
        },
    };
};
```

`components/solid.ts`:

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

// A wall or the floor: a body that never moves, the size of its picture.
export const Solid = (x: number, y: number, w: number, h: number) => {
    createSprite({
        tint: '#2a2a3a',
        width: w,
        height: h,
        transform: {
            x,
            y
        }
    });
    useRetroBody2d({
        body: 'static',
        collider: {
            shape: 'rect',
            width: w,
            height: h
        }
    });
};
```

`scenes/demo.ts`:

```ts
import { createScene, createText, usePointer, useRetroWorld2d, useSpawn } from 'nacatamalon';

import * as Settings from '../settings';
import { Solid } from '../components/solid';
import { Ball } from '../components/ball';
import { paintBall } from '../art/ball';

export const Scene = () => {
    useRetroWorld2d({
        gravity: {
            x: 0,
            y: 700
        }
    });

    // The floor asks for no bounce at all: what each ball does is its own.
    useSpawn(Solid)(Settings.WIDTH / 2, Settings.HEIGHT - Settings.WALL / 2, Settings.WIDTH, Settings.WALL);

    const picture = paintBall(Settings.R * 2);
    const spawnBall = useSpawn(Ball);
    const gap = Settings.WIDTH / (Settings.BOUNCES.length + 1);
    const balls = Settings.BOUNCES.map((bounce, i) => spawnBall(gap * (i + 1), bounce, Settings.PALETTE[i], picture));

    createText({
        text: 'CLICK TO DROP THEM AGAIN',
        transform: {
            x: Settings.WIDTH / 2,
            y: Settings.HEIGHT - 30
        },
        anchor: {
            x: 0.5,
            y: 0
        }
    });

    usePointer().onDown(() => {
        for (const ball of balls) {
            ball.api.drop();
        }
    });

    return createScene();
};
```

`settings.ts`:

```ts
// How much of its speed a retro body keeps when something stops it is its own `bounce`, from 0
// to 1, and it is NOT mixed with what it hits. Every ball here lands on the same floor, which
// asks for nothing, and each one bounces exactly as high as its own number says.
//
// A ball pressed into the floor by gravity with too little speed left to leave it stops
// bouncing, so the low ones come to rest instead of trembling for ever. With bounce: 1 nothing
// is lost, and that ball goes back up to where it started, every time.

export const WIDTH = 480;
export const HEIGHT = 340;
export const WALL = 12;
export const R = 12;
export const START_Y = 60;
export const BOUNCES = [0, 0.2, 0.4, 0.6, 0.8, 1];
export const PALETTE = ['#5a6a8a', '#5db6ff', '#4cff91', '#ffd93d', '#ff9f45', '#ff6b6b'];
```
