Remove 'reveal pattern' from sight line module

This commit is contained in:
Catalin Constantin Mititiuc 2024-04-27 12:24:51 -07:00
parent 77f9f8afa4
commit 25c74d9a8e
2 changed files with 63 additions and 75 deletions

View File

@ -1,5 +1,5 @@
import * as firingArc from './game/firingArc.js'; import * as firingArc from './game/firingArc.js';
import SightLine from './game/sightLine.js'; import * as sightLine from './game/sightLine.js';
import Counter from './game/counter.js'; import Counter from './game/counter.js';
const svgns = "http://www.w3.org/2000/svg"; const svgns = "http://www.w3.org/2000/svg";
@ -77,21 +77,21 @@ function getSelected() {
} }
function clearSightLine() { function clearSightLine() {
sightLine.hexes = []; sightLine.setHexes([]);
sightLine.clear(); sightLine.clear();
distanceCallback && distanceCallback(); distanceCallback && distanceCallback();
} }
function updateSightLine(cell) { function updateSightLine(cell) {
const { dataset: { x: sX }, parentElement: { dataset: { y: sY }}} = cell, const { dataset: { x: sX }, parentElement: { dataset: { y: sY }}} = cell,
{ dataset: { x: tX }, parentElement: { dataset: { y: tY }}} = sightLine.lockTarget; { dataset: { x: tX }, parentElement: { dataset: { y: tY }}} = sightLine.getLockTarget();
const selector = sightLine.calcIndexes(+sX, +sY, +tX, +tY) const selector = sightLine.calcIndexes(+sX, +sY, +tX, +tY)
.map(([x, y]) => `g[data-y="${y}"] g[data-x="${x}"] use[href="#hex"]`) .map(([x, y]) => `g[data-y="${y}"] g[data-x="${x}"] use[href="#hex"]`)
.join(', '); .join(', ');
const hexes = svg.querySelectorAll(selector); const hexes = svg.querySelectorAll(selector);
sightLine.hexes = hexes; sightLine.setHexes(hexes);
sightLine.update(getCellPosition(cell)); sightLine.update(getCellPosition(cell));
distanceCallback && distanceCallback(hexes.length - 1); distanceCallback && distanceCallback(hexes.length - 1);
} }
@ -105,14 +105,15 @@ function drawSightLine(sourceCell, targetCell) {
.join(', '); .join(', ');
const hexes = svg.querySelectorAll(selector); const hexes = svg.querySelectorAll(selector);
sightLine.hexes = hexes; sightLine.setHexes(hexes);
sightLine.drawLine(getCellPosition(sourceCell), getCellPosition(targetCell)); const line = sightLine.create(getCellPosition(sourceCell), getCellPosition(targetCell));
svg.querySelector('.board').appendChild(line);
distanceCallback && distanceCallback(hexes.length - 1); distanceCallback && distanceCallback(hexes.length - 1);
} }
let svg, distanceCallback, proneFlagCallback, selectCallback; let svg, distanceCallback, proneFlagCallback, selectCallback;
let board, sightLine, counterMod, let board, counterMod,
placing = []; placing = [];
export function setDistanceCallback(callback) { export function setDistanceCallback(callback) {
@ -130,7 +131,6 @@ export function setSelectCallback(callback) {
export function start(el) { export function start(el) {
svg = el; svg = el;
board = svg.querySelector('.board'); board = svg.querySelector('.board');
sightLine = SightLine(board);
counterMod = Counter(svg, board); counterMod = Counter(svg, board);
getCells(svg).forEach(cell => { getCells(svg).forEach(cell => {

View File

@ -65,7 +65,39 @@ function axial_lerp(q1, r1, q2, r2, t) {
return { q: lerp(q1, q2, t), r: lerp(r1, r2, t) }; return { q: lerp(q1, q2, t), r: lerp(r1, r2, t) };
} }
function calcIndexes(x1, y1, x2, y2) { function lock(sightLine, cell) {
sightLine.classList.remove(activeClassName);
cell.classList.add(targetClassName);
return cell;
}
function unlock(sightLine, lockTarget) {
sightLine.classList.add(activeClassName);
lockTarget.classList.remove(targetClassName);
return null;
}
let sightLine, lockTarget,
activeHexes = [];
export function create({ x: x1, y: y1 }, { x: x2, y: y2 }) {
const line = document.createElementNS(svgns, 'line');
line.classList.add('sight-line');
line.classList.add(activeClassName);
line.setAttributeNS(null, 'x1', x1);
line.setAttributeNS(null, 'y1', y1);
line.setAttributeNS(null, 'x2', x2);
line.setAttributeNS(null, 'y2', y2);
sightLine = line;
return line;
}
export function calcIndexes(x1, y1, x2, y2) {
const axial1 = evenr_to_axial(x1, y1), const axial1 = evenr_to_axial(x1, y1),
axial2 = evenr_to_axial(x2, y2), axial2 = evenr_to_axial(x2, y2),
n = offset_distance(x1, y1, x2, y2), n = offset_distance(x1, y1, x2, y2),
@ -82,77 +114,33 @@ function calcIndexes(x1, y1, x2, y2) {
return results; return results;
} }
function create({ x: x1, y: y1 }, { x: x2, y: y2 }) { export function clear() {
const sightLine = document.createElementNS(svgns, 'line'); sightLine && sightLine.remove();
sightLine = null;
sightLine.classList.add('sight-line'); lockTarget && lockTarget.classList.remove(targetClassName);
sightLine.classList.add(activeClassName); lockTarget = null;
sightLine.setAttributeNS(null, 'x1', x1); }
sightLine.setAttributeNS(null, 'y1', y1);
sightLine.setAttributeNS(null, 'x2', x2);
sightLine.setAttributeNS(null, 'y2', y2);
export function update({ x, y }) {
sightLine.setAttributeNS(null, 'x1', x);
sightLine.setAttributeNS(null, 'y1', y);
}
export function toggleLock(cell) {
lockTarget = lockTarget ? unlock(sightLine, lockTarget) : lock(sightLine, cell);
}
export function getSightLine() {
return sightLine; return sightLine;
} }
function lock(sightLine, cell) { export function getLockTarget() {
sightLine.classList.remove(activeClassName); return lockTarget;
cell.classList.add(targetClassName);
return cell;
} }
function unlock(sightLine, lockTarget) { export function setHexes(hexes) {
sightLine.classList.add(activeClassName); activeHexes.forEach(h => h.classList.remove(activeClassName));
lockTarget.classList.remove(targetClassName); hexes.forEach(h => h.classList.add(activeClassName));
activeHexes = hexes;
return null;
}
export default function (board) {
let sightLine,
lockTarget,
activeHexes = [];
return {
calcIndexes,
drawLine: function (...positions) {
this.clear();
sightLine = create(...positions);
board.appendChild(sightLine);
},
clear: function () {
sightLine && sightLine.remove();
sightLine = null;
lockTarget && lockTarget.classList.remove(targetClassName);
lockTarget = null;
},
update: function ({ x, y }) {
sightLine.setAttributeNS(null, 'x1', x);
sightLine.setAttributeNS(null, 'y1', y);
},
toggleLock: function (cell) {
lockTarget = lockTarget ? unlock(sightLine, lockTarget) : lock(sightLine, cell);
},
get sightLine() {
return sightLine;
},
get lockTarget() {
return lockTarget;
},
set hexes(hexes) {
activeHexes.forEach(h => h.classList.remove(activeClassName));
hexes.forEach(h => h.classList.add(activeClassName));
activeHexes = hexes;
}
};
} }