Web Dev Solutions

Catalin Mititiuc

aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorCatalin Mititiuc <webdevcat@proton.me>2024-07-25 17:21:31 -0700
committerCatalin Mititiuc <webdevcat@proton.me>2024-07-25 17:21:31 -0700
commit4be3df2bddf9466321b0785c2df15ce487a87a07 (patch)
tree2afb0b884aec9e8de63e40d6c7a6e8f347e9a4f5 /src/modules
parent00581b900f0f1d0dc6e972f80daa8438000115e3 (diff)
Fix distance calculation
Diffstat (limited to 'src/modules')
-rw-r--r--src/modules/gameboard.js24
1 files 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) {