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-04-26 23:55:01 -0700 |
---|---|---|
committer | Catalin Mititiuc <webdevcat@proton.me> | 2024-04-26 23:55:01 -0700 |
commit | d911ba9bcaa34d37cd12ad583a88c143e3c138ab (patch) | |
tree | 600e95d9ded6d62e7f52a4ea704808c12cc8dfe3 /src/modules/game.js | |
parent | bcd34e76db0e169b6e6e1b2d3f50cba35666e4e1 (diff) |
Refactor sight line module to extract svg queries into game module
Diffstat (limited to 'src/modules/game.js')
-rw-r--r-- | src/modules/game.js | 49 |
1 files changed, 30 insertions, 19 deletions
diff --git a/src/modules/game.js b/src/modules/game.js index b2f0672..1257d90 100644 --- a/src/modules/game.js +++ b/src/modules/game.js @@ -14,7 +14,7 @@ export default class Game { const board = this.getBoard(); this.firingArc = FiringArc(svg, board); - this.sightLine = SightLine(svg, board); + this.sightLine = SightLine(board); this.counter = new Counter(svg); this.setUpCells(); @@ -86,13 +86,6 @@ export default class Game { return pt; } - /** - * @param {(count: [number]) => void} fn - */ - set distanceCallback(fn) { - this.sightLine.distanceCallback = fn; - } - endMove() { const selected = this.getSelected(); @@ -120,7 +113,7 @@ export default class Game { if (selected) { this.placing = []; this.getSelected().classList.remove(this.counter.selectedClass); - this.sightLine.clear(); + this.clearSightLine(); this.firingArc.clipAll(); } } @@ -177,7 +170,7 @@ export default class Game { const lockedSl = this.getLockedSightLine(); if (!lockedSl) { - this.sightLine.clear(); + this.clearSightLine(); } else { this.updateSightLine(cell); } @@ -200,7 +193,7 @@ export default class Game { const lockedSl = this.getLockedSightLine(); if (!lockedSl) { - this.sightLine.clear(); + this.clearSightLine(); } else { this.updateSightLine(toPlace.parentElement); } @@ -221,7 +214,7 @@ export default class Game { const lockedSl = this.getLockedSightLine(); if (!lockedSl) { - this.sightLine.clear(); + this.clearSightLine(); } else { this.updateSightLine(cell); } @@ -296,7 +289,7 @@ export default class Game { let sl = this.getActiveSightLine(); if (sl) { - this.sightLine.clear(); + this.clearSightLine(); } let occupant = this.getCellOccupant(cell); @@ -326,17 +319,35 @@ export default class Game { updateSightLine(cell) { const { dataset: { x: sX }, parentElement: { dataset: { y: sY }}} = cell, - source = { index: { x: sX, y: sY }, position: this.getCellPosition(cell) }; + { dataset: { x: tX }, parentElement: { dataset: { y: tY }}} = this.sightLine.lockTarget; + + const selector = this.sightLine.calcIndexes(+sX, +sY, +tX, +tY) + .map(([x, y]) => `g[data-y="${y}"] g[data-x="${x}"] use[href="#hex"]`) + .join(', '); - this.sightLine.update(source); + const hexes = this.svg.querySelectorAll(selector); + this.sightLine.hexes = hexes; + this.sightLine.update(this.getCellPosition(cell)); + this.distanceCallback && this.distanceCallback(hexes.length - 1); } drawSightLine(sourceCell, targetCell) { const { dataset: { x: sX }, parentElement: { dataset: { y: sY }}} = sourceCell, - { dataset: { x: tX }, parentElement: { dataset: { y: tY }}} = targetCell, - source = { index: { x: sX, y: sY }, position: this.getCellPosition(sourceCell) }, - target = { index: { x: tX, y: tY }, position: this.getCellPosition(targetCell) }; + { dataset: { x: tX }, parentElement: { dataset: { y: tY }}} = targetCell; + + const selector = this.sightLine.calcIndexes(+sX, +sY, +tX, +tY) + .map(([x, y]) => `g[data-y="${y}"] g[data-x="${x}"] use[href="#hex"]`) + .join(', '); + + const hexes = this.svg.querySelectorAll(selector); + this.sightLine.hexes = hexes; + this.sightLine.drawLine(this.getCellPosition(sourceCell), this.getCellPosition(targetCell)); + this.distanceCallback && this.distanceCallback(hexes.length - 1); + } - this.sightLine.draw(source, target); + clearSightLine() { + this.sightLine.hexes = []; + this.sightLine.clear(); + this.distanceCallback && this.distanceCallback(); } } |