Fix distance calculation

This commit is contained in:
Catalin Constantin Mititiuc 2025-06-16 22:41:33 -07:00
parent b9aa686528
commit 08cdbe0565

View File

@ -90,33 +90,39 @@ function clearSightLine() {
Observable.notify('distance'); Observable.notify('distance');
} }
function getSightLineHexes(source, target) { function calcSightLineIndexes(source, target) {
const { q: sq, r: sr, s: ss } = source.dataset; const { q: sq, r: sr, s: ss } = source.dataset;
const { q: tq, r: tr, s: ts } = target.dataset; const { q: tq, r: tr, s: ts } = target.dataset;
const sourceIndex = { q: +sq, r: +sr, s: +ss }; const sourceIndex = { q: +sq, r: +sr, s: +ss };
const targetIndex = { q: +tq, r: +tr, s: +ts }; const targetIndex = { q: +tq, r: +tr, s: +ts };
const selector = sightLine return sightLine.calcIndexes(sourceIndex, targetIndex);
.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"]`) .map(({ q, r, s }) => `g[data-q="${q}"][data-r="${r}"][data-s="${s}"] use[href="#hex"]`)
.join(', '); .join(', ');
return svg.querySelectorAll(selector); return svg.querySelectorAll(selector);
} }
function updateSightLine(cell) { function calcSightLine(source, target) {
const hexes = getSightLineHexes(cell, sightLine.getLockTarget()); const indexes = calcSightLineIndexes(source, target);
const hexes = getSightLineHexes(indexes);
sightLine.setHexes(hexes); sightLine.setHexes(hexes);
Observable.notify('distance', indexes.length - 1);
}
function updateSightLine(cell) {
calcSightLine(cell, sightLine.getLockTarget());
sightLine.update(getCellPosition(cell)); sightLine.update(getCellPosition(cell));
Observable.notify('distance', hexes.length - 1);
} }
function drawSightLine(sourceCell, targetCell) { function drawSightLine(sourceCell, targetCell) {
const hexes = getSightLineHexes(sourceCell, targetCell); calcSightLine(sourceCell, targetCell)
sightLine.setHexes(hexes);
const line = sightLine.create(getCellPosition(sourceCell), getCellPosition(targetCell)); const line = sightLine.create(getCellPosition(sourceCell), getCellPosition(targetCell));
svg.querySelector('.gameboard').appendChild(line); svg.querySelector('.gameboard').appendChild(line);
Observable.notify('distance', hexes.length - 1);
} }
function moveBackOneStepInHistory(counter) { function moveBackOneStepInHistory(counter) {