index : btroops | |
Virtual board game-aid for BattleTroops, an infantry combat simulator wargame published by FASA in 1989. |
aboutsummaryrefslogtreecommitdiff |
diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/radial.js | 124 |
1 files changed, 69 insertions, 55 deletions
diff --git a/src/radial.js b/src/radial.js index 6cf9058..dd368f7 100644 --- a/src/radial.js +++ b/src/radial.js @@ -3,10 +3,26 @@ if (window.IS_DEV) { source.addEventListener('change', () => location.reload()); } +const xmlns = 'http://www.w3.org/2000/svg'; +const svg = document.querySelector('svg'); + +const hex = { + inradius: 8.66, + circumradius: 10, +} + +const horzSpacing = hex.inradius; +const vertSpacing = hex.circumradius * 3 / 2; + + function toKey(q, r, s) { return `${[q, r, s]}`; } +function sameSigns(a, b) { + return a > -1 && b > -1 || a < 0 && b < 0; +} + function getNeighbors(coords) { const [q, r, s] = coords.split(',').map(n => +n); @@ -45,19 +61,21 @@ function generateRadialCoords(l, { q, r, s }, radius) { return list; } -function sameSigns(a, b) { - return a > -1 && b > -1 || a < 0 && b < 0; -} - -function generateRadialCoordsRect({ rows, columns }, { q = 0, r = 0, s = 0 } = {}) { +function generateRadialCoordsRect({ rows, columns, odd = false } = {}, { q = 0, r = 0, s = 0 } = {}) { const list = new Set(); for (let col = 0; col < columns; col++) { - for (let rw = 0; rw < rows; rw++) { - const alternate = rw % 2; - const dq = -(alternate + rw) / 2 + alternate - col; - const dr = rw; - const ds = -(rw + alternate) / 2 + col; + for (let row = 0; row < rows; row++) { + const alternating = row % 2; + const dr = row; + + let dq = -(alternating + row) / 2 + alternating - col; + let ds = -(row + alternating) / 2 + col; + + if (odd) { + dq = dq - alternating; + ds = ds + alternating; + } list.add(toKey(q + dq, r + dr, s + ds)); } @@ -66,60 +84,56 @@ function generateRadialCoordsRect({ rows, columns }, { q = 0, r = 0, s = 0 } = { return list; } -const hex = { - inradius: 8.66, - circumradius: 10, -} - -const horzSpacing = hex.inradius; -const vertSpacing = hex.circumradius * 3 / 2; +function drawHexes(el, list) { + list.forEach(key => { + const [q, r, s] = key.split(',').map(n => +n); -// let list = generateRadialCoords(new Map(), { q: 2, r: 0, s: -2 }, 1); -// list = generateRadialCoords(list, { q: -2, r: 0, s: 2 }, 1); -// list = generateRadialCoords(list, { q: 0, r: 0, s: 0 }, 0); -const list = generateRadialCoordsRect({ rows: 4, columns: 5 }, { q: 7, r: -4, s: -3 }); -const svg = document.querySelector('svg'); -const xmlns = 'http://www.w3.org/2000/svg'; + let x; -// const map = new Map(); + if (q === s) + x = 0; + else if (sameSigns(q, s)) + x = Math.abs(q - s); + else + x = Math.abs(q) + Math.abs(s); -list.forEach(key => { - const [q, r, s] = key.split(',').map(n => +n); + x = (q > s ? -1 : 1) * x * horzSpacing; + y = r * vertSpacing; - let x; + const g = document.createElementNS(xmlns, 'g'); + g.setAttributeNS(null, 'transform', `translate(${x}, ${y})`); - if (q === s) - x = 0; - else if (sameSigns(q, s)) - x = Math.abs(q - s); - else - x = Math.abs(q) + Math.abs(s); + const use = document.createElementNS(xmlns, 'use'); + use.setAttributeNS(null, 'href', '#hex'); - x = (q > s ? -1 : 1) * x * horzSpacing; - y = r * vertSpacing; + const qText = document.createElementNS(xmlns, 'text'); + qText.textContent = q; + qText.setAttributeNS(null, 'x', -3); + qText.setAttributeNS(null, 'y', -3); - const g = document.createElementNS(xmlns, 'g'); - g.setAttributeNS(null, 'transform', `translate(${x}, ${y})`); + const rText = document.createElementNS(xmlns, 'text'); + rText.textContent = r; + rText.setAttributeNS(null, 'x', 5); + rText.setAttributeNS(null, 'y', 1.5); - const use = document.createElementNS(xmlns, 'use'); - use.setAttributeNS(null, 'href', '#hex'); + const sText = document.createElementNS(xmlns, 'text'); + sText.textContent = s; + sText.setAttributeNS(null, 'x', -3); + sText.setAttributeNS(null, 'y', + 5); - const qText = document.createElementNS(xmlns, 'text'); - qText.textContent = q; - qText.setAttributeNS(null, 'x', -3); - qText.setAttributeNS(null, 'y', -3); + [use, qText, rText, sText].forEach(el => g.appendChild(el)); + el.appendChild(g); + // map.set(key, g); + }); +} - const rText = document.createElementNS(xmlns, 'text'); - rText.textContent = r; - rText.setAttributeNS(null, 'x', 5); - rText.setAttributeNS(null, 'y', 1.5); +const grid = generateRadialCoordsRect({ rows: 10, columns: 14 }, { q: 9, r: -5, s: -4 }); +const building1 = generateRadialCoordsRect({ rows: 4, columns: 3 }, { q: 6, r: -3, s: -3 }); +const building2 = generateRadialCoordsRect({ rows: 4, columns: 3, odd: true }, { q: 0, r: -4, s: 4 }); +const building3 = generateRadialCoords(new Map(), { q: -1, r: 2, s: -1 }, 1); - const sText = document.createElementNS(xmlns, 'text'); - sText.textContent = s; - sText.setAttributeNS(null, 'x', -3); - sText.setAttributeNS(null, 'y', + 5); +building1.forEach(coords => grid.delete(coords)); +building2.forEach(coords => grid.delete(coords)); +building3.forEach(coords => grid.delete(coords)); - [use, qText, rText, sText].forEach(el => g.appendChild(el)); - svg.appendChild(g); - // map.set(key, g); -}); +drawHexes(svg, grid); |