# Pushing crates

> Crates of different mass pushed by a hero: the push is shared by mass, a row moves as one, and blocked says when the wall wins.

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

Two dynamic retro bodies that run into each other share the push by their `mass`: walking into a crate as heavy as you, you both go at half your speed; into one four times heavier, at a fifth. The mass is set directly, because a retro body has no material to weigh.

What is pushed pushes what is in its way, so a row of crates moves as one, and the last one against the wall stops them all, and you with them. That is when `blocked.right` turns true and the hero goes red: pushing a crate that gives way is not being blocked. **Arrows** to walk, **up** to jump.

## 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');
```

`components/crate.ts`:

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

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

export const Crate = (x: number, size: number, mass: number, color: string) => {
    // The crate is placed itself, and its picture and its number sit on it: the body moves the
    // crate, and both ride along.
    useTransform({
        x,
        y: Settings.FLOOR_Y - size / 2
    });
    createSprite({
        tint: color,
        width: size,
        height: size
    });
    createText({
        text: `${mass}`
    });
    useRetroBody2d({
        body: 'dynamic',
        collider: {
            shape: 'rect',
            width: size,
            height: size
        },
        mass,
    });
};
```

`components/hero.ts`:

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

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

const FREE = getColor('#5db6ff');
const STOPPED = getColor('#ff6b6b');

export const Hero = () => {
    const sprite = createSprite({
        tint: FREE,
        width: 14,
        height: 24,
        transform: {
            x: 60,
            y: Settings.FLOOR_Y - 12
        }
    });
    useRetroBody2d({
        body: 'dynamic',
        collider: {
            shape: 'rect',
            width: 14,
            height: 24
        },
        mass: 1,
    });

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

    useUpdate(() => {
        if (body === null) {
            return;
        }
        const walk = (keys.isDown('ArrowRight') ? 1 : 0) - (keys.isDown('ArrowLeft') ? 1 : 0);
        body.velocity.x = walk * Settings.WALK;
        if ((keys.justPressed('ArrowUp') || keys.justPressed('z')) && body.blocked.down) {
            body.velocity.y = -Settings.JUMP;
        }
        // Red while something it cannot move stops it.
        sprite.tint = body.blocked.left || body.blocked.right ? STOPPED : FREE;
    });
};
```

`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 { Hero } from '../components/hero';
import { Crate } from '../components/crate';

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

    useSpawn(Solid)(Settings.WIDTH / 2, Settings.HEIGHT - 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 spawnCrate = useSpawn(Crate);
    // A light one, one as heavy as the hero, and a heavy one...
    spawnCrate(120, 20, 0.5, '#4cff91');
    spawnCrate(190, 24, 1, '#ffd93d');
    spawnCrate(260, 30, 4, '#ff9f45');
    // ...and a row near the wall, to push all together.
    for (let i = 0; i < 4; i++) {
        spawnCrate(340 + i * 22, 20, 1, '#c77dff');
    }

    useSpawn(Hero, 'Hero')();

    createText({
        text: 'ARROWS TO WALK, UP TO JUMP',
        transform: {
            x: Settings.WIDTH / 2,
            y: 24
        },
        anchor: {
            x: 0.5,
            y: 0
        }
    });

    return createScene();
};
```

`settings.ts`:

```ts
// Two dynamic retro bodies that run into each other share the push by their `mass`: walking into
// a crate as heavy as you, you both go at half your speed; into one four times heavier, at a
// fifth. The mass is set directly, because a retro body has no material to weigh.
//
// What is pushed pushes what is in its way, so a row of crates moves as one, and the last one
// against the wall stops them all, and you with them: that is when `blocked.right` turns true,
// and the hero goes red. Pushing a crate that gives way is not being blocked.

export const WIDTH = 480;
export const HEIGHT = 300;
export const WALL = 12;
export const FLOOR_Y = HEIGHT - WALL;
export const WALK = 110;
export const JUMP = 330;
```
