# Jump-through platforms

> One-way platforms from bodies with solidSides, dropping through with collidesIf, landing with touching and wasTouching, and impact speed from onEnter.

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

Platforms made of bodies, solid only on top (`solidSides: { up: true }`): jumped up through from below, stood on from above. A body with an open side stops only what comes in through a solid one, so the hero is never snapped up onto a platform it is still inside.

**Down** and **Z** drops through: for a moment the hero `collidesIf` the other is not a platform. Landing is `touching.down` this step and not `wasTouching.down` the step before, and `onEnter` says how hard it came down: the hero squashes by that much. **Arrows** to move, **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, getColor, useKeyboard, useRetroBody2d, useSelf, useTimer, useTransform, useUpdate } from 'nacatamalon';
import { getRetroBody2d } from 'nacatamalon/retro-physics2d';

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

const BLUE = getColor('#5db6ff');
const WHITE = getColor('#ffffff');

// `say` puts a line on the screen: the scene's text, which stays where it is while the hero moves.
export const Hero = (say: (line: string) => void) => {
    // The hero is placed itself, and its picture sits on it: squashing the picture leaves the
    // collider as it is.
    useTransform({
        x: 60,
        y: Settings.HEIGHT - Settings.WALL - 10
    });
    const sprite = createSprite({
        tint: BLUE,
        width: 12,
        height: 20
    });
    useRetroBody2d({
        body: 'dynamic',
        collider: {
            shape: 'rect',
            width: 12,
            height: 20
        },
    });
    const self = useSelf();
    const body = getRetroBody2d(self);
    const keys = useKeyboard();
    const timer = useTimer();
    let squash = 0;

    // How hard it came down, from the contact it was told about.
    body?.onEnter((other, contact) => {
        if (contact.side === 'down' && contact.speed > 100) {
            squash = Math.min(0.5, contact.speed / 1200);
            say(`LANDED ON ${other.name.toUpperCase()} AT ${Math.round(contact.speed)}`);
        }
    });

    useUpdate((dt) => {
        if (body === null) {
            return;
        }
        body.velocity.x = ((keys.isDown('ArrowRight') ? 1 : 0) - (keys.isDown('ArrowLeft') ? 1 : 0)) * Settings.RUN;
        const jump = keys.justPressed('z') || keys.justPressed(' ');
        if (jump && keys.isDown('ArrowDown') && body.touching.down) {
            // Through the platform it stands on: for a moment, platforms are not met.
            const stop = body.collidesIf((other) => other.name !== 'Platform');
            timer.after(0.25, stop);
        } else if (jump && body.touching.down) {
            body.velocity.y = -Settings.JUMP;
        }
        // The step it landed in.
        if (body.touching.down && !body.wasTouching.down) {
            sprite.tint = WHITE;
            timer.after(0.08, () => { sprite.tint = BLUE; });
        }
        squash = Math.max(0, squash - dt * 2);
        sprite.transform.scaleY = 1 - squash;
        sprite.transform.scaleX = 1 + squash;
    });
};
```

`components/platform.ts`:

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

// Solid only on top. A moving one is driven by its velocity, back and forth.
export const Platform = (x: number, y: number, width: number, moves: boolean) => {
    const sprite = createSprite({
        tint: moves ? '#ffd93d' : '#c77dff',
        width,
        height: 6,
        transform: {
            x,
            y
        }
    });
    useRetroBody2d({
        body: moves ? 'kinematic' : 'static',
        collider: {
            shape: 'rect',
            width,
            height: 6
        },
        solidSides: {
            up: true,
            down: false,
            left: false,
            right: false
        },
    });
    if (!moves) {
        return;
    }
    const body = getRetroBody2d(useSelf());
    let going = 1;
    useUpdate(() => {
        if (sprite.transform.x > x + 70) going = -1;
        if (sprite.transform.x < x - 70) going = 1;
        body?.setLinearVelocity(going * 50, 0);
    });
};
```

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

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

    const solid = useSpawn(Solid, 'Floor');
    solid(Settings.WIDTH / 2, Settings.HEIGHT - Settings.WALL / 2, Settings.WIDTH, Settings.WALL);
    solid(Settings.WALL / 2, Settings.HEIGHT / 2, Settings.WALL, Settings.HEIGHT);
    solid(Settings.WIDTH - Settings.WALL / 2, Settings.HEIGHT / 2, Settings.WALL, Settings.HEIGHT);

    const platform = useSpawn(Platform, 'Platform');
    Settings.PLATFORMS.forEach(([x, y, width], i) => platform(x, y, width, i === 1));

    const said = createText({
        text: '',
        transform: {
            x: Settings.WIDTH / 2,
            y: 40
        },
        anchor: {
            x: 0.5,
            y: 0
        }
    });
    useSpawn(Hero, 'Hero')((line: string) => {
        said.text = line;
    });

    createText({
        text: 'ARROWS, Z TO JUMP, DOWN + Z TO DROP',
        transform: {
            x: Settings.WIDTH / 2,
            y: 20
        },
        anchor: {
            x: 0.5,
            y: 0
        }
    });

    return createScene();
};
```

`settings.ts`:

```ts
// Platforms made of bodies, solid only on top (`solidSides: { up: true }`, the other three left
// open): jumped up through from below, stood on from above. A body with an open side stops only what
// comes in through a solid one, so the hero is never snapped up onto a platform it is still inside.
//
// Down and jump drops through: for a moment the hero `collidesIf` the other is not a platform.
// Landing is `touching.down` this step and not `wasTouching.down` the step before, and `onEnter`
// says how hard it came down: the hero squashes by that much.

export const WIDTH = 480;
export const HEIGHT = 320;
export const WALL = 12;
export const RUN = 110;
export const JUMP = 400;
export const PLATFORMS: Array<[number, number, number]> = [
    // x, y, width
    [120, 240, 120],
    [330, 190, 110],
    [150, 140, 90],
    [360, 90, 120],
];
```
