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/game')
-rw-r--r-- | src/modules/game/sightLine.js | 61 |
1 files changed, 33 insertions, 28 deletions
diff --git a/src/modules/game/sightLine.js b/src/modules/game/sightLine.js index b0af107..ae46265 100644 --- a/src/modules/game/sightLine.js +++ b/src/modules/game/sightLine.js @@ -82,7 +82,7 @@ function linedraw(x1, y1, x2, y2) { return results; } -function createElement({ x: x1, y: y1 }, { x: x2, y: y2 }) { +function create({ x: x1, y: y1 }, { x: x2, y: y2 }) { const sightLine = document.createElementNS(svgns, 'line'); sightLine.classList.add('sight-line'); @@ -98,14 +98,12 @@ function createElement({ x: x1, y: y1 }, { x: x2, y: y2 }) { export default function(svg, board) { let activeHexes = [], lockTarget, - distanceCallback; + distanceCallback, + sightLine; function drawHexes(...coords) { clearHexes(); - - if (distanceCallback) { - distanceCallback(offset_distance(...coords)); - } + distanceCallback && distanceCallback(offset_distance(...coords)); const lineCoords = linedraw(...coords), @@ -118,20 +116,16 @@ export default function(svg, board) { } function clearHexes() { - if (distanceCallback) { - distanceCallback(); - } + distanceCallback && distanceCallback(); activeHexes.forEach(el => el.classList.remove(activeClassName)); activeHexes = []; } - this.sightLine; - - this.clear = function () { - if (this.sightLine) { - this.sightLine.remove(); - delete this.sightLine; + function clear() { + if (sightLine) { + sightLine.remove(); + sightLine = null; } if (lockTarget) { @@ -142,37 +136,48 @@ export default function(svg, board) { clearHexes(); } - this.draw = function (source, target) { - this.clear(); + function draw(source, target) { + clear(); - this.sightLine = createElement(source.position, target.position); - board.appendChild(this.sightLine); + sightLine = create(source.position, target.position); + board.appendChild(sightLine); drawHexes(+source.index.x, +source.index.y, +target.index.x, +target.index.y); } - this.update = function(source) { + function update(source) { const { dataset: { x }, parentElement: { dataset: { y }}} = lockTarget; - this.sightLine.setAttributeNS(null, 'x1', source.position.x); - this.sightLine.setAttributeNS(null, 'y1', source.position.y); + sightLine.setAttributeNS(null, 'x1', source.position.x); + sightLine.setAttributeNS(null, 'y1', source.position.y); drawHexes(+source.index.x, +source.index.y, +x, +y); } - this.toggleLock = function (cell) { + function toggleLock(cell) { if (lockTarget) { - this.sightLine.classList.add(activeClassName); + sightLine.classList.add(activeClassName); lockTarget.classList.remove(targetClassName); lockTarget = null; } else { - this.sightLine.classList.remove(activeClassName); + sightLine.classList.remove(activeClassName); cell.classList.add(targetClassName); lockTarget = cell; } } - this.setDistanceCallback = function(callback) { - distanceCallback = callback; - } + return { + draw: draw, + clear: clear, + update: update, + toggleLock: toggleLock, + + get sightLine() { + return sightLine; + }, + + set distanceCallback(callback) { + distanceCallback = callback; + } + }; } |