Use observable for showing hex distance count

This commit is contained in:
Catalin Constantin Mititiuc 2025-06-16 22:41:31 -07:00
parent 5453b87db7
commit 96739897ec
2 changed files with 33 additions and 37 deletions

View File

@ -72,6 +72,11 @@ function clearMoveEndedIndicators(records) {
records.forEach(el => el.classList.remove('movement-ended')); records.forEach(el => el.classList.remove('movement-ended'));
} }
function distance(count = '-') {
distanceOutput.querySelector('#hex-count').textContent = count;
distanceOutput.style.display = count === '-' ? 'none' : 'block';
}
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/random#getting_a_random_integer_between_two_values_inclusive // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/random#getting_a_random_integer_between_two_values_inclusive
function getRandomIntInclusive(min, max) { function getRandomIntInclusive(min, max) {
const minCeiled = Math.ceil(min); const minCeiled = Math.ceil(min);
@ -151,15 +156,6 @@ document.getElementById('toggle-prone-counter').addEventListener('input', functi
selected && gameboard.toggleProne(); selected && gameboard.toggleProne();
}); });
contentVisToggleEl.addEventListener('input', toggleContentVis);
gameboard.setDistanceCallback((count = '-') => {
distanceOutput.querySelector('#hex-count').textContent = count;
distanceOutput.style.display = count === '-' ? 'none' : 'block';
});
Observable.subscribe('proneflag', checked => proneToggle.checked = checked);
document.querySelectorAll('.end-move').forEach(el => document.querySelectorAll('.end-move').forEach(el =>
el.addEventListener('click', () => Observable.notify('endmove')) el.addEventListener('click', () => Observable.notify('endmove'))
); );
@ -172,20 +168,6 @@ document.querySelector('#fullscreen').addEventListener('click', () => {
} }
}); });
contentVisToggleEl.checked = (localStorage.getItem('content-visibility') !== 'false');
toggleContentVis();
mapSelectDialog
.init()
.selectCurrentOptionOnPageLoad()
.showOnClick()
.updateValueOnSelection()
.changeMapOnConfirm(loadScenario);
mapResourceEl.addEventListener('load', load);
mapResourceEl.data = map;
mapResourceEl = null;
document.querySelector('#download-save').addEventListener('click', e => { document.querySelector('#download-save').addEventListener('click', e => {
const data = document.querySelector('object').contentDocument.documentElement.outerHTML; const data = document.querySelector('object').contentDocument.documentElement.outerHTML;
const element = document.createElement('a'); const element = document.createElement('a');
@ -208,6 +190,28 @@ document.querySelector('input[type="file"]').addEventListener('change', e => {
loadScenario(URL.createObjectURL(file)) loadScenario(URL.createObjectURL(file))
}); });
document.querySelector('#roll-dice').addEventListener('click', () => {
dice.forEach(el => {
el.classList.remove('roll-in');
el.classList.add('roll-out');
});
});
contentVisToggleEl.addEventListener('input', toggleContentVis);
contentVisToggleEl.checked = (localStorage.getItem('content-visibility') !== 'false');
toggleContentVis();
mapSelectDialog
.init()
.selectCurrentOptionOnPageLoad()
.showOnClick()
.updateValueOnSelection()
.changeMapOnConfirm(loadScenario);
mapResourceEl.addEventListener('load', load);
mapResourceEl.data = map;
mapResourceEl = null;
dice.forEach(el => { dice.forEach(el => {
el.classList.add(roll(d6)); el.classList.add(roll(d6));
@ -220,9 +224,5 @@ dice.forEach(el => {
}); });
}); });
document.querySelector('#roll-dice').addEventListener('click', () => { Observable.subscribe('distance', distance);
dice.forEach(el => { Observable.subscribe('proneflag', checked => proneToggle.checked = checked);
el.classList.remove('roll-in');
el.classList.add('roll-out');
});
});

View File

@ -3,7 +3,7 @@ import * as sightLine from './game/sight_line.js';
import * as soldier from './game/soldier.js'; import * as soldier from './game/soldier.js';
import { Observable } from "./observable"; import { Observable } from "./observable";
let svg, distanceCallback, selected, let svg, selected,
placing = []; placing = [];
function getCellContents(cell) { function getCellContents(cell) {
@ -89,7 +89,7 @@ function deselect() {
function clearSightLine() { function clearSightLine() {
sightLine.setHexes([]); sightLine.setHexes([]);
sightLine.clear(); sightLine.clear();
distanceCallback && distanceCallback(); Observable.notify('distance');
} }
function updateSightLine(cell) { function updateSightLine(cell) {
@ -103,7 +103,7 @@ function updateSightLine(cell) {
const hexes = svg.querySelectorAll(selector); const hexes = svg.querySelectorAll(selector);
sightLine.setHexes(hexes); sightLine.setHexes(hexes);
sightLine.update(getCellPosition(cell)); sightLine.update(getCellPosition(cell));
distanceCallback && distanceCallback(hexes.length - 1); Observable.notify('distance', hexes.length - 1);
} }
function drawSightLine(sourceCell, targetCell) { function drawSightLine(sourceCell, targetCell) {
@ -118,7 +118,7 @@ function drawSightLine(sourceCell, targetCell) {
sightLine.setHexes(hexes); 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);
distanceCallback && distanceCallback(hexes.length - 1); Observable.notify('distance', hexes.length - 1);
} }
function moveBackOneStepInHistory(counter) { function moveBackOneStepInHistory(counter) {
@ -178,10 +178,6 @@ export function getUnits() {
return soldier.getAllCounters(svg); return soldier.getAllCounters(svg);
} }
export function setDistanceCallback(callback) {
distanceCallback = callback;
}
export function start(el) { export function start(el) {
svg = el; svg = el;