1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
|
// 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;
}
};
|