Web Dev Solutions

Catalin Mititiuc

From bdeddc71eb64c60e8a6db64cb401b6e3a5f5571a Mon Sep 17 00:00:00 2001 From: Catalin Mititiuc Date: Wed, 24 Apr 2024 15:08:07 -0700 Subject: Remove references to 'container' in counter.js; fix dblclick not working right --- src/modules/game.js | 54 ++++++++++++++++++++++++++++++++++------------------- 1 file changed, 35 insertions(+), 19 deletions(-) (limited to 'src/modules/game.js') 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: -- cgit v1.2.3