index : btroops | |
Virtual board game-aid for BattleTroops, an infantry combat simulator wargame published by FASA in 1989. |
aboutsummaryrefslogtreecommitdiff |
diff options
author | Catalin Mititiuc <webdevcat@proton.me> | 2024-04-26 23:55:01 -0700 |
---|---|---|
committer | Catalin Mititiuc <webdevcat@proton.me> | 2024-04-26 23:55:01 -0700 |
commit | d911ba9bcaa34d37cd12ad583a88c143e3c138ab (patch) | |
tree | 600e95d9ded6d62e7f52a4ea704808c12cc8dfe3 /src/modules/game | |
parent | bcd34e76db0e169b6e6e1b2d3f50cba35666e4e1 (diff) |
Refactor sight line module to extract svg queries into game module
Diffstat (limited to 'src/modules/game')
-rw-r--r-- | src/modules/game/sightLine.js | 109 |
1 files changed, 42 insertions, 67 deletions
diff --git a/src/modules/game/sightLine.js b/src/modules/game/sightLine.js index 8c3be37..94e91e5 100644 --- a/src/modules/game/sightLine.js +++ b/src/modules/game/sightLine.js @@ -65,7 +65,7 @@ function axial_lerp(q1, r1, q2, r2, t) { return { q: lerp(q1, q2, t), r: lerp(r1, r2, t) }; } -function linedraw(x1, y1, x2, y2) { +function calcIndexes(x1, y1, x2, y2) { const axial1 = evenr_to_axial(x1, y1), axial2 = evenr_to_axial(x2, y2), n = offset_distance(x1, y1, x2, y2), @@ -95,89 +95,64 @@ function create({ x: x1, y: y1 }, { x: x2, y: y2 }) { return sightLine; } -export default function(svg, board) { - let activeHexes = [], - lockTarget, - distanceCallback, - sightLine; - - function drawHexes(...coords) { - clearHexes(); - distanceCallback && distanceCallback(offset_distance(...coords)); - - const lineCoords = linedraw(...coords), +function lock(cell, sightLine, lockTarget) { + sightLine.classList.remove(activeClassName); + cell.classList.add(targetClassName); - selector = lineCoords - .map(([x, y]) => `g[data-y="${y}"] g[data-x="${x}"] use[href="#hex"]`) - .join(', '); + return cell; +} - activeHexes = svg.querySelectorAll(selector); - activeHexes.forEach(p => p.classList.add(activeClassName)); - } +function unlock(sightLine, lockTarget) { + sightLine.classList.add(activeClassName); + lockTarget.classList.remove(targetClassName); - function clearHexes() { - distanceCallback && distanceCallback(); + return null; +} - activeHexes.forEach(el => el.classList.remove(activeClassName)); +export default function (board) { + let sightLine, + lockTarget, activeHexes = []; - } - - function clear() { - if (sightLine) { - sightLine.remove(); - sightLine = null; - } - - if (lockTarget) { - lockTarget.classList.remove(targetClassName); - lockTarget = null; - } - - clearHexes(); - } - function draw(source, target) { - clear(); - - sightLine = create(source.position, target.position); - board.appendChild(sightLine); - - drawHexes(+source.index.x, +source.index.y, +target.index.x, +target.index.y); - } + return { + calcIndexes, - function update(source) { - const { dataset: { x }, parentElement: { dataset: { y }}} = lockTarget; + drawLine: function (...positions) { + this.clear(); - sightLine.setAttributeNS(null, 'x1', source.position.x); - sightLine.setAttributeNS(null, 'y1', source.position.y); + sightLine = create(...positions); + board.appendChild(sightLine); + }, - drawHexes(+source.index.x, +source.index.y, +x, +y); - } + clear: function () { + sightLine && sightLine.remove(); + sightLine = null; - function toggleLock(cell) { - if (lockTarget) { - sightLine.classList.add(activeClassName); - lockTarget.classList.remove(targetClassName); + lockTarget && lockTarget.classList.remove(targetClassName); lockTarget = null; - } else { - sightLine.classList.remove(activeClassName); - cell.classList.add(targetClassName); - lockTarget = cell; - } - } + }, - return { - draw, - clear, - update, - toggleLock, + update: function ({ x, y }) { + sightLine.setAttributeNS(null, 'x1', x); + sightLine.setAttributeNS(null, 'y1', y); + }, + + toggleLock: function (cell) { + lockTarget = lockTarget ? unlock(sightLine, lockTarget) : lock(cell, sightLine, lockTarget); + }, get sightLine() { return sightLine; }, - set distanceCallback(callback) { - distanceCallback = callback; + get lockTarget() { + return lockTarget; + }, + + set hexes(hexes) { + activeHexes.forEach(h => h.classList.remove(activeClassName)); + hexes.forEach(h => h.classList.add(activeClassName)); + activeHexes = hexes; } }; } |