import { Drawable } from "./drawable";
import { Figure } from "./figure";
import { ITEM_KEY_SPRITE } from "./images";

let globalItemId = 0;
const idMap = new Map<number, Figure>();

export const getItemsCount = () => idMap.size;

export function createItems() {
    for (let frame = 0; frame < ITEM_KEY_SPRITE.frames.length; frame++) {
        idMap.set(
            globalItemId++,
            new Figure(0, 0, ITEM_KEY_SPRITE, frame),
        );
    }
}

export class Item extends Drawable {
    constructor(
        public id: number,
        public count: number = 1,
        public x: number = -1,
        public y: number = -1,
    ) {
        super();
    }

    override doDraw() {
        const figure = idMap.get(this.id);
        if (figure) {
            figure.x = this.x;
            figure.y = this.y;
            figure.doDraw();
        }
    }
}
