Web Dev Solutions

Catalin Mititiuc

aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
Diffstat (limited to 'src/modules')
-rw-r--r--src/modules/gameboard.js260
-rw-r--r--src/modules/scenario.js3
2 files changed, 189 insertions, 74 deletions
diff --git a/src/modules/gameboard.js b/src/modules/gameboard.js
index 02b9c0f..3f02784 100644
--- a/src/modules/gameboard.js
+++ b/src/modules/gameboard.js
@@ -10,6 +10,8 @@ const top = {
collection: new Map()
};
+const frontmostStore = new Map();
+
function getCellContents(cell) {
return cell.querySelectorAll('*:not(use[href="#hex"])');
}
@@ -215,46 +217,142 @@ function workaroundForWebKitBug233432(listener) {
export function start(el) {
svg = el;
- const gridTop = svg.querySelector('.grid-top');
- top.container = svg.querySelector('.grid-top > .container');
- const topHex = svg.querySelector('.grid-top > use[href="#hex"]');
+ //const gridTop = svg.querySelector('.grid-top');
+ //top.container = svg.querySelector('.grid-top > .container');
+ //const topHex = svg.querySelector('.grid-top > use[href="#hex"]');
+
+ const grid = svg.querySelector('.grid');
+ const frontmost = grid.querySelector('.frontmost');
+
+ svg.addEventListener('pointerover', e => {
+ //console.log('pointerover', e.target.closest('[data-q][data-r][data-s][data-t], .frontmost'), e);
+ const targetCell = e.target.closest('[data-q][data-r][data-s][data-t]');
+ console.log('SVG pointerover', targetCell);
+ const counter = targetCell && targetCell.querySelector('.counter');
+ //console.log('pointerover', 'targetCell', targetCell);
+
+ if (counter) {
+ firingArc.toggleCounterVisibility(svg, counter, true);
+ frontmost.setAttributeNS(null, 'transform', targetCell.getAttributeNS(null, 'transform'));
+ frontmostStore.set(counter, targetCell);
+ frontmost.append(counter);
+ }
- const clearHexDialog = document.querySelector('#clear-hex');
- clearHexDialog.addEventListener('close', e => {
- if (clearHexDialog.returnValue === 'confirm') {
- [...top.container.children].forEach(child => {
- top.collection.delete(child);
- child.remove();
+ targetCell && targetCell.classList.add('hover');
+
+ //if (targetCell && !targetCell.classList.contains('frontmost')) {
+ // targetCell.classList.add('hover');
+ // const occupant = targetCell.querySelector('.counter');
+ //
+ // if (occupant) {
+ // firingArc.toggleCounterVisibility(svg, occupant, true);
+ // }
+ // const children = [...targetCell.children].filter(c => c.getAttributeNS(null, 'href') !== '#hex');
+ // if (children.length > 0) {
+ // frontmost.setAttributeNS(null, 'transform', targetCell.getAttributeNS(null, 'transform'));
+ // children.forEach(child => {
+ // frontmostStore.set(child, targetCell);
+ // frontmost.append(child);
+ // });
+ // }
+ //}
+ //console.log('frontmost contents', frontmost.children);
+ });
+
+ svg.addEventListener('pointerout', e => {
+ //const targetCell = e.target.closest('[data-q][data-r][data-s][data-t], .frontmost');
+ console.log('pointer out target', e.target);
+ const targetCell = e.target.closest('[data-q][data-r][data-s][data-t], .frontmost');
+
+ if (targetCell) {
+ console.log('SVG pointerout', targetCell);
+ [...frontmost.children].forEach(child => {
+ //console.log('child', child, 'relatedTarget', e.relatedTarget);
+ //if ([
+ // !e.relatedTarget, // out of the window
+ // targetCell.classList.contains('frontmost') && !e.relatedTarget.closest('.frontmost'), // from one element in frontmost to another element in frontmost
+ // !targetCell.classList.contains('frontmost') && frontmostStore.get(child) === targetCell, // leaving from a hex under frontmost
+ //].some(e => e)) {
+ console.log('child', child, 'belongs to', frontmostStore.get(child));
+ console.log('relatedTarget', e.relatedTarget);
+ if (!e.relatedTarget || frontmostStore.get(child) !== targetCell || (e.relatedTarget !== child && !child.contains(e.relatedTarget))) {
+ //if (!e.relatedTarget || frontmostStore.get(child) !== targetCell || e.relatedTarget !== child) {
+ const parent = frontmostStore.get(child);
+ console.log('returning to', parent);
+ //console.log('RETURNING to', parent);
+ parent.append(child);
+
+ //if (child.classList.contains('.counter')) {
+ // firingArc.toggleCounterVisibility(svg, child, false);
+ //}
+
+ firingArc.toggleCounterVisibility(svg, child, false);
+ parent.classList.remove('hover');
+ frontmostStore.delete(child);
+ }
+ });
+ //targetCell.classList.remove('hover');
+ if (frontmost.children.length < 1) targetCell.classList.remove('hover');
+ } else {
+ [...frontmost.children].forEach(child => {
+ const parent = frontmostStore.get(child);
+ parent.append(child);
+ parent.classList.remove('hover');
+ frontmostStore.delete(child);
});
}
+ //console.log('frontmost contents', frontmost.children);
});
- clearHexDialog.querySelector('button[value="confirm"]').addEventListener('click', function(e) {
- e.preventDefault();
- clearHexDialog.close(this.value);
+ grid.addEventListener('click', e => {
+ console.log('click', e.target);
});
- gridTop.addEventListener('pointerleave', workaroundForWebKitBug233432(e => {
- [...top.container.children].forEach(child => {
- top.collection.get(child).parent.append(child);
- top.collection.delete(child);
- });
-
- top.cell = null;
- }));
-
- topHex.addEventListener('click', clickHandler);
-
- topHex.addEventListener('contextmenu', e => {
- e.preventDefault();
- getSelected() ? sightLine.toggleLock(top.cell) : clearHexDialog.showModal();
- });
+ const clearHexDialog = document.querySelector('#clear-hex');
+ //clearHexDialog.addEventListener('close', e => {
+ // if (clearHexDialog.returnValue === 'confirm') {
+ // [...top.container.children].forEach(child => {
+ // top.collection.delete(child);
+ // child.remove();
+ // });
+ // }
+ //});
+
+ //clearHexDialog.querySelector('button[value="confirm"]').addEventListener('click', function(e) {
+ // e.preventDefault();
+ // clearHexDialog.close(this.value);
+ //});
+
+ //gridTop.addEventListener('pointerleave', workaroundForWebKitBug233432(e => {
+ // console.log('pointerleave top', performance.now(), top.cell);
+ // const occupant = svg.querySelector('.grid-top .container .counter');
+ //
+ // if (occupant) {
+ // firingArc.toggleCounterVisibility(svg, occupant, false);
+ // }
+ //
+ // [...top.container.children].forEach(child => {
+ // top.collection.get(child).parent.append(child);
+ // top.collection.delete(child);
+ // });
+ //
+ // top.cell = null;
+ //
+ // getActiveSightLine(svg) && clearSightLine();
+ //}));
+
+ //topHex.addEventListener('click', clickHandler);
+ //
+ //topHex.addEventListener('contextmenu', e => {
+ // e.preventDefault();
+ // getSelected() ? sightLine.toggleLock(top.cell) : clearHexDialog.showModal();
+ //});
const startingLocations = svg.querySelector('.start-locations');
startingLocations && getUnits(startingLocations).forEach(unit => unit.addEventListener('click', selectOffBoard));
function clickHandler(e) {
- const occupant = svg.querySelector('.grid-top .container .counter')
+ const occupant = svg.querySelector('.grid-top .container .counter');
let toPlace = placing.pop();
if (isCounter(toPlace) || isMechTemplate(toPlace)) {
@@ -280,43 +378,47 @@ export function start(el) {
const selected = getSelected();
}
- getCells(svg).forEach(cell => {
- cell.addEventListener('pointerover', () => {
- //console.log(['pointerenter', cell]);
-
- top.cell = cell;
-
- [...top.container.children].forEach(child => {
- top.collection.get(child).parent.append(child);
- top.collection.delete(child);
- });
-
- top.container.parentElement.setAttributeNS(null, 'transform', cell.getAttributeNS(null, 'transform'));
-
- [...cell.children].filter(c => c.getAttributeNS(null, 'href') !== '#hex').forEach(child => {
- top.collection.set(child, { parent: cell });
- top.container.append(child);
- });
-
- let occupant = svg.querySelector('.grid-top .container .counter');
- const selected = getSelected();
-
- if (placing[0]?.getAttributeNS(null, 'class') == 'mech-template') {
- cell.appendChild(placing[0]);
- }
-
- if (selected && svg.querySelector('.grid').contains(selected) && !getLockedSightLine(svg) && cell !== selected.parentElement) {
- clearSightLine();
- drawSightLine(selected.parentElement, cell);
- }
-
- occupant = getCellOccupant(cell);
-
- if (occupant) {
- firingArc.toggleCounterVisibility(svg, occupant, true);
- }
- });
- });
+ //getCells(svg).forEach(cell => {
+ // cell.addEventListener('pointerleave', () => {
+ // console.log('pointerleave cell', performance.now(), cell);
+ // });
+ //
+ // cell.addEventListener('pointerover', () => {
+ // console.log('pointerenter', performance.now(), cell);
+ //
+ // top.cell = cell;
+ //
+ // [...top.container.children].forEach(child => {
+ // top.collection.get(child).parent.append(child);
+ // top.collection.delete(child);
+ // });
+ //
+ // top.container.parentElement.setAttributeNS(null, 'transform', cell.getAttributeNS(null, 'transform'));
+ //
+ // [...cell.children].filter(c => c.getAttributeNS(null, 'href') !== '#hex').forEach(child => {
+ // top.collection.set(child, { parent: cell });
+ // top.container.append(child);
+ // });
+ //
+ // let occupant = svg.querySelector('.grid-top .container .counter');
+ // const selected = getSelected();
+ //
+ // if (placing[0]?.getAttributeNS(null, 'class') == 'mech-template') {
+ // cell.appendChild(placing[0]);
+ // }
+ //
+ // if (selected && svg.querySelector('.grid').contains(selected) && !getLockedSightLine(svg) && cell !== selected.parentElement) {
+ // clearSightLine();
+ // drawSightLine(selected.parentElement, cell);
+ // }
+ //
+ // occupant = getCellOccupant(cell);
+ //
+ // if (occupant) {
+ // firingArc.toggleCounterVisibility(svg, occupant, true);
+ // }
+ // });
+ //});
//const cell = document.querySelector('[data-q="0"][data-r="0"][data-s="0"][data-t="0"]');
//const povr = new PointerEvent('pointerover');
@@ -326,17 +428,29 @@ export function start(el) {
// debug //
// Add a trooper counter
- //const attacker = { dataset: { allegiance: 'attacker', number: 1, squad: 1 }};
// const defender = { dataset: { allegiance: 'defender', number: 1, squad: 2 }};
- //const cell = getCell(0, 0, 0, 0);
- //const trooper = soldier.createCounter(attacker, 'blazer');
- //const trooperSack = document.createElementNS(svgns, 'g');
- //trooperSack.classList.add('cell-contents');
- //trooperSack.append(trooper);
- //soldier.place(svg, trooper, cell);
+
+ const cell = getCell(0, 0, 0, 0);
+ const attacker = { dataset: { allegiance: 'attacker', number: 1, squad: 1 }};
+ const trooper = soldier.createCounter(attacker, 'blazer');
+ soldier.place(svg, trooper, cell);
// Add some counters in an unoccupied cell
//const countersCell = getCell(-1, 1, 0, 0);
+ const counter = document.createElementNS(svgns, 'use');
+ const name = 'grenade';
+ //counter.addEventListener('click', e => {
+ // e.stopPropagation()
+ // const container = counter.parentElement;
+ // counter.remove()
+ // arrangeCounters(container);
+ //});
+
+ //counter.setAttributeNS(null, 'href', `#counter-${name}`);
+ //counter.classList.add(`counter-${name}`);
+ //cell.append(counter);
+ //arrangeCounters(cell)
+
//setCounter('grenade');
//setCounter('prone');
//setCounter('1st-floor');
@@ -374,7 +488,7 @@ export function setFiringArc() {
isOnBoard = counter => counter && counter.parentElement.hasAttribute('data-q');
if (isOnBoard(counter)) {
- returnToParent(top);
+ //returnToParent(top);
firingArc.set(svg, this.dataset.size, counter, getCellPosition(counter.parentElement));
}
}
diff --git a/src/modules/scenario.js b/src/modules/scenario.js
index 805d0fe..4f72168 100644
--- a/src/modules/scenario.js
+++ b/src/modules/scenario.js
@@ -97,7 +97,8 @@ export async function build(svg, request) {
const scenarioBuildings = scenario.querySelector('.gameboard .buildings');
if (scenarioGrid) {
- grid.replaceWith(svg.ownerDocument.importNode(scenarioGrid, true));
+ const frontmost = grid.querySelector('.frontmost');
+ [...scenarioGrid.children].forEach(child => frontmost.before(svg.ownerDocument.importNode(child, true)));
}
if (scenarioBuildings)