From 4be3df2bddf9466321b0785c2df15ce487a87a07 Mon Sep 17 00:00:00 2001
From: Catalin Mititiuc
Date: Thu, 25 Jul 2024 17:21:31 -0700
Subject: Fix distance calculation
---
src/modules/gameboard.js | 24 +++++++++++++++---------
1 file changed, 15 insertions(+), 9 deletions(-)
diff --git a/src/modules/gameboard.js b/src/modules/gameboard.js
index 4d6790a..693c783 100644
--- a/src/modules/gameboard.js
+++ b/src/modules/gameboard.js
@@ -90,33 +90,39 @@ function clearSightLine() {
Observable.notify('distance');
}
-function getSightLineHexes(source, target) {
+function calcSightLineIndexes(source, target) {
const { q: sq, r: sr, s: ss } = source.dataset;
const { q: tq, r: tr, s: ts } = target.dataset;
const sourceIndex = { q: +sq, r: +sr, s: +ss };
const targetIndex = { q: +tq, r: +tr, s: +ts };
- const selector = sightLine
- .calcIndexes(sourceIndex, targetIndex)
+ return sightLine.calcIndexes(sourceIndex, targetIndex);
+}
+
+function getSightLineHexes(indexes) {
+ const selector = indexes
.map(({ q, r, s }) => `g[data-q="${q}"][data-r="${r}"][data-s="${s}"] use[href="#hex"]`)
.join(', ');
return svg.querySelectorAll(selector);
}
-function updateSightLine(cell) {
- const hexes = getSightLineHexes(cell, sightLine.getLockTarget());
+function calcSightLine(source, target) {
+ const indexes = calcSightLineIndexes(source, target);
+ const hexes = getSightLineHexes(indexes);
sightLine.setHexes(hexes);
+ Observable.notify('distance', indexes.length - 1);
+}
+
+function updateSightLine(cell) {
+ calcSightLine(cell, sightLine.getLockTarget());
sightLine.update(getCellPosition(cell));
- Observable.notify('distance', hexes.length - 1);
}
function drawSightLine(sourceCell, targetCell) {
- const hexes = getSightLineHexes(sourceCell, targetCell);
- sightLine.setHexes(hexes);
+ calcSightLine(sourceCell, targetCell)
const line = sightLine.create(getCellPosition(sourceCell), getCellPosition(targetCell));
svg.querySelector('.gameboard').appendChild(line);
- Observable.notify('distance', hexes.length - 1);
}
function moveBackOneStepInHistory(counter) {
--
cgit v1.2.3