// Hex grid utilities and calculations const HexGrid = { get SIZE() { return Config.hex.SIZE }, get WIDTH() { return Config.hex.WIDTH }, get HEIGHT() { return Config.hex.HEIGHT }, get GRID_SIZE() { return Config.hex.GRID_SIZE }, COLOR: Config.colors.GRID, IMPASSABLE_COLOR: Config.colors.BACKGROUND, // Convert hex coordinates to pixel coordinates toPixel(hex) { const x = this.SIZE * (3/2 * hex.q); const y = this.SIZE * (Math.sqrt(3)/2 * hex.q + Math.sqrt(3) * hex.r); return {x, y}; }, // Convert pixel coordinates to hex coordinates fromPixel(x, y) { const q = (2/3 * x) / this.SIZE; const r = (-1/3 * x + Math.sqrt(3)/3 * y) / this.SIZE; return this.round(q, r); }, // Round hex coordinates to nearest hex round(q, r) { let x = q; let z = r; let y = -x-z; let rx = Math.round(x); let ry = Math.round(y); let rz = Math.round(z); const x_diff = Math.abs(rx - x); const y_diff = Math.abs(ry - y); const z_diff = Math.abs(rz - z); if (x_diff > y_diff && x_diff > z_diff) { rx = -ry-rz; } else if (y_diff > z_diff) { ry = -rx-rz; } else { rz = -rx-ry; } return {q: rx, r: rz}; }, // Calculate visible hexes getViewportHexes() { const hexes = []; const halfGrid = Math.floor(this.GRID_SIZE / 2); for (let r = -halfGrid; r < halfGrid; r++) { for (let q = -halfGrid; q < halfGrid; q++) { hexes.push({q, r}); } } return hexes; }, // Add this method to check if a hex is passable isPassable(hex) { const halfGrid = Math.floor(this.GRID_SIZE / 2); return Math.abs(hex.q) <= halfGrid && Math.abs(hex.r) <= halfGrid; } };