index : btroops | |
Virtual board game-aid for BattleTroops, an infantry combat simulator wargame published by FASA in 1989. |
aboutsummaryrefslogtreecommitdiff |
diff options
Diffstat (limited to 'src/modules/gameboard.js')
-rw-r--r-- | src/modules/gameboard.js | 60 |
1 files changed, 29 insertions, 31 deletions
diff --git a/src/modules/gameboard.js b/src/modules/gameboard.js index abf37b0..8f03603 100644 --- a/src/modules/gameboard.js +++ b/src/modules/gameboard.js @@ -1,8 +1,9 @@ import * as firingArc from './game/firing_arc.js'; import * as sightLine from './game/sight_line.js'; import * as soldier from './game/soldier.js'; +import { Observable } from "./observable"; -let svg, distanceCallback, proneFlagCallback, selectCallback, +let svg, distanceCallback, proneFlagCallback, selected, placing = []; @@ -74,6 +75,17 @@ function getSelected() { return svg.querySelector(`.counter.selected[data-allegiance][data-number]`); } +function deselect() { + const selected = getSelected(); + placing = []; + + if (selected) { + selected.classList.remove(soldier.getSelectedClass()); + clearSightLine(); + firingArc.clipAll(svg); + } +} + function clearSightLine() { sightLine.setHexes([]); sightLine.clear(); @@ -159,7 +171,7 @@ function hasPreviousMoveInHistory(counter) { } function selectOffBoard() { - this.classList.contains(soldier.getSelectedClass()) ? unSelect() : select(this); + Observable.notify('select', this); } export function getUnits() { @@ -174,10 +186,6 @@ export function setProneFlagCallback(callback) { proneFlagCallback = callback; } -export function setSelectCallback(callback) { - selectCallback = callback; -} - export function start(el) { svg = el; @@ -202,10 +210,10 @@ export function start(el) { placing.push(toPlace); getLockedSightLine(svg) ? updateSightLine(toPlace.parentElement) : drawSightLine(toPlace.parentElement, cell); } else { - unSelect(); + deselect(); } } else if (!occupant.classList.contains('clone')) { - select(occupant); + Observable.notify('select', occupant); } else { if (isClone(occupant).of(toPlace)) { if (hasPreviousMoveInHistory(occupant)) { @@ -218,7 +226,7 @@ export function start(el) { placing.push(toPlace); } } else if (!toPlace && occupant) { - select(occupant); + Observable.notify('select', occupant); } else { console.log('removing cell contents'); getCellContents(cell).forEach(el => el.remove()); @@ -234,7 +242,7 @@ export function start(el) { selector = `[data-allegiance="${allegiance}"][data-number="${number}"]`; svg.querySelectorAll(selector).forEach(el => el.remove()); - selectCallback(); + Observable.notify('select'); } }); @@ -271,33 +279,23 @@ export function start(el) { }); }); + Observable.subscribe('select', select); console.log('gameboard.js loaded'); } -export function select(selected) { - unSelect(); - let counter = soldier.getCounter(svg, selected); +export function select(data) { + if (!data) return; - if (counter) { - firingArc.get(svg, counter).forEach(el => el.removeAttribute('clip-path')); - } else { - counter = soldier.createCounter(selected); - } + const counter = soldier.getCounter(svg, data) || soldier.createCounter(data); + const isSelected = counter.classList.contains(soldier.getSelectedClass()); - placing.push(counter); - counter.classList.add(soldier.getSelectedClass()); - selectCallback && selectCallback({ prone: soldier.hasProne(counter), ...counter.dataset }); -} + deselect(); -export function unSelect() { - const selected = getSelected(); - placing = []; + if (isSelected) return; - if (selected) { - getSelected().classList.remove(soldier.getSelectedClass()); - clearSightLine(); - firingArc.clipAll(svg); - } + counter.classList.add(soldier.getSelectedClass()); + firingArc.get(svg, counter).forEach(el => el.removeAttribute('clip-path')); + placing.push(counter); } export function endMove() { @@ -305,7 +303,7 @@ export function endMove() { if (selected) { soldier.endMove(svg, selected); - unSelect(); + deselect(); } } |