Web Dev Solutions

Catalin Mititiuc

From ce6cd0be50975f565e52454cfb37e142eac51fc9 Mon Sep 17 00:00:00 2001 From: Catalin Mititiuc Date: Mon, 1 Jul 2024 16:30:15 -0700 Subject: WIP: split hexes and building structure into different layers --- public/assets/css/map.css | 2 +- public/assets/css/radial.css | 21 ++++++++++-- public/assets/images/radial.svg | 3 ++ public/assets/images/scenario_template.svg | 6 ++-- src/index.js | 2 +- src/modules/gameboard.js | 53 ++++++++++++++++++------------ src/radial.js | 40 +++++++++------------- 7 files changed, 74 insertions(+), 53 deletions(-) diff --git a/public/assets/css/map.css b/public/assets/css/map.css index 535e5b4..4c3c9a2 100644 --- a/public/assets/css/map.css +++ b/public/assets/css/map.css @@ -15,7 +15,7 @@ text { } use[href="#hex"] { - opacity: 0.25; + opacity: 0; fill: transparent; fill-opacity: 0.5; stroke-width: 0.5px; diff --git a/public/assets/css/radial.css b/public/assets/css/radial.css index 2f03cb2..9b84d8a 100644 --- a/public/assets/css/radial.css +++ b/public/assets/css/radial.css @@ -47,7 +47,7 @@ use[href="#hex"] { display: inline; opacity: 1; fill: #ffffff; - fill-opacity: 0.5; + /* fill-opacity: 0.5; */ /* fill: url(#asterisk); */ stroke: none; } @@ -72,6 +72,14 @@ use[href="#hex"] { display: none; } +[data-view-elevation="-1"] [class*="elevation"] { + display: none; +} + +[data-view-elevation="-1"] #background { + fill: #333; +} + [data-view-elevation="-1"] .building .elevation-basement { display: inline; } @@ -84,14 +92,21 @@ use[href="#hex"] { display: inline; } -[data-view-elevation="2"] .building .elevation-2 { +[data-view-elevation="2"] .building .elevation-2, +[data-view-elevation="2"] .building .elevation-1:last-child { display: inline; } -[data-view-elevation="3"] .building .elevation-3 { +[data-view-elevation="3"] .building .elevation-3, +[data-view-elevation="3"] .building .elevation-1:last-child, +[data-view-elevation="3"] .building .elevation-2:last-child { display: inline; } +#dots { + pointer-events: none; +} + /* [data-view-elevation="-1"] .building > *, [data-view-elevation="-1"] .building > .structure > * { diff --git a/public/assets/images/radial.svg b/public/assets/images/radial.svg index f2abd15..775f0f2 100644 --- a/public/assets/images/radial.svg +++ b/public/assets/images/radial.svg @@ -23,6 +23,9 @@ + + + diff --git a/public/assets/images/scenario_template.svg b/public/assets/images/scenario_template.svg index 20798a9..1b14c39 100644 --- a/public/assets/images/scenario_template.svg +++ b/public/assets/images/scenario_template.svg @@ -47,9 +47,9 @@ - + - + @@ -57,6 +57,6 @@ - + diff --git a/src/index.js b/src/index.js index bc4fd8f..bb299be 100644 --- a/src/index.js +++ b/src/index.js @@ -210,7 +210,7 @@ document.querySelector('#roll-dice').addEventListener('click', () => { document.querySelectorAll('[name="select-elevation"]').forEach(el => { el.addEventListener('change', function (e) { - document.querySelector('object').contentDocument.querySelector('.grid').dataset.viewElevation = this.value; + document.querySelector('object').contentDocument.querySelector('.gameboard').dataset.viewElevation = this.value; }); }); diff --git a/src/modules/gameboard.js b/src/modules/gameboard.js index 1cdec7b..45b2cb5 100644 --- a/src/modules/gameboard.js +++ b/src/modules/gameboard.js @@ -23,7 +23,6 @@ function getCellOccupant(cell) { } function getCells(svg) { - // return svg.querySelectorAll('g.grid > g[data-y] > g[data-x]'); return svg.querySelectorAll('[data-q][data-r][data-s][data-t]'); } @@ -86,35 +85,49 @@ function clearSightLine() { Observable.notify('distance'); } -function updateSightLine(cell) { - const CURRENT_ELEVATION_LEVEL = 0; - const { q: sq, r: sr, s: ss } = cell.dataset; - const { q: tq, r: tr, s: ts } = sightLine.getLockTarget().dataset; +function getSightLineHexes(source, target) { + const { q: sq, r: sr, s: ss } = source.dataset; + const { q: tq, r: tr, s: ts } = target.dataset; const sourceIndex = { q: +sq, r: +sr, s: +ss }; const targetIndex = { q: +tq, r: +tr, s: +ts }; - const selector = sightLine.calcIndexes(sourceIndex, targetIndex) - .map(({ q, r, s }) => `g[data-q="${q}"][data-r="${r}"][data-s="${s}"][data-t="${CURRENT_ELEVATION_LEVEL}"] use[href="#hex"]`) + const selector = sightLine + .calcIndexes(sourceIndex, targetIndex) + .map(({ q, r, s }) => `g[data-q="${q}"][data-r="${r}"][data-s="${s}"] use[href="#hex"]`) .join(', '); - const hexes = svg.querySelectorAll(selector); + return svg.querySelectorAll(selector); +} + +function updateSightLine(cell) { + // const CURRENT_ELEVATION_LEVEL = 0; + // const { q: sq, r: sr, s: ss } = cell.dataset; + // const { q: tq, r: tr, s: ts } = sightLine.getLockTarget().dataset; + // const sourceIndex = { q: +sq, r: +sr, s: +ss }; + // const targetIndex = { q: +tq, r: +tr, s: +ts }; + + // const selector = sightLine.calcIndexes(sourceIndex, targetIndex) + // .map(({ q, r, s }) => `g[data-q="${q}"][data-r="${r}"][data-s="${s}"][data-t="${CURRENT_ELEVATION_LEVEL}"] use[href="#hex"]`) + // .join(', '); + + const hexes = getSightLineHexes(cell, sightLine.getLockTarget()); sightLine.setHexes(hexes); sightLine.update(getCellPosition(cell)); Observable.notify('distance', hexes.length - 1); } function drawSightLine(sourceCell, targetCell) { - const CURRENT_ELEVATION_LEVEL = 0; - const { q: sq, r: sr, s: ss } = sourceCell.dataset; - const { q: tq, r: tr, s: ts } = targetCell.dataset; - const sourceIndex = { q: +sq, r: +sr, s: +ss }; - const targetIndex = { q: +tq, r: +tr, s: +ts }; + // const CURRENT_ELEVATION_LEVEL = 0; + // const { q: sq, r: sr, s: ss } = sourceCell.dataset; + // const { q: tq, r: tr, s: ts } = targetCell.dataset; + // const sourceIndex = { q: +sq, r: +sr, s: +ss }; + // const targetIndex = { q: +tq, r: +tr, s: +ts }; - const selector = sightLine.calcIndexes(sourceIndex, targetIndex) - .map(({ q, r, s }) => `g[data-q="${q}"][data-r="${r}"][data-s="${s}"][data-t="${CURRENT_ELEVATION_LEVEL}"] use[href="#hex"]`) - .join(', '); + // const selector = sightLine.calcIndexes(sourceIndex, targetIndex) + // .map(({ q, r, s }) => `g[data-q="${q}"][data-r="${r}"][data-s="${s}"][data-t="${CURRENT_ELEVATION_LEVEL}"] use[href="#hex"]`) + // .join(', '); - const hexes = svg.querySelectorAll(selector); + const hexes = getSightLineHexes(sourceCell, targetCell); sightLine.setHexes(hexes); const line = sightLine.create(getCellPosition(sourceCell), getCellPosition(targetCell)); svg.querySelector('.gameboard').appendChild(line); @@ -175,7 +188,6 @@ function selectOffBoard() { } function select(data) { - console.log('select', data); const counter = data && (soldier.getCounter(svg, data) || soldier.createCounter(data)); const isSelected = counter?.classList.contains(soldier.getSelectedClass()); @@ -223,7 +235,6 @@ export function start(el) { placing.push(toPlace); getLockedSightLine(svg) ? updateSightLine(toPlace.parentElement) : drawSightLine(toPlace.parentElement, cell); } else { - // deselect(); Observable.notify('select'); } } else if (!occupant.classList.contains('clone')) { @@ -242,7 +253,7 @@ export function start(el) { } else if (!toPlace && occupant) { Observable.notify('select', occupant); } else { - console.log('removing cell contents'); + console.log('removing cell contents', cell); getCellContents(cell).forEach(el => el.remove()); } @@ -331,7 +342,7 @@ export function toggleFiringArcVisibility() { export function setFiringArc() { const counter = getSelected(), - isOnBoard = counter => counter && counter.parentElement.hasAttribute('data-x'); + isOnBoard = counter => counter && counter.parentElement.hasAttribute('data-q'); if (isOnBoard(counter)) { firingArc.set(svg, this.dataset.size, counter, getCellPosition(counter.parentElement)); diff --git a/src/radial.js b/src/radial.js index 2c261bf..ef65e02 100644 --- a/src/radial.js +++ b/src/radial.js @@ -414,9 +414,12 @@ function drawBuildings(buildings, container, { q: pq, r: pr, s: ps }, furniture) const { x, y } = radialToScreenCoords({ q: origin.q + pq, r: origin.r + pr, s: origin.s + ps }); const transform = origin.transform || ((x, y) => `translate(${x}, ${y})`); const buildingStructure = document.createElementNS(xmlns, 'g'); - buildingStructure.classList.add('structure'); + buildingStructure.classList.add('building'); + buildingStructure.classList.add(building.type); buildingStructure.setAttributeNS(null, 'transform', transform(x, y)); - buildingContainer.appendChild(buildingStructure); + + const [mapsheet] = container.classList; + document.querySelector(`.buildings .${mapsheet}`).appendChild(buildingStructure); building.elevationLevels.forEach(elevationLevel => { const hexContainer = document.createElementNS(xmlns, 'g'); @@ -468,9 +471,13 @@ function drawBuildings(buildings, container, { q: pq, r: pr, s: ps }, furniture) function drawMapsheet(gameboard, mapsheet, position) { const container = document.createElementNS(xmlns, 'g'); - container.id = mapsheet.id; + container.classList.add(mapsheet.id); gameboard.appendChild(container); + const buildingContainer = document.createElementNS(xmlns, 'g'); + buildingContainer.classList.add(mapsheet.id); + document.querySelector('.buildings').appendChild(buildingContainer); + const gridContainer = document.createElementNS(xmlns, 'g'); gridContainer.classList.add('elevation-0'); @@ -498,7 +505,6 @@ function drawMapsheet(gameboard, mapsheet, position) { container.appendChild(gridContainer); return new Map([...grid, ...buildingHexes]); - // return { id, grid, buildings }; } const horzMapVect = function(coords) { @@ -558,27 +564,22 @@ let sheets = []; // sheets = [[mapsheet1]]; // sheets = [[mapsheet2]]; -// sheets = [[mapsheet2, mapsheet3]]; +sheets = [[mapsheet2], [mapsheet3]]; // sheets = [[mapsheet2], [mapsheet1], [mapsheet3]]; // drawMapsheet(grid, mapsheet2, vectorAdd({ q: 0, r: 0, s: 0 }, { q: 1, r: -2, s: 1 }, 6)); -sheets = [ - [mapsheet2, mapsheet1], - [mapsheet3, mapsheet4] -]; +// sheets = [ +// [mapsheet2, mapsheet1], +// [mapsheet3, mapsheet4] +// ]; // drawBuildings([{ // type: 'building2', // elevationLevels: range(-1, 2), // grid: new Map(buildingHexes.bld2), // position: coords => coords -// }], gameboard, { q: 0, r: 0, s: 0 }); - -// const map1building2furniture = document.querySelector('defs .mapsheet2 .building2 .furniture'); -// document.querySelector('.gameboard .building2 .structure').appendChild(map1building2furniture); - -// console.log(mapsheet2); +// }], grid, { q: 0, r: 0, s: 0 }); let finalGrid = new Map(); @@ -593,18 +594,10 @@ findScalar(findMult(sheets)).forEach(([vscalar, row]) => { } ms = drawMapsheet(grid, ms, horzMapVect(vertMapVect({ q: 0, r: 0, s: 0 }))); - // console.log(ms); finalGrid = new Map([...finalGrid, ...ms]); }) }); -const origin = document.querySelector('[transform="translate(0, 0)"]'); -// console.log(origin); - -// console.log([...finalGrid.entries()].find(([k, v]) => v === origin)); - -console.log(finalGrid.get('0,0,0,0')); - const circle = document.createElementNS(xmlns, 'circle'); circle.setAttributeNS(null, 'r', 5); circle.setAttributeNS(null, 'fill', 'green'); @@ -627,4 +620,3 @@ function addGroup(container, className) { container.appendChild(g); return g; } - -- cgit v1.2.3