// Isometric Utilities
// Konverzija med kartezičnimi in izometričnimi koordinatami
class IsometricUtils {
    constructor(tileWidth = 48, tileHeight = 24) {
        this.tileWidth = tileWidth;
        this.tileHeight = tileHeight;
    }

    // Kartezične (grid) koordinate -> Isometrične (screen) koordinate
    toScreen(gridX, gridY) {
        const screenX = (gridX - gridY) * (this.tileWidth / 2);
        const screenY = (gridX + gridY) * (this.tileHeight / 2);
        return { x: screenX, y: screenY };
    }

    // Isometrične (screen) koordinate -> Kartezične (grid) koordinate
    toGrid(screenX, screenY) {
        const gridX = (screenX / (this.tileWidth / 2) + screenY / (this.tileHeight / 2)) / 2;
        const gridY = (screenY / (this.tileHeight / 2) - screenX / (this.tileWidth / 2)) / 2;
        return { x: Math.floor(gridX), y: Math.floor(gridY) };
    }

    // Izračun depth (z-index) za pravilno sortiranje
    // Izračun depth (z-index)
    // V Phaserju je najbolje uporabiti kar Y koordinato spritha.
    // Tu vrnemo pričakovano Y pozicijo za grid celico.
    getDepth(gridX, gridY) {
        // (gridX + gridY) * tileHeight/2 je točno screenY
        return (gridX + gridY) * (this.tileHeight / 2);
    }

    // Izračun centerja tile-a
    getTileCenter(gridX, gridY) {
        const screen = this.toScreen(gridX, gridY);
        return {
            x: screen.x,
            y: screen.y + this.tileHeight / 2
        };
    }

    // Preveri ali je grid koordinata znotraj meja
    isInBounds(gridX, gridY, mapWidth, mapHeight) {
        return gridX >= 0 && gridX < mapWidth && gridY >= 0 && gridY < mapHeight;
    }

    // Dobi sosednje tile-e (NSEW)
    getNeighbors(gridX, gridY, mapWidth, mapHeight) {
        const neighbors = [];
        const directions = [
            { x: 0, y: -1 },  // North
            { x: 1, y: 0 },   // East
            { x: 0, y: 1 },   // South
            { x: -1, y: 0 }   // West
        ];

        for (const dir of directions) {
            const nx = gridX + dir.x;
            const ny = gridY + dir.y;
            if (this.isInBounds(nx, ny, mapWidth, mapHeight)) {
                neighbors.push({ x: nx, y: ny });
            }
        }

        return neighbors;
    }
}
