diff options
Diffstat (limited to 'html/rogue/js/hex.js')
-rw-r--r-- | html/rogue/js/hex.js | 43 |
1 files changed, 37 insertions, 6 deletions
diff --git a/html/rogue/js/hex.js b/html/rogue/js/hex.js index 0d1c2e5..fa08e3d 100644 --- a/html/rogue/js/hex.js +++ b/html/rogue/js/hex.js @@ -1,20 +1,20 @@ -// Hex grid utilities and calculations +// This that witchy shit -- we be hexin! + 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 + // hex to pixel 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 + // pixel to hex fromPixel(x, y) { const q = (2/3 * x) / this.SIZE; const r = (-1/3 * x + Math.sqrt(3)/3 * y) / this.SIZE; @@ -46,7 +46,7 @@ const HexGrid = { return {q: rx, r: rz}; }, - // Calculate visible hexes + // Is this hex in the viewport? getViewportHexes() { const hexes = []; const halfGrid = Math.floor(this.GRID_SIZE / 2); @@ -59,9 +59,40 @@ const HexGrid = { return hexes; }, - // Add this method to check if a hex is passable + // 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; + }, + + // Centralized hex drawing function + drawHexPath(ctx, x, y, size = this.SIZE, padding = 0) { + ctx.beginPath(); + for (let i = 0; i < 6; i++) { + const angle = 2 * Math.PI / 6 * i; + const xPos = Math.round(x + (size + padding) * Math.cos(angle)); + const yPos = Math.round(y + (size + padding) * Math.sin(angle)); + if (i === 0) { + ctx.moveTo(xPos, yPos); + } else { + ctx.lineTo(xPos, yPos); + } + } + ctx.closePath(); + }, + + toScreenCoordinates(hex, camera) { + const pixel = this.toPixel(hex); + return { + x: Math.round(pixel.x - camera.x), + y: Math.round(pixel.y - camera.y) + }; + }, + + isInViewport(screenX, screenY, canvas) { + return !(screenX < -this.WIDTH || + screenX > canvas.width + this.WIDTH || + screenY < -this.HEIGHT || + screenY > canvas.height + this.HEIGHT); } }; \ No newline at end of file |