# Spawn and destroy

> Objects that come and go while the game runs: a tank fires bullets with useSpawn and each bullet ends its own life with destroy.

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

`useSpawn` creates an object while the game runs (a bullet, an enemy wave, an explosion) and `destroy` removes one. Move with **←** **→** and fire with **Space**: each shot is a new object that carries the `useUpdate` moving it and ends its own life offscreen or on impact.

Removal is deferred to the end of the frame, so a bullet cannot register a second hit after it exploded and never leaves a ghost frame behind. Note what the tank does not do: no bullet pool, no list, no cleanup.

## Code

`index.ts`:

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

const game = createGame('#app', {
    width: WIDTH,
    height: HEIGHT,
    background: '#101014',
    actions: ACTIONS
});

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

`components/brick.ts`:

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

type TBrickProps = {
    x: number;
    y: number;
};

export const Brick = ({ x, y }: TBrickProps) => {
    createSprite({
        tint: '#b5651d',
        width: BRICK.width,
        height: BRICK.height,
        transform: {
            x,
            y
        }
    });
};
```

`components/bullet.ts`:

```ts
import { createSprite, destroy, useCollisionWith, useSelf, useUpdate } from 'nacatamalon';
import { Brick } from './brick';
import { BULLET } from '../settings';

type TBulletProps = {
    x: number;
    y: number;
};

export const Bullet = ({ x, y }: TBulletProps) => {
    const self = useSelf();

    const sprite = createSprite({
        tint: '#ffe066',
        width: BULLET.width,
        height: BULLET.height,
        transform: {
            x,
            y
        }
    });

    useUpdate((delta) => {
        sprite.transform.y -= BULLET.speed * delta;

        if (sprite.transform.y < -BULLET.height) {
            destroy(self);
        }
    });

    // The component identifies its bricks. No global list or hand-written overlap test is needed.
    useCollisionWith(Brick, (brick) => {
        destroy(brick);
        destroy(self);
    });
};
```

`components/tank.ts`:

```ts
import { createSprite, useActions, useSpawn, useUpdate } from 'nacatamalon';
import { clamp } from 'nacatamalon/math';
import { Bullet } from './bullet';
import { BULLET, TANK, WIDTH } from '../settings';

export const Tank = () => {
    const input = useActions();

    // A fired shot belongs to the scene; moving its tank later must not carry it along.
    const fire = useSpawn(Bullet, {
        parent: 'scene'
    });

    const sprite = createSprite({
        tint: '#8bc34a',
        width: TANK.width,
        height: TANK.height,
        transform: {
            x: WIDTH / 2,
            y: TANK.y
        }
    });

    useUpdate((delta) => {
        const direction = input.value('right') - input.value('left');

        sprite.transform.x = clamp(
            sprite.transform.x + direction * TANK.speed * delta,
            TANK.width / 2,
            WIDTH - TANK.width / 2
        );

        if (input.justPressed('fire')) {
            fire({
                x: sprite.transform.x,
                y: sprite.transform.y - BULLET.muzzleOffset
            });
        }
    });
};
```

`scenes/level.ts`:

```ts
import { createScene, useSpawn } from 'nacatamalon';
import { Brick } from '../components/brick';
import { Tank } from '../components/tank';
import { BRICK } from '../settings';

export const Level = () => {
    useSpawn(Tank)();

    const spawnBrick = useSpawn(Brick);

    for (let index = 0; index < BRICK.count; index++) {
        spawnBrick({
            x: BRICK.startX + index * BRICK.gap,
            y: BRICK.y
        });
    }

    return createScene();
};
```

`settings.ts`:

```ts
import { INPUT } from 'nacatamalon';
import type { TActionMap } from 'nacatamalon';

export const WIDTH = 340;
export const HEIGHT = 260;

export const BRICK = {
    width: 22,
    height: 14,
    count: 5,
    startX: 72,
    gap: 46,
    y: 67
};

export const TANK = {
    width: 26,
    height: 26,
    speed: 150,
    y: 222
};

export const BULLET = {
    width: 4,
    height: 10,
    speed: 320,
    muzzleOffset: 18
};

export const ACTIONS: TActionMap = [
    ...INPUT.ACTION_PRESETS.platformer.filter(action => ['left', 'right'].includes(action.name)),
    {
        name: 'fire',
        bindings: [
            {
                type: 'key',
                key: ' '
            },
            {
                type: 'button',
                button: 'a'
            }
        ]
    }
];
```
