From d911ba9bcaa34d37cd12ad583a88c143e3c138ab Mon Sep 17 00:00:00 2001
From: Catalin Mititiuc
Date: Fri, 26 Apr 2024 23:55:01 -0700
Subject: Refactor sight line module to extract svg queries into game module
---
src/modules/game.js | 49 ++++++++++++++++++++++++++++++-------------------
1 file changed, 30 insertions(+), 19 deletions(-)
(limited to 'src/modules/game.js')
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();
}
}
--
cgit v1.2.3