index : btroops | |
Virtual board game-aid for BattleTroops, an infantry combat simulator wargame published by FASA in 1989. |
aboutsummaryrefslogtreecommitdiff |
diff options
author | Catalin Mititiuc <webdevcat@proton.me> | 2024-05-22 09:12:47 -0700 |
---|---|---|
committer | Catalin Mititiuc <webdevcat@proton.me> | 2024-05-22 09:12:47 -0700 |
commit | 98e4fd58d8c222652f3a5bd1d8a76f7618824e17 (patch) | |
tree | 12f1486c73f39fa5190dd938f99efb0257094761 /src | |
parent | 7395df376deac945fa8f10cbc05c318b1d8dc931 (diff) |
Use observable for end move and prone flag
Diffstat (limited to 'src')
-rw-r--r-- | src/index.js | 12 | ||||
-rw-r--r-- | src/modules/game/soldier.js | 2 | ||||
-rw-r--r-- | src/modules/gameboard.js | 13 | ||||
-rw-r--r-- | src/modules/observable.js | 2 | ||||
-rw-r--r-- | src/modules/record_sheet.js | 4 |
5 files changed, 16 insertions, 17 deletions
diff --git a/src/index.js b/src/index.js index 298f961..53cbf2e 100644 --- a/src/index.js +++ b/src/index.js @@ -2,6 +2,7 @@ import * as panzoom from './modules/pan-zoom.js'; import * as gameboard from './modules/gameboard.js'; import * as recordSheet from './modules/record_sheet.js'; import * as mapSelectDialog from './modules/map_select_dialog.js'; +import { Observable } from './modules/observable.js'; globalThis.svgns = 'http://www.w3.org/2000/svg'; @@ -131,7 +132,7 @@ document.querySelectorAll('.end-turn').forEach(el => clearMoveEndedIndicators(opponentRecords); gameboard.clearFiringArcs(opponent); - gameboard.select(firstOpponentRecord); + Observable.notify('select', firstOpponentRecord); }) ); @@ -157,12 +158,11 @@ gameboard.setDistanceCallback((count = '-') => { distanceOutput.style.display = count === '-' ? 'none' : 'block'; }); -gameboard.setProneFlagCallback(checked => proneToggle.checked = checked); +Observable.subscribe('proneflag', checked => proneToggle.checked = checked); -document.querySelectorAll('.end-move').forEach(el => el.addEventListener('click', () => { - recordSheet.endMove(); - gameboard.endMove(); -})); +document.querySelectorAll('.end-move').forEach(el => + el.addEventListener('click', () => Observable.notify('endmove')) +); document.querySelector('#fullscreen').addEventListener('click', () => { if (!document.fullscreenElement) { diff --git a/src/modules/game/soldier.js b/src/modules/game/soldier.js index fb0a559..ae1d708 100644 --- a/src/modules/game/soldier.js +++ b/src/modules/game/soldier.js @@ -129,7 +129,7 @@ export function endMove(svg, counter) { } export function hasProne(counter) { - return !!counter.querySelector('[href="#counter-prone"]'); + return !!counter.querySelector('[href="#counter-prone"]:not(.removed)'); } export function toggleProne(counter) { diff --git a/src/modules/gameboard.js b/src/modules/gameboard.js index 8f03603..48a32bb 100644 --- a/src/modules/gameboard.js +++ b/src/modules/gameboard.js @@ -3,8 +3,7 @@ import * as sightLine from './game/sight_line.js'; import * as soldier from './game/soldier.js'; import { Observable } from "./observable"; -let svg, distanceCallback, proneFlagCallback, - selected, +let svg, distanceCallback, selected, placing = []; function getCellContents(cell) { @@ -78,6 +77,7 @@ function getSelected() { function deselect() { const selected = getSelected(); placing = []; + Observable.notify('proneflag', false); if (selected) { selected.classList.remove(soldier.getSelectedClass()); @@ -182,10 +182,6 @@ export function setDistanceCallback(callback) { distanceCallback = callback; } -export function setProneFlagCallback(callback) { - proneFlagCallback = callback; -} - export function start(el) { svg = el; @@ -231,6 +227,9 @@ export function start(el) { console.log('removing cell contents'); getCellContents(cell).forEach(el => el.remove()); } + + const selected = getSelected(); + Observable.notify('proneflag', selected && soldier.hasProne(selected)); }); cell.addEventListener('dblclick', e => { @@ -280,6 +279,7 @@ export function start(el) { }); Observable.subscribe('select', select); + Observable.subscribe('endmove', endMove); console.log('gameboard.js loaded'); } @@ -295,6 +295,7 @@ export function select(data) { counter.classList.add(soldier.getSelectedClass()); firingArc.get(svg, counter).forEach(el => el.removeAttribute('clip-path')); + Observable.notify('proneflag', soldier.hasProne(counter)); placing.push(counter); } diff --git a/src/modules/observable.js b/src/modules/observable.js index e4f6797..c9bea9b 100644 --- a/src/modules/observable.js +++ b/src/modules/observable.js @@ -2,7 +2,7 @@ const observers = {}; export const Observable = Object.freeze({ notify: (event, data) => { - observers[event].forEach((observer) => observer(data)) + (observers[event] || []).forEach((observer) => observer(data)) }, subscribe: (event, func) => { diff --git a/src/modules/record_sheet.js b/src/modules/record_sheet.js index 7b0a80f..b180a8e 100644 --- a/src/modules/record_sheet.js +++ b/src/modules/record_sheet.js @@ -118,8 +118,6 @@ function deselect() { if (selected) { selected.classList.remove('selected'); } - - document.getElementById('toggle-prone-counter').checked = false; } function clear() { @@ -137,7 +135,6 @@ function select(data) { if (isSelected || !data) return; record.classList.add('selected'); - document.querySelector('#toggle-prone-counter').checked = data.prone; } export function getSelected() { @@ -170,5 +167,6 @@ export function start(startLoc, units) { } Observable.subscribe('select', select); + Observable.subscribe('endmove', endMove); addEventListeners(); } |