# Living texture

> Ground you can blow holes in: a click cuts a crater, only that rectangle is sent again, and the same picture is the collision.

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

A texture that changes while the game runs. The ground is one picture painted in code; a click cuts a crater out of it with a transparent `fillCircle`, and `updatePixelTexture` sends **only the rectangle** around the crater to the graphics card, not the whole picture. That is what keeps a big picture that changes a little every frame cheap: a minimap filling in, fog of war lifting, a floor you can dig.

The same picture is also the **collision**. Each ball asks `getPixel` whether the pixel under it is solid, so the balls over a new crater fall into it. There is no second copy of the ground to keep in step with what is drawn.

**Click** to dig.

## Code

`index.ts`:

```ts
import { createGame } from 'nacatamalon';
import { Game } from './scenes/demo';
import { WIDTH, HEIGHT } from './settings';

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

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

`art/ground.ts`:

```ts
import { WIDTH, HEIGHT } from '../settings';
import { getColor } from 'nacatamalon';
import { createPixels, fillCircle, fillGradient, fillNoise, getPixel, mapPixels } from 'nacatamalon/pixels';
import type { TPixels } from 'nacatamalon/pixels';

// The pictures the scene is made of, painted here: the sky behind, the ground you dig into, and the
// ball. index.ts turns them into textures and plays with them.

const c = (hex: string) => getColor(hex);

// Where the ground starts in each column: two waves added together.
const surfaceAt = (x: number): number => 120 + 18 * Math.sin(x * 0.03) + 9 * Math.sin(x * 0.11 + 1.3);

export const paintSky = (): TPixels =>
    fillGradient(createPixels(WIDTH, HEIGHT), {
        from: c('#1a1440'),
        to: c('#e88a6a'),
        bands: 10,
        dither: true
    });

// Dirt from noise below a wavy top edge, with a strip of grass along it, and nothing above.
export const paintGround = (): TPixels => {
    const dirt = fillNoise(createPixels(WIDTH, HEIGHT), {
        seed: 3,
        scale: 16,
        octaves: 3,
        from: c('#4a2e1c'),
        to: c('#8a5a34')
    });

    return mapPixels(createPixels(WIDTH, HEIGHT), (_, x, y) => {
        const top = surfaceAt(x);

        if (y < top) {
            return c('transparent');
        }

        return y - top < 4 ? c('#3fae4a') : getPixel(dirt, x, y);
    });
};

export const paintBall = (): TPixels => fillCircle(createPixels(9, 9), 4, 4, 4, c('#ffe066'));
```

`components/terrain.ts`:

```ts
import { createSprite, createText, getColor, usePointer, useUpdate } from 'nacatamalon';
import { useRandom } from 'nacatamalon/math';
import { createPixelTexture, fillCircle, getPixel, updatePixelTexture } from 'nacatamalon/pixels';
import type { TSprite } from 'nacatamalon';
import { WIDTH, HEIGHT, RADIUS } from '../settings';
import { paintBall, paintGround, paintSky } from '../art/ground';

type TBall = { x: number; y: number; vy: number; sprite: TSprite; };

export const Terrain = () => {
    const rnd = useRandom();

    createSprite({
        texture: createPixelTexture(paintSky()),
        width: WIDTH,
        height: HEIGHT,
        anchor: {
            x: 0,
            y: 0
        }
    });

    const ground = paintGround();
    const groundTexture = createPixelTexture(ground);

    createSprite({
        texture: groundTexture,
        width: WIDTH,
        height: HEIGHT,
        anchor: {
            x: 0,
            y: 0
        },
        zIndex: 1
    });

    const solid = (x: number, y: number): boolean => getPixel(ground, x, y).a > 0.5;

    const ballTexture = createPixelTexture(paintBall());
    const balls: TBall[] = [];

    for (let i = 0; i < 8; i++) {
        balls.push({
            x: rnd.rand(10, WIDTH - 10),
            y: rnd.rand(-120, 0),
            vy: 0,
            sprite: createSprite({
                texture: ballTexture,
                width: 9,
                height: 9,
                zIndex: 2
            }),
        });
    }

    const label = createText({
        text: '',
        zIndex: 3,
        transform: {
            x: 8,
            y: 8
        }
    });

    createText({
        text: 'CLICK TO DIG',
        zIndex: 3,
        tint: '#ffd0f0',
        transform: {
            x: 8,
            y: 20
        }
    });

    let craters = 0;

    const show = (sent: string) => {
        label.text = 'CRATERS ' + craters + (sent === '' ? '' : '   SENT ' + sent + ' OF ' + WIDTH + 'x' + HEIGHT);
    };

    show('');

    usePointer().onDown((info) => {
        const x = Math.round(info.screenX);
        const y = Math.round(info.screenY);

        fillCircle(ground, x, y, RADIUS, getColor('transparent'));

        const size = RADIUS * 2 + 1;

        updatePixelTexture(groundTexture, {
            x: x - RADIUS,
            y: y - RADIUS,
            width: size,
            height: size
        });
        craters++;
        show(size + 'x' + size);
    });

    useUpdate((delta) => {
        for (const ball of balls) {
            ball.vy = Math.min(ball.vy + 400 * delta, 300);

            const next = ball.y + ball.vy * delta;

            if (solid(ball.x, next + 4)) {
                // Landed: sit on the first empty pixel above the ground.
                let y = Math.floor(next);

                while (y > 0 && solid(ball.x, y + 4)) {
                    y--;
                }

                ball.y = y;
                ball.vy = 0;
            } else {
                ball.y = next;
            }

            if (ball.y > HEIGHT + 10) {
                ball.x = rnd.rand(10, WIDTH - 10);
                ball.y = -10;
                ball.vy = 0;
            }

            ball.sprite.transform.x = ball.x;
            ball.sprite.transform.y = ball.y;
        }
    });
};
```

`scenes/demo.ts`:

```ts
import { createScene, useSpawn } from 'nacatamalon';
import { Terrain } from '../components/terrain';

export const Game = () => {
    useSpawn(Terrain)();

    return createScene();
};
```

`settings.ts`:

```ts
export const WIDTH = 320;
export const HEIGHT = 224;
export const RADIUS = 16;
```
