index : btroops | |
Virtual board game-aid for BattleTroops, an infantry combat simulator wargame published by FASA in 1989. |
aboutsummaryrefslogtreecommitdiff |
diff options
Diffstat (limited to 'src/modules/firingArc.js')
-rw-r--r-- | src/modules/firingArc.js | 300 |
1 files changed, 0 insertions, 300 deletions
diff --git a/src/modules/firingArc.js b/src/modules/firingArc.js deleted file mode 100644 index 5e5d84a..0000000 --- a/src/modules/firingArc.js +++ /dev/null @@ -1,300 +0,0 @@ -const HORZ_POINT_DISTANCE = 1.005, - VERT_POINT_DISTANCE = Math.sqrt(3) * HORZ_POINT_DISTANCE / 2, - - FIRING_ARC_SIZE = { - 'small': Math.atan(HORZ_POINT_DISTANCE / (6 * VERT_POINT_DISTANCE)), - 'medium': Math.atan((HORZ_POINT_DISTANCE / 2) / VERT_POINT_DISTANCE), - 'large': Math.atan((21 * HORZ_POINT_DISTANCE) / (6 * VERT_POINT_DISTANCE)) -}; - -function calculateAngle(xDiff, yDiff) { - yDiff = -yDiff; - let angle = Math.abs(Math.atan(yDiff / xDiff)); - - if (xDiff < 0 && yDiff > 0) { - angle = Math.PI - angle; - } else if (xDiff < 0 && yDiff < 0) { - angle = Math.PI + angle; - } else if (xDiff > 0 && yDiff < 0) { - angle = 2 * Math.PI - angle; - } - - return angle; -} - -function edgePoint(x1, y1, x2, y2, maxX, maxY) { - let pointCoords, - xDiff = x2 - x1, - yDiff = y2 - y1, - xIntercept = y => (y - y1) * xDiff / yDiff + x1, - yIntercept = x => (x - x1) * yDiff / xDiff + y1; - - if (xDiff > 0 && yDiff > 0) { - let x = xIntercept(maxY); - - pointCoords = x <= maxX ? [x, maxY] : [maxX, yIntercept(maxX)]; - } else if (xDiff > 0 && yDiff < 0) { - let y = yIntercept(maxX); - - pointCoords = y >= 0 ? [maxX, y] : [xIntercept(0), 0]; - } else if (xDiff < 0 && yDiff < 0) { - let x = xIntercept(0); - - pointCoords = x >= 0 ? [x, 0] : [0, yIntercept(0)]; - } else { - let y = yIntercept(0); - - pointCoords = y <= maxY ? [0, y] : [xIntercept(maxY), maxY]; - } - - return pointCoords; -} - -function position(e) { - let activeFiringArc = this.querySelector('polygon.firing-arc.active'); - - // TODO: handle exactly horizontal and vertical lines - - if (activeFiringArc) { - let activeFiringArcOutline = this.querySelector(`#lines polygon[data-number="${activeFiringArc.dataset.number}"][data-allegiance="${activeFiringArc.dataset.allegiance}"]`), - board = this.querySelector('#image-maps'), - { width, height } = board.getBBox(), - pt = new DOMPoint(e.clientX, e.clientY), - { x: pointerX, y: pointerY } = pt.matrixTransform(board.getScreenCTM().inverse()), - [maxXpx, maxYpx] = [width, height], - { x: x1px, y: y1px } = activeFiringArc.points[0]; - - let [x2px, y2px] = [ - pointerX / width * maxXpx, - pointerY / height * maxYpx - ]; - - let xDiff = x2px - x1px; - let yDiff = y2px - y1px; - let angle = calculateAngle(xDiff, yDiff); - - let arcAngle = FIRING_ARC_SIZE[activeFiringArc.dataset.size]; - let distance = Math.sqrt((x2px - x1px) ** 2 + (y2px - y1px) ** 2); - let yDelta = distance * Math.cos(angle) * Math.tan(arcAngle); - let xDelta = distance * Math.sin(angle) * Math.tan(arcAngle); - - let [newY1, newX1] = [y2px + yDelta, x2px + xDelta]; - let [newY2, newX2] = [y2px - yDelta, x2px - xDelta]; - - [newX1, newY1] = edgePoint(x1px, y1px, newX1, newY1, maxXpx, maxYpx); - [newX2, newY2] = edgePoint(x1px, y1px, newX2, newY2, maxXpx, maxYpx); - - let oppositeEdgeConditions = [ - newX1 == 0 && newX2 == maxXpx, - newX2 == 0 && newX1 == maxXpx, - newY1 == 0 && newY2 == maxYpx, - newY2 == 0 && newY1 == maxYpx - ] - - let orthogonalEdgeConditions = [ - (newX1 == 0 || newX1 == maxXpx) && (newY2 == 0 || newY2 == maxYpx), - (newX2 == 0 || newX2 == maxXpx) && (newY1 == 0 || newY1 == maxYpx), - ] - - let points; - - if (oppositeEdgeConditions.some(e => e)) { - let cornerPoints; - - if (xDiff > 0 && yDiff > 0) { - if ((newY1 == 0 && newY2 == maxYpx) || (newY1 == maxYpx && newY2 == 0)) { - cornerPoints = [[maxXpx, 0], [maxXpx, maxYpx]]; - } else { - cornerPoints = [[maxXpx, maxYpx], [0, maxYpx]]; - } - } else if (xDiff > 0 && yDiff < 0) { - if ((newY1 == 0 && newY2 == maxYpx) || (newY1 == maxYpx && newY2 == 0)) { - cornerPoints = [[maxXpx, 0], [maxXpx, maxYpx]]; - } else { - cornerPoints = [[0, 0], [maxXpx, 0]]; - } - - } else if (xDiff < 0 && yDiff > 0) { - if ((newY1 == 0 && newY2 == maxYpx) || (newY1 == maxYpx && newY2 == 0)) { - cornerPoints = [[0, maxYpx], [0, 0]]; - } else { - cornerPoints = [[maxXpx, maxYpx], [0, maxYpx]]; - } - - } else { - if ((newY1 == 0 && newY2 == maxYpx) || (newY1 == maxYpx && newY2 == 0)) { - cornerPoints = [[0, maxYpx], [0, 0]]; - } else { - cornerPoints = [[0, 0], [maxXpx, 0]]; - } - - } - points = `${x1px},${y1px} ${newX1},${newY1} ${cornerPoints[1]} ${cornerPoints[0]} ${newX2},${newY2}`; - } else if (orthogonalEdgeConditions.some(e => e)) { - let cornerPoints = []; - let cp1, cp2; - - if (newX1 == 0 || newX1 == maxXpx) { - cp1 = [newX1, yDiff > 0 ? maxYpx : 0]; - } else { - cp1 = [xDiff > 0 ? maxXpx : 0, newY1]; - } - - if (newX2 == 0 || newX2 == maxXpx) { - cp2 = [newX2, yDiff > 0 ? maxYpx : 0]; - } else { - cp2 = [xDiff > 0 ? maxXpx : 0, newY2]; - } - - if (cp1[0] == cp2[0] && cp1[1] == cp2[1]) { - cornerPoints.push(cp1); - } else { - cornerPoints.push(cp1); - cornerPoints.push([xDiff > 0 ? maxXpx : 0, yDiff > 0 ? maxYpx : 0]) - cornerPoints.push(cp2); - } - - points = `${x1px},${y1px} ${newX1},${newY1} ${cornerPoints.join(' ')} ${newX2},${newY2}`; - } else { - points = `${x1px},${y1px} ${newX1},${newY1} ${newX2},${newY2}`; - } - - activeFiringArcOutline.setAttributeNS(null, 'points', points); - activeFiringArc.setAttributeNS(null, 'points', points); - } -} - -export default class FiringArc { - #firingArcVisibility = { - davion: false, - liao: false - }; - - constructor(svg) { - this.svg = svg; - } - - set(size, { dataset: { allegiance, number }}, { x, y }) { - const svgns = "http://www.w3.org/2000/svg"; - - let existingArcs = this.svg.querySelectorAll( - `#firing-arcs [data-number="${number}"][data-allegiance="${allegiance}"]` - ); - - existingArcs.forEach(el => el.remove()); - - let arcLayer = this.svg.querySelector('#shapes'); - let outlineLayer = this.svg.querySelector('#lines'); - let arcContainer = this.svg.querySelector('#firing-arcs'); - - let grid = this.svg.querySelector('.board'); - const transform = getComputedStyle(grid).transform.match(/-?\d+\.?\d*/g); - const pt = new DOMPoint(x, y); - const mtx = new DOMMatrix(transform); - let tPt = pt.matrixTransform(mtx); - - let pivotPoint = [tPt.x, tPt.y]; - let firingArc = document.createElementNS(svgns, 'polygon'); - let firingArcOutline = document.createElementNS(svgns, 'polygon'); - - firingArc.classList.add('firing-arc', 'active'); - firingArc.dataset.number = number; - firingArc.dataset.allegiance = allegiance; - firingArc.dataset.size = size; - firingArc.setAttributeNS(null, 'points', `${pivotPoint} ${pivotPoint} ${pivotPoint}`); - - firingArcOutline.dataset.number = number; - firingArcOutline.dataset.allegiance = allegiance; - firingArcOutline.setAttributeNS(null, 'points', `${pivotPoint} ${pivotPoint} ${pivotPoint}`); - - let clipShape = document.createElementNS(svgns, 'circle'); - clipShape.setAttributeNS(null, 'cx', tPt.x); - clipShape.setAttributeNS(null, 'cy', tPt.y); - clipShape.setAttributeNS(null, 'r', 100); - - let clipPath = document.createElementNS(svgns, 'clipPath'); - clipPath.setAttributeNS(null, 'id', `clip-path-${allegiance}-${number}`); - clipPath.dataset.number = number; - clipPath.dataset.allegiance = allegiance; - clipPath.appendChild(clipShape); - - arcContainer.appendChild(clipPath); - arcLayer.appendChild(firingArc); - outlineLayer.appendChild(firingArcOutline); - - let firingArcPlacementListener = e => { - this.svg.querySelectorAll('.firing-arc.active').forEach(el => el.classList.remove('active')); - grid.removeAttribute('style'); - this.svg.removeEventListener('mousemove', position); - firingArc.removeEventListener('click', firingArcPlacementListener); - firingArc.removeEventListener('contextmenu', cancelFiringArcPlacement); - }; - - let cancelFiringArcPlacement = e => { - e.preventDefault(); - - firingArc.removeEventListener('click', firingArcPlacementListener); - firingArc.removeEventListener('contextmenu', cancelFiringArcPlacement); - - let existingArcs = this.svg.querySelectorAll( - `#firing-arcs [data-number="${number}"][data-allegiance="${allegiance}"]` - ); - - existingArcs.forEach(el => el.remove()); - - grid.removeAttribute('style'); - this.svg.removeEventListener('mousemove', position); - }; - - grid.style.pointerEvents = 'none'; - this.svg.addEventListener('mousemove', position); - firingArc.addEventListener('click', firingArcPlacementListener); - firingArc.addEventListener('contextmenu', cancelFiringArcPlacement); - } - - clear(allegiance) { - const selector = `#firing-arcs [data-allegiance="${allegiance}"]`; - this.svg.querySelectorAll(selector).forEach(el => el.remove()); - } - - get({ dataset: { allegiance, number }}) { - return this.svg.querySelectorAll(`#firing-arcs polygon[data-number="${number}"][data-allegiance="${allegiance}"]`); - } - - getUnclipped() { - return this.svg.querySelectorAll('#firing-arcs polygon:not([clip-path])'); - } - - toggleVisibility(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; - } - - toggleCounterVisibility({ dataset: { number, allegiance }}, vis) { - const cp = this.svg.querySelector(`#clip-path-${allegiance}-${number}`), - display = vis ? 'none' : ''; - - if (cp) { - cp.style.display = this.#firingArcVisibility[allegiance] ? 'none' : display; - } - } - - clipAll() { - let unclipped = this.getUnclipped(); - - 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})`); - }); - } -} |