Remove calling sight line module with 'new'

This commit is contained in:
Catalin Constantin Mititiuc 2025-06-16 22:41:29 -07:00
parent 0fbed6a3e7
commit 5f648d48d4
2 changed files with 38 additions and 30 deletions

View File

@ -14,7 +14,10 @@ export default class Game {
const board = this.getBoard(); const board = this.getBoard();
this.firingArc = new FiringArc(svg, board); 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.counter = new Counter(svg);
this.setUpCells(); this.setUpCells();
@ -90,7 +93,7 @@ export default class Game {
* @param {(count: [number]) => void} fn * @param {(count: [number]) => void} fn
*/ */
set distanceCallback(fn) { set distanceCallback(fn) {
this.sightLine.setDistanceCallback(fn); this.sightLine.distanceCallback = fn;
} }
endMove() { endMove() {

View File

@ -82,7 +82,7 @@ function linedraw(x1, y1, x2, y2) {
return results; 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'); const sightLine = document.createElementNS(svgns, 'line');
sightLine.classList.add('sight-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) { export default function(svg, board) {
let activeHexes = [], let activeHexes = [],
lockTarget, lockTarget,
distanceCallback; distanceCallback,
sightLine;
function drawHexes(...coords) { function drawHexes(...coords) {
clearHexes(); clearHexes();
distanceCallback && distanceCallback(offset_distance(...coords));
if (distanceCallback) {
distanceCallback(offset_distance(...coords));
}
const lineCoords = linedraw(...coords), const lineCoords = linedraw(...coords),
@ -118,20 +116,16 @@ export default function(svg, board) {
} }
function clearHexes() { function clearHexes() {
if (distanceCallback) { distanceCallback && distanceCallback();
distanceCallback();
}
activeHexes.forEach(el => el.classList.remove(activeClassName)); activeHexes.forEach(el => el.classList.remove(activeClassName));
activeHexes = []; activeHexes = [];
} }
this.sightLine; function clear() {
if (sightLine) {
this.clear = function () { sightLine.remove();
if (this.sightLine) { sightLine = null;
this.sightLine.remove();
delete this.sightLine;
} }
if (lockTarget) { if (lockTarget) {
@ -142,37 +136,48 @@ export default function(svg, board) {
clearHexes(); clearHexes();
} }
this.draw = function (source, target) { function draw(source, target) {
this.clear(); clear();
this.sightLine = createElement(source.position, target.position); sightLine = create(source.position, target.position);
board.appendChild(this.sightLine); board.appendChild(sightLine);
drawHexes(+source.index.x, +source.index.y, +target.index.x, +target.index.y); 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; const { dataset: { x }, parentElement: { dataset: { y }}} = lockTarget;
this.sightLine.setAttributeNS(null, 'x1', source.position.x); sightLine.setAttributeNS(null, 'x1', source.position.x);
this.sightLine.setAttributeNS(null, 'y1', source.position.y); sightLine.setAttributeNS(null, 'y1', source.position.y);
drawHexes(+source.index.x, +source.index.y, +x, +y); drawHexes(+source.index.x, +source.index.y, +x, +y);
} }
this.toggleLock = function (cell) { function toggleLock(cell) {
if (lockTarget) { if (lockTarget) {
this.sightLine.classList.add(activeClassName); sightLine.classList.add(activeClassName);
lockTarget.classList.remove(targetClassName); lockTarget.classList.remove(targetClassName);
lockTarget = null; lockTarget = null;
} else { } else {
this.sightLine.classList.remove(activeClassName); sightLine.classList.remove(activeClassName);
cell.classList.add(targetClassName); cell.classList.add(targetClassName);
lockTarget = cell; lockTarget = cell;
} }
} }
this.setDistanceCallback = function(callback) { return {
draw: draw,
clear: clear,
update: update,
toggleLock: toggleLock,
get sightLine() {
return sightLine;
},
set distanceCallback(callback) {
distanceCallback = callback; distanceCallback = callback;
} }
};
} }