From accff86ae105908dcd740ebfc1f0e1af5d26d366 Mon Sep 17 00:00:00 2001
From: Catalin Mititiuc
Date: Wed, 24 Apr 2024 16:18:49 -0700
Subject: Move more firing arc logic to its module
---
src/modules/game.js | 61 ++++++-----------------------------------------------
1 file changed, 7 insertions(+), 54 deletions(-)
(limited to 'src/modules/game.js')
diff --git a/src/modules/game.js b/src/modules/game.js
index 2810fba..fe15055 100644
--- a/src/modules/game.js
+++ b/src/modules/game.js
@@ -8,11 +8,6 @@ export default class Game {
info;
placing = [];
- #firingArcVisibility = {
- davion: false,
- liao: false
- };
-
constructor(svg) {
this.svg = svg;
this.firingArc = new FiringArc(svg);
@@ -50,14 +45,6 @@ export default class Game {
return this.svg.querySelector('line.sight-line');
}
- getExistingArcs({ dataset: { allegiance, number }}) {
- return this.svg.querySelectorAll(`#firing-arcs polygon[data-number="${number}"][data-allegiance="${allegiance}"]`);
- }
-
- getUnclippedFiringArcs() {
- return this.svg.querySelectorAll('#firing-arcs polygon:not([clip-path])');
- }
-
getGridIndex({ parentElement: { dataset: { x }, parentElement: { dataset: { y }}}}) {
return { x, y };
}
@@ -106,7 +93,7 @@ export default class Game {
this.unSelect();
this.placing.push(counter);
counter.classList.add(this.counter.selectedClass);
- this.getExistingArcs(counter).forEach(el => el.removeAttribute('clip-path'));
+ this.firingArc.get(counter).forEach(el => el.removeAttribute('clip-path'));
this.selectCallback({ prone: this.counter.hasProne(counter), ...counter.dataset });
}
}
@@ -118,13 +105,12 @@ export default class Game {
this.placing = [];
this.getSelected().classList.remove(this.counter.selectedClass);
this.sightLine.clear();
- this.clipFiringArcs();
+ this.firingArc.clipAll();
}
}
endTurn(allegiance) {
- const selector = `#firing-arcs [data-allegiance="${allegiance}"]`;
- this.svg.querySelectorAll(selector).forEach(el => el.remove());
+ this.firingArc.clear(allegiance);
}
toggleProne() {
@@ -137,27 +123,7 @@ export default class Game {
}
toggleFiringArcVisibility(allegiance) {
- const vis = this.#firingArcVisibility[allegiance],
- clipPaths = this.svg.querySelectorAll(`clipPath[data-allegiance="${allegiance}"]`);
-
- clipPaths.forEach(cp => cp.style.display = !vis ? 'none' : '');
- this.#firingArcVisibility[allegiance] = !vis;
- }
-
- clipFiringArcs() {
- let unclipped = this.getUnclippedFiringArcs();
-
- unclipped.forEach(el => {
- const { number, allegiance } = el.dataset,
- clipPathId = `clip-path-${allegiance}-${number}`,
- isVisible = this.#firingArcVisibility[allegiance];
-
- if (isVisible) {
- this.svg.querySelector(`#${clipPathId}`).style.display = 'none';
- }
-
- el.setAttributeNS(null, 'clip-path', `url(#${clipPathId})`);
- });
+ this.firingArc.toggleVisibility(allegiance);
}
setUpCells() {
@@ -327,12 +293,7 @@ export default class Game {
let occupant = this.getCellOccupant(cell);
if (occupant) {
- const { number, allegiance } = occupant.dataset;
- const cp = this.svg.querySelector(`#clip-path-${allegiance}-${number}`);
-
- if (cp) {
- cp.style.display = 'none';
- }
+ this.firingArc.toggleCounterVisibility(occupant, true);
}
});
@@ -346,12 +307,7 @@ export default class Game {
let occupant = this.getCellOccupant(cell);
if (occupant) {
- let { number, allegiance } = occupant.dataset,
- cp = this.svg.querySelector(`#clip-path-${allegiance}-${number}`);
-
- if (cp) {
- cp.style.display = this.#firingArcVisibility[allegiance] ? 'none' : '';
- }
+ this.firingArc.toggleCounterVisibility(occupant, false);
}
});
});
@@ -362,10 +318,7 @@ export default class Game {
isOnBoard = counter => counter && counter.parentElement.hasAttribute('data-x');
if (isOnBoard(counter)) {
- const { allegiance, number } = counter.dataset,
- cellPosition = this.getCellPosition(counter.parentElement);
-
- this.firingArc.set(size, allegiance, number, cellPosition);
+ this.firingArc.set(size, counter, this.getCellPosition(counter.parentElement));
}
}
--
cgit v1.2.3