Spaces:
Running
Running
Update index.html
Browse files- index.html +67 -19
index.html
CHANGED
@@ -1,19 +1,67 @@
|
|
1 |
-
|
2 |
-
|
3 |
-
|
4 |
-
|
5 |
-
|
6 |
-
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
const canvas = document.getElementById("gameCanvas");
|
2 |
+
const ctx = canvas.getContext("2d");
|
3 |
+
|
4 |
+
// Hexagon properties
|
5 |
+
const hexSize = 50; // Size of each hexagon
|
6 |
+
const hexWidth = Math.sqrt(3) * hexSize;
|
7 |
+
const hexHeight = 2 * hexSize;
|
8 |
+
const offsetX = hexWidth * 0.75;
|
9 |
+
const offsetY = hexHeight * 0.5;
|
10 |
+
const rows = 10;
|
11 |
+
const cols = 10;
|
12 |
+
|
13 |
+
// Hex map storage
|
14 |
+
let hexGrid = [];
|
15 |
+
|
16 |
+
// Function to generate a hexagonal grid
|
17 |
+
function generateHexGrid() {
|
18 |
+
for (let row = 0; row < rows; row++) {
|
19 |
+
for (let col = 0; col < cols; col++) {
|
20 |
+
let x = col * offsetX;
|
21 |
+
let y = row * hexHeight + (col % 2 ? offsetY : 0);
|
22 |
+
hexGrid.push({ x, y, type: getRandomTerrain() });
|
23 |
+
}
|
24 |
+
}
|
25 |
+
}
|
26 |
+
|
27 |
+
// Function to draw hexagons
|
28 |
+
function drawHex(x, y, type) {
|
29 |
+
ctx.beginPath();
|
30 |
+
for (let i = 0; i < 6; i++) {
|
31 |
+
let angle = (Math.PI / 3) * i;
|
32 |
+
let px = x + hexSize * Math.cos(angle);
|
33 |
+
let py = y + hexSize * Math.sin(angle);
|
34 |
+
ctx.lineTo(px, py);
|
35 |
+
}
|
36 |
+
ctx.closePath();
|
37 |
+
ctx.fillStyle = getTerrainColor(type);
|
38 |
+
ctx.fill();
|
39 |
+
ctx.stroke();
|
40 |
+
}
|
41 |
+
|
42 |
+
// Function to assign random terrain types
|
43 |
+
function getRandomTerrain() {
|
44 |
+
const terrains = ["water", "plains", "forest", "mountain"];
|
45 |
+
return terrains[Math.floor(Math.random() * terrains.length)];
|
46 |
+
}
|
47 |
+
|
48 |
+
// Function to get terrain color
|
49 |
+
function getTerrainColor(type) {
|
50 |
+
switch (type) {
|
51 |
+
case "water": return "#3A80BA";
|
52 |
+
case "plains": return "#C2B280";
|
53 |
+
case "forest": return "#228B22";
|
54 |
+
case "mountain": return "#A9A9A9";
|
55 |
+
default: return "#FFFFFF";
|
56 |
+
}
|
57 |
+
}
|
58 |
+
|
59 |
+
// Function to render the map
|
60 |
+
function renderMap() {
|
61 |
+
ctx.clearRect(0, 0, canvas.width, canvas.height);
|
62 |
+
hexGrid.forEach(hex => drawHex(hex.x, hex.y, hex.type));
|
63 |
+
}
|
64 |
+
|
65 |
+
// Initialize and draw map
|
66 |
+
generateHexGrid();
|
67 |
+
renderMap();
|