Remove calling the gme module with 'new'

This commit is contained in:
Catalin Constantin Mititiuc 2025-06-16 22:41:29 -07:00
parent ff2aaab4da
commit 48f239470f
2 changed files with 295 additions and 285 deletions

View File

@ -41,7 +41,7 @@ document.querySelector('object').addEventListener('load', function () {
window.addEventListener('load', () => { window.addEventListener('load', () => {
const svg = document.querySelector('object').contentDocument.querySelector('svg'), const svg = document.querySelector('object').contentDocument.querySelector('svg'),
game = new Game(svg); game = Game(svg);
window.game = game; window.game = game;

View File

@ -4,350 +4,360 @@ import Counter from './game/counter.js';
const svgns = "http://www.w3.org/2000/svg"; const svgns = "http://www.w3.org/2000/svg";
export default class Game { function getCellContents(cell) {
info; return cell.querySelectorAll('*:not(use[href="#hex"])');
placing = []; }
constructor(svg) { function getGridIndex({ parentElement: { dataset: { x }, parentElement: { dataset: { y }}}}) {
this.svg = svg; return { x: +x, y: +y };
}
const board = this.getBoard(); function getCounterAtGridIndex(x, y) {
return getCell(x, y).querySelector('.counter');
}
this.firingArc = FiringArc(svg, board); function getHex(cell) {
this.sightLine = SightLine(board); return cell.querySelector('use[href="#hex"]');
this.counter = Counter(svg, board); }
this.setUpCells(); function getCellOccupant(cell) {
return cell.querySelector('.counter');
}
// debug function getCells(svg) {
const counter = this.counter.getCounter({ dataset: { allegiance: 'davion', number: '1' }}); return svg.querySelectorAll('g[data-y] > g[data-x]');
this.counter.place(counter, this.getCell(17, 25)); }
this.select(counter);
}
getCells() { function getLockedSightLine(svg) {
return this.svg.querySelectorAll('g[data-y] > g[data-x]'); return svg.querySelector('line.sight-line:not(.active)');
} }
getCell(x, y) { function getSightLine(svg) {
return this.svg.querySelector(`g[data-y="${y}"] > g[data-x="${x}"]`); return svg.querySelector('line.sight-line');
} }
getCellOccupant(cell) { function getActiveSightLine(svg) {
return cell.querySelector('.counter'); return svg.querySelector('line.sight-line.active');
} }
getCellContents(cell) { function isGrenade(el) {
return cell.querySelectorAll('*:not(use[href="#hex"])'); return el && el.getAttribute('href') === '#counter-grenade';
} }
getHex(cell) { function isClone(counter) {
return cell.querySelector('use[href="#hex"]'); const isClone = counter.classList.contains('clone'),
} { allegiance: clAl, number: clNum } = counter.dataset;
getSelected() { return {
return this.svg.querySelector(`.counter.selected[data-allegiance][data-number]`); of: function ({ dataset: { allegiance, number }}) {
} return isClone && clAl == allegiance && clNum == number;
}
};
}
getSightLine() { function getCellPosition(cell) {
return this.svg.querySelector('line.sight-line'); let pt = new DOMPoint(0, 0),
} transform = getComputedStyle(cell).transform.match(/-?\d+\.?\d*/g),
getActiveSightLine() {
return this.svg.querySelector('line.sight-line.active');
}
getLockedSightLine() {
return this.svg.querySelector('line.sight-line:not(.active)');
}
getGridIndex({ parentElement: { dataset: { x }, parentElement: { dataset: { y }}}}) {
return { x: +x, y: +y };
}
getCounterAtGridIndex(x, y) {
return this.getCell(x, y).querySelector('.counter');
}
getBoard() {
return this.svg.querySelector('.board');
}
getCellPosition(cell) {
let pt = new DOMPoint(0, 0),
transform = getComputedStyle(cell).transform.match(/-?\d+\.?\d*/g),
mtx = new DOMMatrix(transform);
pt = pt.matrixTransform(mtx);
transform = getComputedStyle(cell.parentElement).transform.match(/-?\d+\.?\d*/g);
mtx = new DOMMatrix(transform); mtx = new DOMMatrix(transform);
pt = pt.matrixTransform(mtx); pt = pt.matrixTransform(mtx);
return pt; transform = getComputedStyle(cell.parentElement).transform.match(/-?\d+\.?\d*/g);
} mtx = new DOMMatrix(transform);
pt = pt.matrixTransform(mtx);
endMove() { return pt;
const selected = this.getSelected(); }
if (selected) { export default function (svg) {
this.counter.endMove(selected); let distanceCallback, proneFlagCallback, selectCallback,
this.unSelect(); placing = [];
}
}
select(selected) { const board = svg.querySelector('.board'),
const counter = this.counter.getCounter(selected); firingArc = FiringArc(svg, board),
sightLine = SightLine(board),
counterMod = Counter(svg, board);
if (counter) { getCells(svg).forEach(cell => {
this.unSelect(); cell.addEventListener('click', e => {
this.placing.push(counter); const state = {
counter.classList.add(this.counter.selectedClass); placing: placing,
this.firingArc.get(counter).forEach(el => el.removeAttribute('clip-path')); hex: getHex(cell),
this.selectCallback && this.selectCallback({ prone: this.counter.hasProne(counter), ...counter.dataset }); occupant: getCellOccupant(cell),
} contents: getCellContents(cell)
}
unSelect() {
const selected = this.getSelected();
if (selected) {
this.placing = [];
this.getSelected().classList.remove(this.counter.selectedClass);
this.clearSightLine();
this.firingArc.clipAll();
}
}
endTurn(allegiance) {
this.firingArc.clear(allegiance);
}
toggleProne() {
const selected = this.getSelected(),
isOnBoard = selected && selected.parentElement.hasAttribute('data-x');
if (selected && isOnBoard) {
this.counter.toggleProne(selected);
}
}
toggleFiringArcVisibility(allegiance) {
this.firingArc.toggleVisibility(allegiance);
}
setUpCells() {
function isGrenade(el) {
return el && el.getAttribute('href') === '#counter-grenade';
}
function isClone(counter) {
const isClone = counter.classList.contains('clone'),
{ allegiance: clAl, number: clNum } = counter.dataset;
return {
of: function ({ dataset: { allegiance, number }}) {
return isClone && clAl == allegiance && clNum == number;
}
}; };
}
this.getCells().forEach(cell => { let toPlace = placing.pop();
cell.addEventListener('click', e => {
const state = {
placing: this.placing,
hex: this.getHex(cell),
occupant: this.getCellOccupant(cell),
contents: this.getCellContents(cell)
};
let toPlace = this.placing.pop(); if (isGrenade(toPlace)) {
state.hex.after(toPlace);
} else if (toPlace && !state.occupant) {
counterMod.place(toPlace, cell);
placing.push(toPlace);
const lockedSl = getLockedSightLine(svg);
if (isGrenade(toPlace)) { if (!lockedSl) {
state.hex.after(toPlace); clearSightLine();
} else if (toPlace && !state.occupant) { } else {
this.counter.place(toPlace, cell); updateSightLine(cell);
this.placing.push(toPlace); }
const lockedSl = this.getLockedSightLine(); } else if (toPlace && state.occupant) {
if (toPlace === state.occupant) {
if ('previous' in toPlace.dataset) {
const trace = counterMod.getTrace(toPlace);
toPlace.remove();
toPlace = getCounterAtGridIndex(...toPlace.dataset.previous.split(','));
toPlace.classList.remove('clone');
toPlace.classList.add(counterMod.selectedClass);
if (!('previous' in toPlace.dataset)) {
trace.remove();
} else {
const points = trace.getAttribute('points').split(' ');
points.pop();
trace.setAttributeNS(null, 'points', points.join(' '));
}
placing.push(toPlace);
const lockedSl = getLockedSightLine(svg);
if (!lockedSl) { if (!lockedSl) {
this.clearSightLine(); clearSightLine();
} else {
updateSightLine(toPlace.parentElement);
}
} else { } else {
this.updateSightLine(cell); unSelect();
} }
} else if (toPlace && state.occupant) { } else if (!state.occupant.classList.contains('clone')) {
if (toPlace === state.occupant) { select(state.occupant);
if ('previous' in toPlace.dataset) { } else {
const trace = this.counter.getTrace(toPlace); if (isClone(state.occupant).of(toPlace)) {
if (!('previous' in state.occupant.dataset)) {
state.occupant.classList.remove('clone');
state.occupant.classList.add(counterMod.selectedClass);
toPlace.remove(); toPlace.remove();
toPlace = this.getCounterAtGridIndex(...toPlace.dataset.previous.split(',')); toPlace = state.occupant;
toPlace.classList.remove('clone'); counterMod.removeClones(toPlace);
toPlace.classList.add(this.counter.selectedClass); counterMod.getTrace(toPlace).remove();
if (!('previous' in toPlace.dataset)) { const lockedSl = getLockedSightLine(svg);
trace.remove();
} else {
const points = trace.getAttribute('points').split(' ');
points.pop();
trace.setAttributeNS(null, 'points', points.join(' '));
}
this.placing.push(toPlace);
const lockedSl = this.getLockedSightLine();
if (!lockedSl) { if (!lockedSl) {
this.clearSightLine(); clearSightLine();
} else { } else {
this.updateSightLine(toPlace.parentElement); updateSightLine(cell);
} }
} else { } else {
this.unSelect(); const index = getGridIndex(state.occupant),
} trace = counterMod.getTrace(toPlace),
} else if (!state.occupant.classList.contains('clone')) { pos = getCellPosition(cell),
this.select(state.occupant); points = trace.getAttribute('points').split(' ').filter(p => p != `${pos.x},${pos.y}`).join(' ');;
} else {
if (isClone(state.occupant).of(toPlace)) {
if (!('previous' in state.occupant.dataset)) {
state.occupant.classList.remove('clone');
state.occupant.classList.add(this.counter.selectedClass);
toPlace.remove();
toPlace = state.occupant;
this.counter.removeClones(toPlace);
this.counter.getTrace(toPlace).remove();
const lockedSl = this.getLockedSightLine();
if (!lockedSl) { let current = toPlace;
this.clearSightLine();
} else {
this.updateSightLine(cell);
}
} else {
const index = this.getGridIndex(state.occupant),
trace = this.counter.getTrace(toPlace),
pos = this.getCellPosition(cell),
points = trace.getAttribute('points').split(' ').filter(p => p != `${pos.x},${pos.y}`).join(' ');;
let current = toPlace; trace.setAttributeNS(null, 'points', points);
trace.setAttributeNS(null, 'points', points); while (current.dataset.previous != `${index.x},${index.y}`) {
current = getCounterAtGridIndex(...current.dataset.previous.split(','));
while (current.dataset.previous != `${index.x},${index.y}`) {
current = this.getCounterAtGridIndex(...current.dataset.previous.split(','));
}
current.dataset.previous = state.occupant.dataset.previous;
state.occupant.remove();
} }
current.dataset.previous = state.occupant.dataset.previous;
state.occupant.remove();
} }
this.placing.push(toPlace);
} }
} else if (!toPlace && state.occupant) { placing.push(toPlace);
this.select(state.occupant);
} else {
console.log('removing cell contents');
state.contents.forEach(el => el.remove());
} }
}); } else if (!toPlace && state.occupant) {
select(state.occupant);
cell.addEventListener('dblclick', e => { } else {
const toPlace = this.placing.pop(), console.log('removing cell contents');
occupant = this.getCellOccupant(cell); state.contents.forEach(el => el.remove());
}
if (toPlace && occupant && toPlace == occupant) {
const { number, allegiance } = toPlace.dataset,
selector = `[data-allegiance="${allegiance}"][data-number="${number}"]`;
this.svg.querySelectorAll(selector).forEach(el => el.remove());
}
});
cell.addEventListener('contextmenu', e => {
e.preventDefault();
this.sightLine.toggleLock(cell);
cell.dispatchEvent(new MouseEvent('pointerover'));
});
cell.addEventListener('pointerover', e => {
let selected = this.getSelected();
if (selected) {
let sl = this.getSightLine(),
isOnBoard = selected.parentElement.hasAttribute('data-x'),
sourceCell = selected.parentElement;
if (isOnBoard && (!sl || sl.classList.contains('active')) && sourceCell != cell) {
this.drawSightLine(sourceCell, cell);
}
}
let occupant = this.getCellOccupant(cell);
if (occupant) {
this.firingArc.toggleCounterVisibility(occupant, true);
}
});
cell.addEventListener('pointerout', e => {
let sl = this.getActiveSightLine();
if (sl) {
this.clearSightLine();
}
let occupant = this.getCellOccupant(cell);
if (occupant) {
this.firingArc.toggleCounterVisibility(occupant, false);
}
});
}); });
cell.addEventListener('dblclick', e => {
const toPlace = placing.pop(),
occupant = getCellOccupant(cell);
if (toPlace && occupant && toPlace == occupant) {
const { number, allegiance } = toPlace.dataset,
selector = `[data-allegiance="${allegiance}"][data-number="${number}"]`;
svg.querySelectorAll(selector).forEach(el => el.remove());
}
});
cell.addEventListener('contextmenu', e => {
e.preventDefault();
sightLine.toggleLock(cell);
cell.dispatchEvent(new MouseEvent('pointerover'));
});
cell.addEventListener('pointerover', e => {
let selected = getSelected();
if (selected) {
let sl = getSightLine(svg),
isOnBoard = selected.parentElement.hasAttribute('data-x'),
sourceCell = selected.parentElement;
if (isOnBoard && (!sl || sl.classList.contains('active')) && sourceCell != cell) {
drawSightLine(sourceCell, cell);
}
}
let occupant = getCellOccupant(cell);
if (occupant) {
firingArc.toggleCounterVisibility(occupant, true);
}
});
cell.addEventListener('pointerout', e => {
let sl = getActiveSightLine(svg);
if (sl) {
clearSightLine();
}
let occupant = getCellOccupant(cell);
if (occupant) {
firingArc.toggleCounterVisibility(occupant, false);
}
});
});
// debug
const c = counterMod.getCounter({ dataset: { allegiance: 'davion', number: '1' }});
counterMod.place(c, getCell(17, 25));
select(c);
function getCell(x, y) {
return svg.querySelector(`g[data-y="${y}"] > g[data-x="${x}"]`);
} }
setFiringArc(size) { function getSelected() {
const counter = this.getSelected(), return svg.querySelector(`.counter.selected[data-allegiance][data-number]`);
isOnBoard = counter => counter && counter.parentElement.hasAttribute('data-x');
if (isOnBoard(counter)) {
this.firingArc.set(size, counter, this.getCellPosition(counter.parentElement));
}
} }
setGrenade() { function clearSightLine() {
let counter = document.createElementNS(svgns, 'use'); sightLine.hexes = [];
counter.setAttributeNS(null, 'href', '#counter-grenade'); sightLine.clear();
distanceCallback && distanceCallback();
this.placing.push(counter);
} }
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 }}} = this.sightLine.lockTarget; { dataset: { x: tX }, parentElement: { dataset: { y: tY }}} = sightLine.lockTarget;
const selector = this.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 = this.svg.querySelectorAll(selector); const hexes = svg.querySelectorAll(selector);
this.sightLine.hexes = hexes; sightLine.hexes = hexes;
this.sightLine.update(this.getCellPosition(cell)); sightLine.update(getCellPosition(cell));
this.distanceCallback && this.distanceCallback(hexes.length - 1); distanceCallback && distanceCallback(hexes.length - 1);
} }
drawSightLine(sourceCell, targetCell) { function drawSightLine(sourceCell, targetCell) {
const { dataset: { x: sX }, parentElement: { dataset: { y: sY }}} = sourceCell, const { dataset: { x: sX }, parentElement: { dataset: { y: sY }}} = sourceCell,
{ dataset: { x: tX }, parentElement: { dataset: { y: tY }}} = targetCell; { dataset: { x: tX }, parentElement: { dataset: { y: tY }}} = targetCell;
const selector = this.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 = this.svg.querySelectorAll(selector); const hexes = svg.querySelectorAll(selector);
this.sightLine.hexes = hexes; sightLine.hexes = hexes;
this.sightLine.drawLine(this.getCellPosition(sourceCell), this.getCellPosition(targetCell)); sightLine.drawLine(getCellPosition(sourceCell), getCellPosition(targetCell));
this.distanceCallback && this.distanceCallback(hexes.length - 1); distanceCallback && distanceCallback(hexes.length - 1);
} }
clearSightLine() { function select(selected) {
this.sightLine.hexes = []; const counter = counterMod.getCounter(selected);
this.sightLine.clear();
this.distanceCallback && this.distanceCallback(); if (counter) {
unSelect();
placing.push(counter);
counter.classList.add(counterMod.selectedClass);
firingArc.get(counter).forEach(el => el.removeAttribute('clip-path'));
selectCallback && selectCallback({ prone: counterMod.hasProne(counter), ...counter.dataset });
}
} }
function unSelect() {
const selected = getSelected();
if (selected) {
placing = [];
getSelected().classList.remove(counterMod.selectedClass);
clearSightLine();
firingArc.clipAll();
}
}
function endMove() {
const selected = getSelected();
if (selected) {
counterMod.endMove(selected);
unSelect();
}
}
function endTurn(allegiance) {
firingArc.clear(allegiance);
}
function toggleProne() {
const selected = getSelected(),
isOnBoard = selected && selected.parentElement.hasAttribute('data-x');
if (selected && isOnBoard) {
counterMod.toggleProne(selected);
}
}
function toggleFiringArcVisibility(allegiance) {
firingArc.toggleVisibility(allegiance);
}
function setFiringArc(size) {
const counter = getSelected(),
isOnBoard = counter => counter && counter.parentElement.hasAttribute('data-x');
if (isOnBoard(counter)) {
firingArc.set(size, counter, getCellPosition(counter.parentElement));
}
}
function setGrenade() {
let counter = document.createElementNS(svgns, 'use');
counter.setAttributeNS(null, 'href', '#counter-grenade');
placing.push(counter);
}
return {
set distanceCallback(callback) {
distanceCallback = callback;
},
set proneFlagCallback(callback) {
proneFlagCallback = callback;
},
set selectCallback(callback) {
selectCallback = callback;
},
select,
unSelect,
endMove,
endTurn,
setFiringArc,
setGrenade,
toggleFiringArcVisibility,
toggleProne
};
} }