# Coins and pools

> Sensor coins collected by tag with useCollisionWith, and a pool that slows the hero by changing its record from onEnter and onExit.

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

A sensor notices what overlaps it and stops nothing: a coin, a pool, a place that hurts. There are two ways to hear about it, and both work with retro bodies. `useCollisionWith('coin', ...)` asks by tag, from the hero, with no listener on the coins. `body.onEnter` and `body.onExit` come from the body itself, with the other object.

The pool slows the hero by changing its record: `useRetroBody2d` hands back what the object is made of, and the simulation reads it every step, so a new `maxSpeed` is the new speed limit. **Arrows** 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/coin.ts`:

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

export const Coin = (x: number, y: number, picture: TTexture) => {
    createSprite({
        texture: picture,
        tint: '#ffd93d',
        width: 9,
        height: 9,
        transform: {
            x,
            y
        }
    });
    useRetroBody2d({
        body: 'static',
        sensor: true,
        collider: {
            shape: 'circle',
            radius: 4.5
        }
    });
};
```

`components/hero.ts`:

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

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

export const Hero = (collected: () => void) => {
    createSprite({
        tint: '#4cff91',
        width: 12,
        height: 12,
        transform: {
            x: 60,
            y: Settings.HEIGHT / 2
        }
    });
    const record = useRetroBody2d({
        body: 'dynamic',
        collider: {
            shape: 'rect',
            width: 12,
            height: 12
        },
        gravityScale: 0,
        drag: Settings.DRAG,
        maxSpeed: Settings.FAST,
    });

    const body = getRetroBody2d(useSelf());
    const keys = useKeyboard();

    useUpdate(() => {
        if (body === null) {
            return;
        }
        body.acceleration.x = ((keys.isDown('ArrowRight') ? 1 : 0) - (keys.isDown('ArrowLeft') ? 1 : 0)) * Settings.PUSH;
        body.acceleration.y = ((keys.isDown('ArrowDown') ? 1 : 0) - (keys.isDown('ArrowUp') ? 1 : 0)) * Settings.PUSH;
    });

    // By tag: every coin carries 'coin', and the hero asks for it. The coins know nothing.
    useCollisionWith('coin', (coin) => {
        destroy(coin);
        collected();
    }, { when: 'start' });

    // From the body: in the pool it wades, out of it it runs. The record is what it is made of,
    // read every step.
    body?.onEnter((other) => {
        if (other.name === 'Pool') {
            record.maxSpeed = Settings.SLOW;
        }
    });
    body?.onExit((other) => {
        if (other.name === 'Pool') {
            record.maxSpeed = Settings.FAST;
        }
    });
};
```

`components/pool.ts`:

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

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

export const Pool = () => {
    createSprite({
        tint: {
            ...getColor('#5db6ff'),
            a: 0.25
        },
        width: Settings.POOL.w,
        height: Settings.POOL.h,
        zIndex: -1,
        transform: {
            x: Settings.POOL.x,
            y: Settings.POOL.y
        }
    });
    useRetroBody2d({
        body: 'static',
        sensor: true,
        collider: {
            shape: 'rect',
            width: Settings.POOL.w,
            height: Settings.POOL.h
        }
    });
};
```

`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 { Coin } from '../components/coin';
import { Pool } from '../components/pool';
import { Hero } from '../components/hero';
import { paintBall } from '../art/ball';

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

    const wall = useSpawn(Solid);
    wall(Settings.WIDTH / 2, Settings.WALL / 2, Settings.WIDTH, Settings.WALL);
    wall(Settings.WIDTH / 2, Settings.HEIGHT - Settings.WALL / 2, Settings.WIDTH, Settings.WALL);
    wall(Settings.WALL / 2, Settings.HEIGHT / 2, Settings.WALL, Settings.HEIGHT);
    wall(Settings.WIDTH - Settings.WALL / 2, Settings.HEIGHT / 2, Settings.WALL, Settings.HEIGHT);
    wall(180, 120, 12, 140);

    useSpawn(Pool, 'Pool')();

    // A grid of coins, some of them in the pool.
    const picture = paintBall(9);
    const spawnCoin = useSpawn(Coin, { name: 'Coin', tags: ['coin'] });
    for (let row = 0; row < 5; row++) {
        for (let column = 0; column < 8; column++) {
            spawnCoin(230 + column * 28, 70 + row * 45, picture);
        }
    }

    let coins = 0;
    const counter = createText({
        text: 'COINS 0',
        transform: {
            x: 60,
            y: 24
        }
    });
    useSpawn(Hero, 'Hero')(() => {
        coins++;
        counter.text = `COINS ${coins}`;
    });

    createText({
        text: 'ARROWS TO MOVE',
        transform: {
            x: Settings.WIDTH / 2 + 80,
            y: 24
        }
    });

    return createScene();
};
```

`settings.ts`:

```ts
// A sensor notices what overlaps it and stops nothing: a coin, a pool, a place that hurts.
// Two ways to hear about it, and both work with retro bodies:
//
//   useCollisionWith('coin', ...) asks by TAG, from the hero: no body, no listener on the coin.
//   body.onEnter / body.onExit, from the body itself, with the other object.
//
// The pool slows the hero by changing its record: useRetroBody2d hands back what the object is
// made of, and the simulation reads it every step, so a new maxSpeed is the new speed limit.

export const WIDTH = 480;
export const HEIGHT = 320;
export const WALL = 10;
export const PUSH = 900;
export const DRAG = 900;
export const FAST = { x: 150, y: 150 };
export const SLOW = { x: 50, y: 50 };
export const POOL = { x: 330, y: 180, w: 150, h: 120 };
```
