# Platformer feel

> Run, slide and jump from acceleration, drag, maxSpeed and blocked: a platformer's feel with no movement code of its own.

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

A platformer's feel from four fields of a retro body, none of them worked out by hand each frame. `acceleration`: the pad pushes, so the hero speeds up instead of starting at full speed. `drag`: with the pad let go it slides to a stop. `maxSpeed`: one top speed for running and another for falling, per axis. `blocked`: down to know it may jump, up for a head that hits a ceiling.

Let go of jump early for a short hop: the upward speed is cut while it is still rising. **Arrows** to run, **up** or **Z** 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/hero.ts`:

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

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

export const Hero = () => {
    const sprite = createSprite({
        tint: '#5db6ff',
        width: 12,
        height: 20,
        transform: {
            x: 40,
            y: Settings.HEIGHT - Settings.WALL - 10
        }
    });
    useRetroBody2d({
        body: 'dynamic',
        collider: {
            shape: 'rect',
            width: 12,
            height: 20
        },
        drag: Settings.DRAG,
        maxSpeed: Settings.MAX,
    });

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

    useUpdate(() => {
        if (body === null) {
            return;
        }
        const walk = (keys.isDown('ArrowRight') ? 1 : 0) - (keys.isDown('ArrowLeft') ? 1 : 0);
        // The pad pushes; with it let go, drag slows it down.
        body.acceleration.x = walk * Settings.RUN;
        if (walk !== 0) {
            sprite.flipX = walk < 0;
        }

        const jump = keys.justPressed('ArrowUp') || keys.justPressed('z') || keys.justPressed(' ');
        if (jump && body.blocked.down) {
            body.velocity.y = -Settings.JUMP;
        }
        // A short hop: jump let go while still going up.
        const held = keys.isDown('ArrowUp') || keys.isDown('z') || keys.isDown(' ');
        if (!held && body.velocity.y < -Settings.JUMP * 0.4) {
            body.velocity.y = -Settings.JUMP * 0.4;
        }
    });
};
```

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

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

    const spawnSolid = useSpawn(Solid);
    spawnSolid(Settings.WIDTH / 2, Settings.HEIGHT - Settings.WALL / 2, Settings.WIDTH, Settings.WALL);
    spawnSolid(Settings.WALL / 2, Settings.HEIGHT / 2, Settings.WALL, Settings.HEIGHT);
    spawnSolid(Settings.WIDTH - Settings.WALL / 2, Settings.HEIGHT / 2, Settings.WALL, Settings.HEIGHT);
    for (const [x, y, w, h] of Settings.LEVEL) {
        spawnSolid(x, y, w, h);
    }

    useSpawn(Hero, 'Hero')();

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

    return createScene();
};
```

`settings.ts`:

```ts
// A platformer's feel from four fields of a retro body, none of them written by hand each frame:
//
//   acceleration: the pad pushes, so the hero speeds up instead of starting at full speed;
//   drag: with the pad let go, it slides to a stop (on an axis with no acceleration);
//   maxSpeed: a top speed for running and another for falling, per axis;
//   blocked: down to know it may jump, up for a head that hits a ceiling.
//
// Let go of jump early for a short hop: the upward speed is cut while it is still rising.

export const WIDTH = 480;
export const HEIGHT = 320;
export const WALL = 12;
export const RUN = 900;
export const DRAG = 700;
export const MAX = { x: 140, y: 480 };
export const JUMP = 360;
export const LEVEL: Array<[number, number, number, number]> = [
    // x, y, width, height of each platform
    [100, 250, 90, 10],
    [210, 200, 80, 10],
    [330, 160, 90, 10],
    [200, 110, 60, 10],
    [80, 150, 60, 10],
    [420, 240, 40, 60],
];
```
