import { Vector2 } from "./index.js";
class Cube {
    x;
    y;
    z;
    constructor(x, y, z) {
        this.x = x;
        this.y = y;
        this.z = z;
    }
}
export class Hex {
    col;
    row;
    constructor(x, y) {
        this.col = x;
        this.row = y;
    }
    static offsetDirections = [
        [
            new Hex(+1, 0),
            new Hex(0, -1),
            new Hex(-1, -1),
            new Hex(-1, 0),
            new Hex(-1, +1),
            new Hex(0, +1)
        ],
        [
            new Hex(+1, 0),
            new Hex(+1, -1),
            new Hex(0, -1),
            new Hex(-1, 0),
            new Hex(0, +1),
            new Hex(+1, +1),
            new Hex(+1, +1)
        ]
    ];
    static neighbour = (hex, direction) => {
        const parity = hex.row & 1;
        const dir = Hex.offsetDirections[parity][direction];
        return new Hex(hex.col + dir.col, hex.row + dir.row);
    };
    static distance = (a, b) => {
        const ac = Hex.offsetToCube(a);
        const bc = Hex.offsetToCube(b);
        return Math.max(Math.abs(ac.x - bc.x), Math.abs(ac.y - bc.y), Math.abs(ac.z - bc.z));
    };
    static offsetToCube = (hex) => {
        const x = hex.col - (hex.row - (hex.row & 1)) / 2;
        const z = hex.row;
        const y = -x - z;
        return new Cube(x, y, z);
    };
    static toWorld = (hex) => {
        const x = Math.sqrt(3) * (hex.col + 0.5 * (hex.row & 1));
        const y = (3 / 2) * hex.row;
        return new Vector2(x, y);
    };
    toWorld() {
        return Hex.toWorld(this);
    }
    static fromWorld = (coord) => {
        const q = (coord.x * Math.sqrt(3)) / 3 - coord.y / 3;
        const r = (coord.y * 2) / 3;
        const cube = new Cube(q, -q - r, r);
        let rx = Math.round(cube.x);
        let ry = Math.round(cube.y);
        let rz = Math.round(cube.z);
        const xDiff = Math.abs(rx - cube.x);
        const yDiff = Math.abs(ry - cube.y);
        const zDiff = Math.abs(rz - cube.z);
        if (xDiff > yDiff && xDiff > zDiff) {
            rx = -ry - rz;
        }
        else if (yDiff > zDiff) {
            ry = -rx - rz;
        }
        else {
            rz = -rx - ry;
        }
        const rounded = new Cube(rx, ry, rz);
        const col = rounded.x + (rounded.z - (rounded.z & 1)) / 2;
        const row = rounded.z;
        return new Hex(col, row);
    };
    static toPixel = (hex, size) => {
        const w = Hex.toWorld(hex);
        return new Vector2(w.x * size, w.y * size);
    };
    toPixel(size) {
        return Hex.toPixel(this, size);
    }
    static fromPixel = (coord, size) => {
        return Hex.fromWorld(new Vector2(coord.x / size, coord.y / size));
    };
}
//# sourceMappingURL=Hex.js.map