Web Dev Solutions

Catalin Mititiuc

aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
Diffstat (limited to 'src/modules/game.js')
-rw-r--r--src/modules/game.js49
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();
}
}