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');
}
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) {