Web Dev Solutions

Catalin Mititiuc

aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorCatalin Mititiuc <webdevcat@proton.me>2024-04-24 15:08:07 -0700
committerCatalin Mititiuc <webdevcat@proton.me>2024-04-24 15:08:07 -0700
commitbdeddc71eb64c60e8a6db64cb401b6e3a5f5571a (patch)
tree9ad95a43de060bfef36e12e1c8f357777c79583d /src/modules/game.js
parent525ab3423728a0485c6b64cca00eed59990ddcfc (diff)
Remove references to 'container' in counter.js; fix dblclick not working right
Diffstat (limited to 'src/modules/game.js')
-rw-r--r--src/modules/game.js54
1 files changed, 35 insertions, 19 deletions
diff --git a/src/modules/game.js b/src/modules/game.js
index e40a8a2..d6059ae 100644
--- a/src/modules/game.js
+++ b/src/modules/game.js
@@ -17,7 +17,7 @@ export default class Game {
this.svg = svg;
this.firingArc = new FiringArc(svg);
this.sightLine = new SightLine(svg);
- this.counter = new Counter(svg, this);
+ this.counter = new Counter(svg);
this.setUpCells();
}
@@ -95,17 +95,31 @@ export default class Game {
if (selected) {
this.counter.endMove(selected);
+ this.unSelect();
}
}
- select(allegiance, number) {
- this.placing.push(this.counter.getCounter(allegiance, number));
- this.counter.select({ dataset: { allegiance, number } });
+ select({ dataset: { allegiance, number } }) {
+ const counter = this.counter.getCounter(allegiance, number);
+
+ if (counter) {
+ this.unSelect();
+ this.placing.push(counter);
+ counter.classList.add(this.counter.selectedClass);
+ this.getExistingArcs(allegiance, number).forEach(el => el.removeAttribute('clip-path'));
+ this.selectCallback({ prone: this.counter.hasProne(counter), ...counter.dataset });
+ }
}
unSelect() {
- this.placing = [];
- this.counter.unSelect();
+ const selected = this.getSelected();
+
+ if (selected) {
+ this.placing = [];
+ this.getSelected().classList.remove(this.counter.selectedClass);
+ this.sightLine.clear();
+ this.clipFiringArcs();
+ }
}
endTurn(allegiance) {
@@ -114,7 +128,12 @@ export default class Game {
}
toggleProne() {
- this.counter.toggleProne();
+ const selected = this.getSelected(),
+ isOnBoard = selected && selected.parentElement.hasAttribute('data-x');
+
+ if (selected && isOnBoard) {
+ this.counter.toggleProne(selected);
+ }
}
toggleFiringArcVisibility(allegiance) {
@@ -166,12 +185,14 @@ export default class Game {
contents: this.getCellContents(cell)
};
+ console.log(state);
+
let toPlace = this.placing.pop();
if (isGrenade(toPlace)) {
state.hex.after(toPlace);
} else if (toPlace && !state.occupant) {
- this.counter.place(this.getHex(cell));
+ this.counter.place(toPlace, cell);
this.placing.push(toPlace);
const lockedSl = this.svg.querySelector('.sight-line:not(.active)');
@@ -187,7 +208,7 @@ export default class Game {
toPlace.remove();
toPlace = this.getCounterAtGridIndex(...toPlace.dataset.previous.split(','));
toPlace.classList.remove('clone');
- toPlace.classList.add('selected');
+ toPlace.classList.add(this.counter.selectedClass);
if (!('previous' in toPlace.dataset)) {
trace.remove();
} else {
@@ -204,16 +225,15 @@ export default class Game {
this.sightLine.update(toPlace.parentElement, this.getCellPosition(toPlace.parentElement));
}
} else {
- this.counter.unSelect();
+ this.unSelect();
}
} else if (!state.occupant.classList.contains('clone')) {
- this.counter.select(state.occupant);
- this.placing.push(state.occupant);
+ this.select(state.occupant);
} else {
if (isClone(state.occupant).of(toPlace)) {
if (!('previous' in state.occupant.dataset)) {
state.occupant.classList.remove('clone');
- state.occupant.classList.add('selected');
+ state.occupant.classList.add(this.counter.selectedClass);
toPlace.remove();
toPlace = state.occupant;
this.counter.removeClones(toPlace);
@@ -246,27 +266,23 @@ export default class Game {
this.placing.push(toPlace);
}
} else if (!toPlace && state.occupant) {
- this.counter.select(state.occupant);
- this.placing.push(state.occupant);
+ this.select(state.occupant);
} else {
console.log('removing cell contents');
state.contents.forEach(el => el.remove());
}
});
-
cell.addEventListener('dblclick', e => {
const toPlace = this.placing.pop(),
occupant = this.getCellOccupant(cell);
- if (toPlace == occupant) {
+ if (toPlace && occupant && toPlace == occupant) {
const { number, allegiance } = toPlace.dataset,
selector = `[data-allegiance="${allegiance}"][data-number="${number}"]`;
this.svg.querySelectorAll(selector).forEach(el => el.remove());
}
-
- this.unSelect();
});
// Logic for this event: