# Layering and alpha

> There is no depth buffer in 2D, so draw order is layering. Sprites carry a zIndex and the renderer sorts by it every frame.

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

In 3D, what covers what is decided by distance to the camera and settled by the depth buffer.
**In 2D there is no such thing**: whatever is drawn later covers what came before, and that
is the whole rule. So the question "what goes in front?" is the question "what order does it
draw in?".

That is `zIndex`. Every sprite carries one, the renderer sorts by it before drawing, and the
highest number lands on top. The three squares here never move and never change opacity: the
only thing that changes each second is which of them holds the high `zIndex`.

Worth noticing that `zIndex` is written straight onto the sprite `createSprite` returned,
inside the `useUpdate`. Nothing needs to be notified and the sprite is not rebuilt: the
renderer reads the value as it stands when the frame comes around.

Alpha is a separate thing, and it is here for a reason. It lives in the colour: each square
has a fixed, different `a` in its `tint`, so you can see through them and the reordering reads
without anything having to move. A transparent sprite obeys `zIndex` like any other.

And each one is a different colour, on purpose: see-through layers of **one single colour**
give the same result in any order, because white over white is white over white. With three
white squares the order would change every second and nothing would show it.

## Code

`index.ts`:

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

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

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

`components/card.ts`:

```ts
import { createSprite } from 'nacatamalon';
import { CARD_SIZE } from '../settings';

type TCardProps = {
    colour: string;
    x: number;
    y: number;
    order: number;
};

export const Card = ({ colour, x, y, order }: TCardProps) => {
    return createSprite({
        tint: colour,
        width: CARD_SIZE,
        height: CARD_SIZE,
        zIndex: order,
        transform: {
            x,
            y
        }
    });
};
```

`scenes/demo.ts`:

```ts
import { createScene, useSpawn, useUpdate } from 'nacatamalon';
import { Card } from '../components/card';
import { CARDS, SWITCH_SECONDS } from '../settings';

export const Cards = () => {
    const spawnCard = useSpawn(Card);

    const cards = CARDS.map((card, order) => spawnCard({
        ...card,
        order
    }).api);

    // A tint's alpha controls transparency; zIndex independently chooses the drawing order.
    useUpdate((_delta, time) => {
        const front = Math.floor(time / SWITCH_SECONDS) % cards.length;

        cards.forEach((card, index) => {
            card.zIndex = index === front ? 10 : index;
        });
    });

    return createScene();
};
```

`settings.ts`:

```ts
export const WIDTH = 480;
export const HEIGHT = 320;
export const CARD_SIZE = 150;
export const SWITCH_SECONDS = 1;

export const CARDS = [
    {
        colour: 'rgba(255, 85, 102, 0.85)',
        x: 190,
        y: 120
    },
    {
        colour: 'rgba(85, 221, 119, 0.6)',
        x: 250,
        y: 160
    },
    {
        colour: 'rgba(85, 153, 255, 0.4)',
        x: 310,
        y: 200
    }
];
```
