# Breakout, retro

> A breakout with no collision code: a kinematic paddle, a ball with bounce 1 and gravityScale 0, bricks broken from onEnter.

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

A whole breakout on retro bodies, and nothing in it does its own collisions. The paddle is `'kinematic'`: the game drives it by its velocity and nothing pushes it back. The ball is `'dynamic'` with `bounce: 1` and `gravityScale: 0`, so it comes off everything as fast as it arrived and never falls. The bricks are `'static'`, and the ball's `onEnter` is what breaks them.

The ball is a circle, so it comes off a brick's corner at an angle, the way it should. **Left** and **right** to move.

## 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, destroy, useRetroBody2d, useSelf, useUpdate } from 'nacatamalon';
import { getRetroBody2d } from 'nacatamalon/retro-physics2d';
import type { TTexture } from 'nacatamalon';

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

export const Ball = (picture: TTexture, scored: () => void) => {
    const sprite = createSprite({
        texture: picture,
        tint: '#ffffff',
        width: Settings.BALL,
        height: Settings.BALL,
        transform: {
            x: Settings.WIDTH / 2,
            y: Settings.PADDLE.y - 20
        }
    });
    useRetroBody2d({
        body: 'dynamic',
        collider: {
            shape: 'circle',
            radius: Settings.BALL / 2
        },
        bounce: 1,
        gravityScale: 0,
    });

    const body = getRetroBody2d(useSelf());
    if (body === null) {
        return;
    }

    const serve = () => {
        sprite.transform.x = Settings.WIDTH / 2;
        sprite.transform.y = Settings.PADDLE.y - 20;
        body.setLinearVelocity(Settings.SPEED * 0.5, -Settings.SPEED * 0.85);
    };
    serve();

    // Told with the OTHER object: a brick breaks, the paddle steers.
    body.onEnter((other) => {
        if (other.name === 'Brick') {
            destroy(other);
            scored();
        }
        if (other.name === 'Paddle') {
            // Where it hit the paddle decides where it goes: the edges send it sideways.
            const paddle = other.drawables[0] as { transform: { x: number } };
            const offset = (sprite.transform.x - paddle.transform.x) / (Settings.PADDLE.w / 2);
            body.setLinearVelocity(offset * Settings.SPEED * 0.8, -Settings.SPEED);
        }
    });

    useUpdate(() => {
        const v = body.velocity;
        // Never too flat, or it would cross the screen for ever...
        if (Math.abs(v.y) < Settings.SPEED * 0.3) {
            v.y = Math.sign(v.y || -1) * Settings.SPEED * 0.3;
        }
        // ...and always the same speed, whatever it hit last.
        const now = Math.hypot(v.x, v.y);
        v.x *= Settings.SPEED / now;
        v.y *= Settings.SPEED / now;
        if (sprite.transform.y > Settings.HEIGHT + 20) {
            serve();
        }
    });
};
```

`components/brick.ts`:

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

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

export const Brick = (x: number, y: number, color: string) => {
    createSprite({
        tint: color,
        width: Settings.BRICK.w - 2,
        height: Settings.BRICK.h - 2,
        transform: {
            x,
            y
        }
    });
    useRetroBody2d({
        body: 'static',
        collider: {
            shape: 'rect',
            width: Settings.BRICK.w - 2,
            height: Settings.BRICK.h - 2
        }
    });
};
```

`components/paddle.ts`:

```ts
import { createSprite, useKeyboard, usePointer, useRetroBody2d, useSelf, useUpdate } from 'nacatamalon';
import { getRetroBody2d } from 'nacatamalon/retro-physics2d';

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

export const Paddle = () => {
    const sprite = createSprite({
        tint: '#e8e8f0',
        width: Settings.PADDLE.w,
        height: Settings.PADDLE.h,
        transform: {
            x: Settings.WIDTH / 2,
            y: Settings.PADDLE.y
        }
    });
    useRetroBody2d({
        body: 'kinematic',
        collider: {
            shape: 'rect',
            width: Settings.PADDLE.w,
            height: Settings.PADDLE.h
        }
    });

    const body = getRetroBody2d(useSelf());
    const pointer = usePointer();
    const keys = useKeyboard();
    let target = Settings.WIDTH / 2;

    useUpdate(() => {
        if (body === null) {
            return;
        }
        const at = pointer.position();
        if (at !== null) {
            target = at.worldX;
        }
        target += ((keys.isDown('ArrowRight') ? 1 : 0) - (keys.isDown('ArrowLeft') ? 1 : 0)) * 6;
        const half = Settings.PADDLE.w / 2 + Settings.WALL;
        target = Math.max(half, Math.min(Settings.WIDTH - half, target));
        // Driven by its speed, never moved by hand: that is what lets it carry the ball's hit.
        body.velocity.x = (target - sprite.transform.x) * 12;
    });
};
```

`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, useRetroWorld2d, useSpawn } from 'nacatamalon';

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

export const Scene = () => {
    // No gravity: the ball only goes where it was sent.
    useRetroWorld2d({
        gravity: {
            x: 0,
            y: 0
        }
    });

    useSpawn(Solid)(Settings.WIDTH / 2, Settings.WALL / 2, Settings.WIDTH, Settings.WALL);
    useSpawn(Solid)(Settings.WALL / 2, Settings.HEIGHT / 2, Settings.WALL, Settings.HEIGHT);
    useSpawn(Solid)(Settings.WIDTH - Settings.WALL / 2, Settings.HEIGHT / 2, Settings.WALL, Settings.HEIGHT);

    const left = (Settings.WIDTH - Settings.BRICK.columns * Settings.BRICK.w) / 2 + Settings.BRICK.w / 2;
    const spawnBrick = useSpawn(Brick, 'Brick');
    for (let row = 0; row < Settings.BRICK.rows; row++) {
        for (let column = 0; column < Settings.BRICK.columns; column++) {
            spawnBrick(left + column * Settings.BRICK.w, Settings.BRICK.top + row * Settings.BRICK.h, Settings.ROWS[row]);
        }
    }

    useSpawn(Paddle, 'Paddle')();

    const score = createText({
        text: 'SCORE 0',
        transform: {
            x: 60,
            y: 26
        }
    });
    let points = 0;
    useSpawn(Ball, 'Ball')(paintBall(Settings.BALL), () => {
        points += 10;
        score.text = `SCORE ${points}`;
    });

    return createScene();
};
```

`settings.ts`:

```ts
// A whole breakout on retro bodies, and nothing in it does its own collisions:
//
//   the paddle is 'kinematic': the game drives it by its velocity and nothing pushes it back;
//   the ball is 'dynamic' with bounce: 1 and gravityScale: 0, so it comes off everything as fast
//   as it arrived and never falls;
//   the bricks are 'static', and the ball's onEnter is what breaks them.
//
// The ball is a circle, so it comes off a brick's corner at an angle, the way it should.

export const WIDTH = 480;
export const HEIGHT = 360;
export const WALL = 10;
export const SPEED = 260;
export const PADDLE = { w: 64, h: 10, y: 330 };
export const BALL = 9;
export const BRICK = { w: 40, h: 14, columns: 10, rows: 5, top: 50 };
export const ROWS = ['#ff6b6b', '#ff9f45', '#ffd93d', '#4cff91', '#5db6ff'];
```
