Web Dev Solutions

Catalin Mititiuc

aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
Diffstat (limited to 'src/modules')
-rw-r--r--src/modules/game.js7
-rw-r--r--src/modules/game/sightLine.js61
2 files changed, 38 insertions, 30 deletions
diff --git a/src/modules/game.js b/src/modules/game.js
index 260da72..62cca1e 100644
--- a/src/modules/game.js
+++ b/src/modules/game.js
@@ -14,7 +14,10 @@ export default class Game {
const board = this.getBoard();
this.firingArc = new FiringArc(svg, board);
- this.sightLine = new SightLine(svg, board);
+ this.sightLine = SightLine(svg, board);
+
+ console.log(this.sightLine);
+
this.counter = new Counter(svg);
this.setUpCells();
@@ -90,7 +93,7 @@ export default class Game {
* @param {(count: [number]) => void} fn
*/
set distanceCallback(fn) {
- this.sightLine.setDistanceCallback(fn);
+ this.sightLine.distanceCallback = fn;
}
endMove() {
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;
+ }
+ };
}