# Lifts and platforms

> Kinematic lifts and platforms carry what stands on them, and a static one moved by hand does the same.

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

A `'kinematic'` retro body is driven by the game, by its velocity, and nothing pushes it back: a lift, a platform on rails. Whatever stands on it is carried, across and down, and pushed up when it rises. A crate left on a platform rides it too: nothing about the hero is special.

The third platform is moved by hand, by changing its object's position every frame. A retro body is where its object is, so that works as well, and it carries what stands on it all the same. **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, useRetroBody2d } from 'nacatamalon';

export const Crate = (x: number, y: number) => {
    createSprite({
        tint: '#4cff91',
        width: 16,
        height: 16,
        transform: {
            x,
            y
        }
    });
    useRetroBody2d({
        body: 'dynamic',
        collider: {
            shape: 'rect',
            width: 16,
            height: 16
        }
    });
};
```

`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: 40,
            y: Settings.HEIGHT - Settings.WALL - 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/lift.ts`:

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

// A platform that goes back and forth between two points at a steady speed, driven by its
// velocity.
export const Lift = (x: number, y: number, w: number, to: { x: number; y: number }, speed: number) => {
    const sprite = createSprite({
        tint: '#ffd93d',
        width: w,
        height: 8,
        transform: {
            x,
            y
        }
    });
    useRetroBody2d({
        body: 'kinematic',
        collider: {
            shape: 'rect',
            width: w,
            height: 8
        }
    });

    const body = getRetroBody2d(useSelf());
    const ends = [{ x, y }, to];
    let going = 1;

    useUpdate(() => {
        if (body === null) {
            return;
        }
        const goal = ends[going];
        const dx = goal.x - sprite.transform.x;
        const dy = goal.y - sprite.transform.y;
        const left = Math.hypot(dx, dy);
        if (left < 1) {
            going = 1 - going;
            return;
        }
        body.setLinearVelocity(dx / left * speed, dy / left * speed);
    });
};
```

`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
        }
    });
};
```

`components/swing.ts`:

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

// A platform moved by hand: its position is written every frame, along a sine, and its body
// follows. Static, because nothing about it is simulated.
export const Swing = (x: number, y: number, w: number) => {
    const sprite = createSprite({
        tint: '#c77dff',
        width: w,
        height: 8,
        transform: {
            x,
            y
        }
    });
    useRetroBody2d({
        body: 'static',
        collider: {
            shape: 'rect',
            width: w,
            height: 8
        }
    });

    let time = 0;
    useUpdate((dt) => {
        time += dt;
        sprite.transform.x = x + Math.sin(time * 1.2) * 50;
    });
};
```

`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 { Lift } from '../components/lift';
import { Swing } from '../components/swing';
import { Crate } from '../components/crate';

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

    const floor = Settings.HEIGHT - Settings.WALL;
    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);
    // A ledge high on the right, only reached by the lifts.
    useSpawn(Solid)(Settings.WIDTH - 60, 90, 96, 10);

    const spawnLift = useSpawn(Lift);
    // Up and down, from the floor to halfway.
    spawnLift(90, floor - 6, 56, { x: 90, y: 160 }, 50);
    // Across, at that height.
    spawnLift(160, 160, 56, { x: 300, y: 160 }, 60);
    // Up to the ledge.
    spawnLift(330, 160, 48, { x: 330, y: 95 }, 40);

    useSpawn(Swing)(220, floor - 40, 60);

    // A crate waiting on the moving one, to show it is not only the hero who rides.
    useSpawn(Crate)(170, 140);

    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
// A 'kinematic' retro body is driven by the game, by its velocity, and nothing pushes it back:
// a lift, a platform on rails. Whatever stands on it is carried, across and down, and pushed up
// when it rises. A crate left on a platform rides it too: nothing about the hero is special.
//
// The third platform is moved by hand, by changing its object's position every frame. A retro
// body is where its object is, so that works as well: it carries what stands on it all the same.

export const WIDTH = 480;
export const HEIGHT = 320;
export const WALL = 12;
export const WALK = 100;
export const JUMP = 340;
```
