# Hierarchy

> The same three levels in flat pixels and in three dimensions: the moon only knows how far it is from its planet.

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

`useTransform` gives an object a place of its own, and everything made **inside** it is placed relative to that. The moon only knows it is `0.55` from its planet: what carries it round the sun is the box above it turning.

The same three levels drive both halves, and the only difference is what hangs off them: squares on the left, spheres on the right. The lamp is inside the sun's box, so it travels with it, and moving one line in `System3D` moves the whole system.

## Code

`index.ts`:

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

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

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

`components/moon_2d.ts`:

```ts
import { useTransform, useUpdate } from 'nacatamalon';
import { createSquare } from '../helpers/create_square';
import { COLORS, FLAT, SPEED } from '../settings';

export const Moon2d = () => {
    const spin = useTransform({
        x: FLAT.orbit
    });

    createSquare(FLAT.moonSize, COLORS.moon, FLAT.moonOrbit);

    useUpdate((delta) => {
        spin.rotation += delta * SPEED.moon;
    });
};
```

`components/moon_3d.ts`:

```ts
import { createMesh, useSphereGeometry, useTransform, useUpdate } from 'nacatamalon';
import { COLORS, SPACE, SPEED } from '../settings';

export const Moon3d = () => {
    const spin = useTransform({
        x: SPACE.orbit
    });

    createMesh({
        geometry: useSphereGeometry({
            radius: SPACE.moonRadius
        }),
        tint: COLORS.moon,
        transform: {
            x: SPACE.moonOrbit
        }
    });

    useUpdate((delta) => {
        spin.rotationY += delta * SPEED.moon;
    });
};
```

`components/planet_2d.ts`:

```ts
import { useSpawn, useTransform, useUpdate } from 'nacatamalon';
import { Moon2d } from './moon_2d';
import { createSquare } from '../helpers/create_square';
import { COLORS, FLAT, SPEED } from '../settings';

export const Planet2d = () => {
    const spin = useTransform();

    createSquare(FLAT.planetSize, COLORS.planet, FLAT.orbit);
    useSpawn(Moon2d)();

    useUpdate((delta) => {
        spin.rotation += delta * SPEED.planet;
    });
};
```

`components/planet_3d.ts`:

```ts
import { createMesh, useSpawn, useSphereGeometry, useTransform, useUpdate } from 'nacatamalon';
import { Moon3d } from './moon_3d';
import { COLORS, SPACE, SPEED } from '../settings';

export const Planet3d = () => {
    const spin = useTransform();

    createMesh({
        geometry: useSphereGeometry({
            radius: SPACE.planetRadius
        }),
        tint: COLORS.planet,
        transform: {
            x: SPACE.orbit
        }
    });
    useSpawn(Moon3d)();

    useUpdate((delta) => {
        spin.rotationY += delta * SPEED.planet;
    });
};
```

`components/system_2d.ts`:

```ts
import { useSpawn, useTransform } from 'nacatamalon';
import { Planet2d } from './planet_2d';
import { createSquare } from '../helpers/create_square';
import { COLORS, FLAT } from '../settings';

export const System2d = () => {
    // Moving this placement moves the sun, planet and moon together.
    useTransform({
        x: FLAT.x,
        y: FLAT.y
    });
    createSquare(FLAT.sunSize, COLORS.sun, 0);
    useSpawn(Planet2d)();
};
```

`components/system_3d.ts`:

```ts
import { createMesh, usePointLight, useSpawn, useSphereGeometry, useTransform } from 'nacatamalon';
import { Planet3d } from './planet_3d';
import { COLORS, SPACE } from '../settings';

export const System3d = () => {
    useTransform({
        x: SPACE.x
    });

    // A lamp inside the sun's object follows the same placement as its mesh.
    usePointLight({
        intensity: 1.4,
        range: 14
    });
    createMesh({
        geometry: useSphereGeometry({
            radius: SPACE.sunRadius
        }),
        tint: COLORS.sun,
        emissive: '#8a6a12'
    });
    useSpawn(Planet3d)();
};
```

`helpers/create_square.ts`:

```ts
import { createSprite } from 'nacatamalon';

// A drawable on the current object, rather than a separate child object.
export const createSquare = (size: number, tint: string, x: number) => {
    return createSprite({
        tint,
        width: size,
        height: size,
        transform: {
            x
        }
    });
};
```

`scenes/demo.ts`:

```ts
import { createScene, useCamera3d, useSpawn } from 'nacatamalon';
import { System2d } from '../components/system_2d';
import { System3d } from '../components/system_3d';

export const Level = () => {
    useCamera3d({
        projection: 'perspective',
        fov: 55,
        transform: {
            y: 2.4,
            z: 7,
            rotationX: -0.32
        }
    });

    useSpawn(System2d)();
    useSpawn(System3d)();

    return createScene();
};
```

`settings.ts`:

```ts
export const WIDTH = 480;
export const HEIGHT = 320;

export const SPEED = {
    planet: 0.7,
    moon: 2.4
};

export const COLORS = {
    sun: '#ffcc55',
    planet: '#4c9dff',
    moon: '#cfd4e0'
};

export const FLAT = {
    x: 118,
    y: 160,
    orbit: 62,
    moonOrbit: 18,
    sunSize: 32,
    planetSize: 16,
    moonSize: 8
};

export const SPACE = {
    x: 1.9,
    orbit: 2.2,
    moonOrbit: 0.55,
    sunRadius: 0.6,
    planetRadius: 0.3,
    moonRadius: 0.12
};
```
