<h1>Hex Grid Path Drawer</h1>
<p>Unhighlighted edges are thin. Hover or click to make them thick and bright.</p>
<div id="grid-container">
<svg id="hex-grid-svg"></svg>
</div>
<script>
// The JavaScript is identical to the previous version.
// All changes were made in the CSS.
document.addEventListener('DOMContentLoaded', function() {
const GRID_COLS = 12;
const GRID_ROWS = 8;
const HEX_RADIUS = 40;
const EDGE_INSET = 2;
const TAPER_AMOUNT = 6;
const svg = document.getElementById('hex-grid-svg');
const svgEdgesGroup = document.createElementNS('http://www.w3.org/2000/svg', 'g');
const svgHitboxGroup = document.createElementNS('http://www.w3.org/2000/svg', 'g');
svg.appendChild(svgEdgesGroup);
svg.appendChild(svgHitboxGroup);
const HEX_WIDTH = Math.sqrt(3) * HEX_RADIUS;
const HEX_HEIGHT = 2 * HEX_RADIUS;
function getHexVertices(centerX, centerY, radius) {
const vertices = [];
for (let i = 0; i < 6; i++) {
const angle = (Math.PI / 180) * (60 * i + 30);
const x = centerX + radius * Math.cos(angle);
const y = centerY + radius * Math.sin(angle);
vertices.push({ x, y });
}
return vertices;
}
function handlePolygonClick(event) {
const edgeId = event.currentTarget.dataset.edgeId;
document.getElementById(edgeId)?.classList.toggle('active');
}
function handlePolygonMouseOver(event) {
const edgeId = event.currentTarget.dataset.edgeId;
document.getElementById(edgeId)?.classList.add('hover-preview');
}
function handlePolygonMouseOut(event) {
const edgeId = event.currentTarget.dataset.edgeId;
document.getElementById(edgeId)?.classList.remove('hover-preview');
}
for (let row = 0; row < GRID_ROWS; row++) {
for (let col = 0; col < GRID_COLS; col++) {
const xOffset = (row % 2 === 1) ? HEX_WIDTH / 2 : 0;
const centerX = (col * HEX_WIDTH) + xOffset;
const centerY = row * HEX_HEIGHT * 0.75;
const outerRadius = HEX_RADIUS - EDGE_INSET;
const innerRadius = HEX_RADIUS - EDGE_INSET - TAPER_AMOUNT;
const outerVertices = getHexVertices(centerX, centerY, outerRadius);
const innerVertices = getHexVertices(centerX, centerY, innerRadius);
const hitboxVertices = getHexVertices(centerX, centerY, HEX_RADIUS);
for (let i = 0; i < 6; i++) {
const edgeId = `edge-${row}-${col}-${i}`;
const op1 = outerVertices[i];
const op2 = outerVertices[(i + 1) % 6];
const ip1 = innerVertices[i];
const ip2 = innerVertices[(i + 1) % 6];
const edge = document.createElementNS('http://www.w3.org/2000/svg', 'polygon');
edge.setAttribute('points', `${op1.x},${op1.y} ${op2.x},${op2.y} ${ip2.x},${ip2.y} ${ip1.x},${ip1.y}`);
edge.setAttribute('id', edgeId);
edge.setAttribute('class', 'hex-edge');
svgEdgesGroup.appendChild(edge);
const hp1 = hitboxVertices[i];
const hp2 = hitboxVertices[(i + 1) % 6];
const hitbox = document.createElementNS('http://www.w3.org/2000/svg', 'polygon');
hitbox.setAttribute('points', `${centerX},${centerY} ${hp1.x},${hp1.y} ${hp2.x},${hp2.y}`);
hitbox.dataset.edgeId = edgeId;
hitbox.setAttribute('class', 'hitbox-polygon');
hitbox.addEventListener('click', handlePolygonClick);
hitbox.addEventListener('mouseover', handlePolygonMouseOver);
hitbox.addEventListener('mouseout', handlePolygonMouseOut);
svgHitboxGroup.appendChild(hitbox);
}
}
}
const svgWidth = (GRID_COLS * HEX_WIDTH) + (HEX_WIDTH / 2);
const svgHeight = ((GRID_ROWS - 1) * HEX_HEIGHT * 0.75) + HEX_HEIGHT;
const padding = HEX_RADIUS;
svg.setAttribute('viewBox', `${-padding} ${-padding} ${svgWidth + padding * 2} ${svgHeight + padding * 2}`);
});
</script>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Hex Sokoban</title>
<style>
body {
font-family: Arial, sans-serif;
display: flex;
flex-direction: column;
align-items: center;
margin: 0;
background-color: #f0f2f5;
}
h1 {
color: #333;
}
#grid-container {
width: 90vw;
max-width: 900px;
height: 80vh;
max-height: 700px;
border: 1px solid #ccc;
box-shadow: 0 4px 8px rgba(0,0,0,0.1);
background-color: #fff;
position: relative;
}
svg {
width: 100%;
height: 100%;
}
.hex-tile {
fill: #eee;
stroke: #ccc;
stroke-width: 1px;
}
.player {
fill: #ff6347;
stroke: #ff4500;
stroke-width: 2px;
}
.crate {
fill: #4682b4;
stroke: #357ec7;
stroke-width: 2px;
}
.crate-edge {
fill: none;
stroke: #fff;
stroke-width: 2px;
}
</style>
</head>
<body>
<h1>Hex Sokoban</h1>
<div id="grid-container">
<svg id="hex-grid-svg"></svg>
</div>
<script>
const GRID_SIZE = 5; // Size of the grid (GRID_SIZE x GRID_SIZE)
const HEX_RADIUS = 30;
const DIRECTIONS = [
{ q: 1, r: 0 }, // E
{ q: 1, r: -1 }, // NE
{ q: 0, r: -1 }, // NW
{ q: -1, r: 0 }, // W
{ q: -1, r: 1 }, // SW
{ q: 0, r: 1 }, // SE
];
let grid = [];
let playerPosition = { q: Math.floor(GRID_SIZE / 2), r: Math.floor(GRID_SIZE / 2) };
let crates = [];
function initGrid() {
for (let q = 0; q < GRID_SIZE; q++) {
grid[q] = [];
for (let r = 0; r < GRID_SIZE; r++) {
grid[q][r] = { crate: null };
}
}
// Place initial crates (example setup)
crates.push({ q: 2, r: 2, edges: new Set([0, 1, 2, 3, 4, 5]) });
crates.push({ q: 2, r: 3, edges: new Set([0, 2, 4]) });
crates.push({ q: 3, r: 2, edges: new Set([1, 3, 5]) });
crates.forEach(crate => {
grid[crate.q][crate.r].crate = crate;
});
}
function hexToPixel(q, r) {
const x = HEX_RADIUS * 1.5 * q;
const y = HEX_RADIUS * 2 * r;
if (r % 2 === 1) {
return { x: x + HEX_RADIUS * 1.5, y };
}
return { x, y };
}
function drawGrid() {
const svg = document.getElementById('hex-grid-svg');
svg.innerHTML = '';
for (let q = 0; q < GRID_SIZE; q++) {
for (let r = 0; r < GRID_SIZE; r++) {
const { x, y } = hexToPixel(q, r);
const hex = document.createElementNS('http://www.w3.org/2000/svg', 'polygon');
const points = getHexPoints(x, y, HEX_RADIUS);
hex.setAttribute('points', points);
hex.setAttribute('class', 'hex-tile');
svg.appendChild(hex);
if (q === playerPosition.q && r === playerPosition.r) {
const playerHex = document.createElementNS('http://www.w3.org/2000/svg', 'polygon');
playerHex.setAttribute('points', points);
playerHex.setAttribute('class', 'player');
svg.appendChild(playerHex);
}
if (grid[q][r].crate) {
const crate = grid[q][r].crate;
const crateHex = document.createElementNS('http://www.w3.org/2000/svg', 'polygon');
crateHex.setAttribute('points', points);
crateHex.setAttribute('class', 'crate');
svg.appendChild(crateHex);
crate.edges.forEach(edgeIndex => {
const edge = document.createElementNS('http://www.w3.org/2000/svg', 'line');
const { x: startX, y: startY } = getEdgeStart(x, y, HEX_RADIUS, edgeIndex);
const { x: endX, y: endY } = getEdgeEnd(x, y, HEX_RADIUS, edgeIndex);
edge.setAttribute('x1', startX);
edge.setAttribute('y1', startY);
edge.setAttribute('x2', endX);
edge.setAttribute('y2', endY);
edge.setAttribute('class', 'crate-edge');
svg.appendChild(edge);
});
}
}
}
}
function getHexPoints(x, y, radius) {
return Array.from({ length: 6 }, (_, i) => {
const angle = Math.PI / 3 * i;
const px = x + radius * Math.cos(angle);
const py = y + radius * Math.sin(angle);
return `${px},${py}`;
}).join(' ');
}
function getEdgeStart(x, y, radius, edgeIndex) {
const angle = Math.PI / 3 * edgeIndex - Math.PI / 6;
return {
x: x + radius * 0.8 * Math.cos(angle),
y: y + radius * 0.8 * Math.sin(angle),
};
}
function getEdgeEnd(x, y, radius, edgeIndex) {
const angle = Math.PI / 3 * edgeIndex - Math.PI / 6;
return {
x: x + radius * 1.2 * Math.cos(angle),
y: y + radius * 1.2 * Math.sin(angle),
};
}
function isInBounds(q, r) {
return q >= 0 && q < GRID_SIZE && r >= 0 && r < GRID_SIZE;
}
function movePlayer(direction) {
const destQ = playerPosition.q + DIRECTIONS[direction].q;
const destR = playerPosition.r + DIRECTIONS[direction].r;
if (!isInBounds(destQ, destR)) return;
const destTile = grid[destQ][destR];
const playerTile = grid[playerPosition.q][playerPosition.r];
if (isPushAttempt(direction, playerTile, destTile)) {
if (isValidPush(direction, destQ, destR)) {
executePush(direction, destQ, destR);
playerPosition = { q: destQ, r: destR };
}
} else {
playerPosition = { q: destQ, r: destR };
}
drawGrid();
checkWin();
}
function isPushAttempt(direction, playerTile, destTile) {
if (playerTile.crate && playerTile.crate.edges.has(direction)) return true;
if (destTile.crate && destTile.crate.edges.has((direction + 3) % 6)) return true;
return false;
}
function isValidPush(direction, destQ, destR) {
let currentQ = destQ + DIRECTIONS[direction].q;
let currentR = destR + DIRECTIONS[direction].r;
while (isInBounds(currentQ, currentR)) {
const currentTile = grid[currentQ][currentR];
if (currentTile.crate) {
if (!currentTile.crate.edges.has((direction + 3) % 6)) return false;
const nextQ = currentQ + DIRECTIONS[direction].q;
const nextR = currentR + DIRECTIONS[direction].r;
if (!isInBounds(nextQ, nextR)) return false;
const nextTile = grid[nextQ][nextR];
if (nextTile.crate && !nextTile.crate.edges.has(direction)) return false;
} else {
break;
}
currentQ += DIRECTIONS[direction].q;
currentR += DIRECTIONS[direction].r;
}
return true;
}
function executePush(direction, destQ, destR) {
let currentQ = destQ + DIRECTIONS[direction].q;
let currentR = destR + DIRECTIONS[direction].r;
while (isInBounds(currentQ, currentR)) {
const currentTile = grid[currentQ][currentR];
if (currentTile.crate) {
const nextQ = currentQ + DIRECTIONS[direction].q;
const nextR = currentR + DIRECTIONS[direction].r;
grid[currentQ][currentR].crate = null;
grid[nextQ][nextR].crate = currentTile.crate;
currentTile.crate.q = nextQ;
currentTile.crate.r = nextR;
currentQ = nextQ;
currentR = nextR;
} else {
break;
}
}
grid[destQ][destR].crate = grid[playerPosition.q][playerPosition.r].crate;
grid[playerPosition.q][playerPosition.r].crate = null;
grid[destQ][destR].crate.q = destQ;
grid[destQ][destR].crate.r = destR;
}
function checkWin() {
for (let q = 0; q < GRID_SIZE; q++) {
for (let r = 0; r < GRID_SIZE; r++) {
const crate = grid[q][r].crate;
if (crate && crate.edges.size === 6) {
alert('You Win!');
return;
}
}
}
}
function handleKeyDown(event) {
const keyMap = {
ArrowRight: 0,
ArrowUp: 1,
ArrowLeft: 3,
ArrowDown: 4,
KeyD: 0,
KeyW: 1,
KeyA: 3,
KeyS: 4,
};
const direction = keyMap[event.code];
if (direction !== undefined) {
movePlayer(direction);
}
}
document.addEventListener('DOMContentLoaded', () => {
initGrid();
drawGrid();
document.addEventListener('keydown', handleKeyDown);
});
</script>
</body>
</html>