Web Dev Solutions

Catalin Mititiuc

if (window.IS_DEV) { const source = new EventSource('/esbuild'); source.addEventListener('change', () => location.reload()); } const xmlns = 'http://www.w3.org/2000/svg'; const svg = document.querySelector('svg'); const hex = { inradius: 8.66, circumradius: 10, } const horzSpacing = hex.inradius; const vertSpacing = hex.circumradius * 3 / 2; function toKey({ q, r, s, t = 0 } = {}) { return `${[q, r, s, t]}`; } function fromKey(key) { const split = key.split(','); return { q: +split[0], r: +split[1], s: +split[2], t: +(split[3] || 1) }; } function sameSigns(a, b) { return a > -1 && b > -1 || a < 0 && b < 0; } function getNeighbors(coords) { const { q, r, s, t } = fromKey(coords); return [ toKey({ q: q + 1, r: r, s: s - 1, t }), toKey({ q: q - 1, r: r, s: s + 1, t }), toKey({ q: q + 1, r: r - 1, s: s, t }), toKey({ q: q - 1, r: r + 1, s: s, t }), toKey({ q: q, r: r + 1, s: s - 1, t }), toKey({ q: q, r: r - 1, s: s + 1, t }), ] } function addHexText(q, r, s, v) { const qText = document.createElementNS(xmlns, 'text'); qText.textContent = q; qText.setAttributeNS(null, 'x', -3); qText.setAttributeNS(null, 'y', -3); const rText = document.createElementNS(xmlns, 'text'); rText.textContent = r; rText.setAttributeNS(null, 'x', 5); rText.setAttributeNS(null, 'y', 1.5); const sText = document.createElementNS(xmlns, 'text'); sText.textContent = s; sText.setAttributeNS(null, 'x', -3); sText.setAttributeNS(null, 'y', 5); const vText = document.createElementNS(xmlns, 'text'); vText.textContent = v; vText.style.fill = 'red'; vText.setAttributeNS(null, 'y', 1); vText.setAttributeNS(null, 'x', -2); return [qText, rText, sText, vText]; } function radialToScreenCoords({ q, r, s }) { let x; if (q === s) x = 0; else if (sameSigns(q, s)) x = Math.abs(q - s); else x = Math.abs(q) + Math.abs(s); x = (q > s ? -1 : 1) * x * horzSpacing; const y = r * vertSpacing; return { x, y }; } function drawHexes(el, list, renderText = false) { for ([key, v] of list) { const { q, r, s, t } = fromKey(key); const { x, y } = radialToScreenCoords({ q, r, s }); const cell = document.createElementNS(xmlns, 'g'); cell.setAttributeNS(null, 'transform', `translate(${x}, ${y})`); const use = document.createElementNS(xmlns, 'use'); use.setAttributeNS(null, 'href', '#hex'); cell.appendChild(use); if (renderText) addHexText(q, r, s, t).forEach(txt => cell.appendChild(txt)); list.set(key, cell); el.appendChild(cell); } } function translateRadialCoords({ q, r, s }, direction, distance = 1) { return { q: q + direction.q * distance, r: r + direction.r * distance, s: s + direction.s * distance }; } function generateRadialCoords(l, { q, r, s, t = 0 } = {}, { left, top, right, bottom }, offset = false) { const origin = toKey({ q, r, s, t }); const list = new Map(l); list.set(origin, t); let queue = [origin]; while (queue.length > 0) { const v = queue.shift(); getNeighbors(v).forEach(w => { const { q: wq, r: wr, s: ws } = fromKey(w); const rDist = Math.abs(wr - r); const alternating = rDist % 2; const dr = (rDist + alternating) / 2; const dLeft = ['right', 'both'].includes(offset) ? left - alternating : left; const dRight = ['left', 'both'].includes(offset) ? right - alternating : right; if ([ !list.has(w), wr < bottom + r && wr > -top + r, wq > -dRight + q - dr && wq < dLeft + q + dr, ws > -dLeft + s - dr && ws < dRight + s + dr, ].every(v => v)) { list.set(w, dr); queue.push(w); } }); } return list; } function translateCoords(map, translator) { const translated = new Map(); for ([key, val] of map) { const { q, r, s, t } = fromKey(key); translated.set(toKey(translator({ q, r, s, t })), val); } return translated; } document.querySelectorAll('[name="select-elevation"]').forEach(el => { const gameboard = document.querySelector('.gameboard'); el.addEventListener('change', function (e) { gameboard.dataset.viewElevation = this.value }); }); // function placeBuilding(buildingSelector, mapsheetSelector) { // const building = svg.querySelector(buildingSelector); // const bld2grid = document.createElementNS(xmlns, 'g'); // const bld2origin = { q: 0, r: 0, s: 0 }; // const bld2map2 = svg.querySelector(`${mapsheetSelector} ${buildingSelector}`); // const [q, r, s] = bld2map2.dataset.placement.split(',').map(n => +n); // const map2bld2place = { q, r, s }; // bld2hexes = building.querySelector('[data-grid-footprint]').dataset.gridFootprint.split(':').map(coords => { // const [q, r, s] = coords.split(',').map(n => +n); // const { q: tq, r: tr, s: ts } = translateRadialCoords({ q, r, s }, map2bld2place); // return [toKey({ q: tq, r: tr, s: ts }), null]; // }); // const bld2screenCoords = radialToScreenCoords(translateRadialCoords(bld2origin, map2bld2place)); // const bld2structure = document.createElementNS(xmlns, 'g'); // bld2structure.setAttributeNS(null, 'transform', `translate(${bld2screenCoords.x}, ${bld2screenCoords.y})`) // building.querySelectorAll('[data-grid-footprint] > *').forEach(el => { // const use = document.createElementNS(xmlns, 'use'); // use.setAttributeNS(null, 'href', `#${el.id}`); // el.classList.forEach(className => use.classList.add(className)); // bld2structure.appendChild(use); // }); // drawHexes(bld2grid, new Map(bld2hexes), true); // const bld2elvBasement = bld2grid.cloneNode(true); // bld2elvBasement.classList.add('elevation-basement'); // bld2map2.appendChild(bld2elvBasement); // const bld2elv1 = bld2grid.cloneNode(true); // bld2elv1.classList.add('elevation-0'); // bld2map2.appendChild(bld2elv1); // const bld2elv2 = bld2grid.cloneNode(true); // bld2elv2.classList.add('elevation-1'); // bld2map2.appendChild(bld2elv2); // const bld2elvRoof = bld2grid.cloneNode(true); // bld2elvRoof.classList.add('elevation-roof'); // bld2map2.appendChild(bld2elvRoof); // bld2map2.appendChild(bld2structure); // } const buildingHexes = {}; buildingHexes.bld1 = generateRadialCoords( new Map(), { q: 0, r: 0, s: 0 }, { left: 3, top: 5, right: 3, bottom: 5 }, 'both' ); buildingHexes.bld2 = [ [{ q: 0, r: 0, s: 0 }, { left: 3, top: 5, right: 3, bottom: 5 }, 'both'], [{ q: 3, r: 0, s: -3 }, { left: 1, top: 2, right: 1, bottom: 4 }, 'right'], [{ q: -3, r: 0, s: 3 }, { left: 1, top: 2, right: 1, bottom: 4 }, 'left'], ].reduce((acc, args) => new Map([...generateRadialCoords(acc, ...args)]), new Map()); buildingHexes.bld3 = generateRadialCoords( new Map(), { q: 0, r: 0, s: 0 }, { left: 2, top: 3, right: 2, bottom: 4 } ); buildingHexes.bld4 = generateRadialCoords( new Map(), { q: 0, r: 0, s: 0 }, { left: 2, top: 4, right: 3, bottom: 5 }, 'left' ); buildingHexes.bld5 = generateRadialCoords( new Map(), { q: 0, r: 0, s: 0 }, { left: 5, top: 3, right: 4, bottom: 2 } ); buildingHexes.bld6 = generateRadialCoords( new Map(), { q: 0, r: 0, s: 0 }, { left: 1, top: 5, right: 2, bottom: 4 } ); buildingHexes.bld7 = [ [{ q: 0, r: 0, s: 0 }, { left: 7, top: 4, right: 7, bottom: 3 }, 'left'], [{ q: 1, r: -4, s: 3 }, { left: 5, top: 1, right: 6, bottom: 1 }], [{ q: 5, r: 3, s: -8 }, { left: 1, top: 1, right: 2, bottom: 1 }], [{ q: -4, r: 3, s: 1 }, { left: 3, top: 1, right: 4, bottom: 1 }], ].reduce((acc, args) => new Map([...generateRadialCoords(acc, ...args)]), new Map()); buildingHexes.bld8 = [ [{ q: 0, r: 0, s: 0 }, { left: 6, top: 7, right: 6, bottom: 4 }, 'both'], [{ q: 5, r: 2, s: -7 }, { left: 1, top: 2, right: 1, bottom: 2 }], [{ q: 1, r: 5, s: -6 }, { left: 4, top: 2, right: 4, bottom: 2 }, 'both'], [{ q: -2, r: 4, s: -2 }, { left: 1, top: 1, right: 1, bottom: 1 }], [{ q: 7, r: -7, s: 0 }, { left: 2, top: 1, right: 2, bottom: 1 }], [{ q: -2, r: -7, s: 9 }, { left: 2, top: 1, right: 2, bottom: 1 }], [{ q: -3, r: -6, s: 9 }, { left: 1, top: 1, right: 1, bottom: 2 }, 'left'], [{ q: -7, r: 3, s: 4 }, { left: 1, top: 3, right: 2, bottom: 3 }, 'right'], [{ q: -7, r: 4, s: 3 }, { left: 1, top: 1, right: 1, bottom: 1 }] ].reduce((acc, args) => new Map([...generateRadialCoords(acc, ...args)]), new Map()); const mapsheet1BuildingCoords = [ translateCoords(buildingHexes.bld2, ({ q, r, s }) => ({ q: q + 7, r: r + 7, s: s - 14 })), translateCoords(buildingHexes.bld4, ({ q, r, s }) => ({ q: -s + 14, r: -r - 6, s: -q - 8 })), translateCoords(buildingHexes.bld3, ({ q, r, s }) => ({ q: q - 6, r: r - 6, s: s + 12 })), translateCoords(buildingHexes.bld6, ({ q, r, s }) => ({ q: -s - 12, r: -r + 7, s: -q + 5 })), generateRadialCoords(new Map(), { q: 0, r: 0, s: 0 }, { left: 4, top: 5, right: 3, bottom: 5 }), ]; const mapsheet2BuildingCoords = [ translateCoords(buildingHexes.bld1, ({ q, r, s }) => ({ q: q, r: r + 7, s: s - 7 })), translateCoords(buildingHexes.bld2, ({ q, r, s }) => ({ q: q + 7, r: r + 7, s: s - 14 })), translateCoords(buildingHexes.bld3, ({ q, r, s }) => ({ q: q - 14, r: r + 5, s: s + 9 })), translateCoords(buildingHexes.bld4, ({ q, r, s }) => ({ q: q - 8, r: r + 6, s: s + 2 })), translateCoords(buildingHexes.bld5, ({ q, r, s }) => ({ q: q + 13, r: r - 6, s: s - 7 })), translateCoords(buildingHexes.bld6, ({ q, r, s }) => ({ q: q + 7, r: r - 6, s: s - 1 })), translateCoords(buildingHexes.bld7, ({ q, r, s }) => ({ q: q - 6, r: r - 5, s: s + 11 })), ]; const mapsheet3BuildingCoords = [ translateCoords(buildingHexes.bld1, ({ q, r, s }) => ({ q: q - 2, r: r - 5, s: s + 7 })), translateCoords(buildingHexes.bld2, ({ q, r, s }) => ({ q: -s + 9, r: -r - 6, s: -q - 3 })), translateCoords(buildingHexes.bld3, ({ q, r, s }) => ({ q: -s + 17, r: -r - 7, s: -q - 10 })), translateCoords(buildingHexes.bld4, ({ q, r, s }) => ({ q: q - 9, r: r - 6, s: s + 15 })), translateCoords(buildingHexes.bld5, ({ q, r, s }) => ({ q: q + 6, r: r + 8, s: s - 14 })), translateCoords(buildingHexes.bld6, ({ q, r, s }) => ({ q: -s + 1, r: -r + 6, s: -q - 7 })), translateCoords(buildingHexes.bld7, ({ q, r, s }) => ({ q: q - 12, r: r + 8, s: s + 4 })), ] const mapsheet4BuildingCoords = [ translateCoords(buildingHexes.bld8, ({ q, r, s }) => ({ q: q + 1, r: r + 1, s: s - 2 })), ] const mapsheetHexCoords = generateRadialCoords(new Map(), { q: 0, r: 0, s: 0 }, { left: 17, top: 13, right: 17, bottom: 14 }, 'left'); const mapsheet1hexCoords = new Map(mapsheetHexCoords); const mapsheet2hexCoords = new Map(mapsheetHexCoords); const mapsheet3hexCoords = new Map(mapsheetHexCoords); const mapsheet4hexCoords = new Map(mapsheetHexCoords); mapsheet1BuildingCoords.forEach(building => { for ([coords, v] of building) mapsheet1hexCoords.delete(coords); }); mapsheet2BuildingCoords.forEach(building => { for ([coords, v] of building) mapsheet2hexCoords.delete(coords); }); mapsheet3BuildingCoords.forEach(building => { for ([coords, v] of building) mapsheet3hexCoords.delete(coords); }); mapsheet4BuildingCoords.forEach(building => { for ([coords, v] of building) mapsheet4hexCoords.delete(coords); }); const gameboard = svg.querySelector('.gameboard'); let mapsheet1 = { id: 'mapsheet1', grid: new Map(mapsheetHexCoords), buildings: mapsheet1BuildingCoords }; let mapsheet10 = { id: 'mapsheet1', grid: new Map(mapsheetHexCoords), buildings: [ { type: 'building2', grid: buildingHexes.bld2, position: ({ q, r, s }) => ({ q: q + 7, r: r + 7, s: s - 14 }) }, { type: 'building3', grid: new Map(buildingHexes.bld3), position: ({ q, r, s }) => ({ q: q - 6, r: r - 6, s: s + 12 }) }, { type: 'building4', grid: generateRadialCoords( new Map(), { q: 0, r: 0, s: 0 }, { left: 2, top: 4, right: 3, bottom: 5 }, 'right' ), position: ({ q, r, s }) => ({ q: q + 15, r: r - 7, s: s - 8, transform: (x, y) => `translate(${x + horzSpacing}, ${y})` }) }, { type: 'building6', grid: new Map(buildingHexes.bld6), position: rotate180({ q: -13, r: 7, s: 6 }) }, ] }; let mapsheet20 = { id: 'mapsheet2', grid: new Map(mapsheetHexCoords), buildings: [ { type: 'building1', grid: new Map(buildingHexes.bld1), position: ({ q, r, s }) => ({ q: q, r: r + 7, s: s - 7 }) }, { type: 'building2', grid: new Map(buildingHexes.bld2), position: ({ q, r, s }) => ({ q: q + 7, r: r + 7, s: s - 14 }) }, { type: 'building3', grid: new Map(buildingHexes.bld3), position: ({ q, r, s }) => ({ q: q - 14, r: r + 5, s: s + 9 }) }, { type: 'building4', grid: new Map(buildingHexes.bld4), position: ({ q, r, s }) => ({ q: q - 8, r: r + 6, s: s + 2 }) }, { type: 'building5', grid: new Map(buildingHexes.bld5), position: ({ q, r, s }) => ({ q: q + 13, r: r - 6, s: s - 7 }) }, { type: 'building6', grid: new Map(buildingHexes.bld6), position: ({ q, r, s }) => ({ q: q + 7, r: r - 6, s: s - 1 }) }, { type: 'building7', grid: new Map(buildingHexes.bld7), position: ({ q, r, s }) => ({ q: q - 6, r: r - 5, s: s + 11 }), }, ] }; let mapsheet30 = { id: 'mapsheet3', grid: new Map(mapsheetHexCoords), buildings: [ { type: 'building1', grid: new Map(buildingHexes.bld1), position: ({ q, r, s }) => ({ q: q - 2, r: r - 5, s: s + 7 }) }, { type: 'building2', grid: new Map(buildingHexes.bld2), position: rotate180({ q: 9, r: -6, s: -3 }) }, { type: 'building3', grid: new Map(buildingHexes.bld3), position: rotate180({ q: 17, r: -7, s: -10 }) }, { type: 'building4', grid: new Map(buildingHexes.bld4), position: rotate180({ q: -10, r: -5, s: 15 }) }, { type: 'building5', grid: new Map(buildingHexes.bld5), position: ({ q, r, s }) => ({ q: q + 6, r: r + 8, s: s - 14 }) }, { type: 'building6', grid: new Map(buildingHexes.bld6), position: rotate180({ q: 0, r: 6, s: -6 }) }, { type: 'building7', grid: new Map(buildingHexes.bld7), position: ({ q, r, s }) => ({ q: q - 12, r: r + 8, s: s + 4 }) }, ] }; function rotate180(coords) { return function ({ q, r, s }) { return { q: -q + coords.q, r: -r + coords.r, s: -s + coords.s, transform: (x, y) => `rotate(180 ${x}, ${y}) translate(${x}, ${y})` }; }; } let mapsheet2 = { id: 'mapsheet2', grid: mapsheet2hexCoords, buildings: mapsheet2BuildingCoords }; let mapsheet3 = { id: 'mapsheet3', grid: mapsheet3hexCoords, buildings: mapsheet3BuildingCoords }; let mapsheet4 = { id: 'mapsheet4', grid: mapsheet4hexCoords, buildings: mapsheet4BuildingCoords }; function drawMapsheet(gameboard, { id, grid, buildings }, { q: pq, r: pr, s: ps }) { const container = document.createElementNS(xmlns, 'g'); container.id = id; gameboard.appendChild(container); const gridContainer = document.createElementNS(xmlns, 'g'); gridContainer.classList.add('elevation-0'); container.appendChild(gridContainer); const buildingHexes = buildings.reduce((acc, building) => { const buildingContainer = document.createElementNS(xmlns, 'g'); buildingContainer.classList.add(`building`); buildingContainer.classList.add(building.type); container.appendChild(buildingContainer); let buildingGrid = translateCoords(building.grid, building.position); buildingGrid = translateCoords(buildingGrid, ({ q, r, s }) => ({ q: q + pq, r: r + pr, s: s + ps })); [-1, 0, 1].forEach(elevationLevel => { const hexContainer = document.createElementNS(xmlns, 'g'); hexContainer.classList.add(`elevation-${elevationLevel === -1 ? 'basement' : elevationLevel}`); buildingContainer.appendChild(hexContainer); buildingGrid = translateCoords(buildingGrid, ({ q, r, s }) => ({ q, r, s, t: elevationLevel })); drawHexes(hexContainer, buildingGrid, true); acc = new Map([...acc, ...buildingGrid]); }); const buildingTemplate = document.querySelector(`defs #${building.type}`); const origin = building.position({ q: 0, r: 0, s: 0 }); 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.setAttributeNS(null, 'transform', transform(x, y)); buildingContainer.appendChild(buildingStructure); for (let child of buildingTemplate.children) { const use = document.createElementNS(xmlns, 'use'); use.setAttributeNS(null, 'href', `#${child.id}`); child.classList.forEach(className => use.classList.add(className)); buildingStructure.appendChild(use); } return acc; }, new Map()); grid = translateCoords(grid, ({ q, r, s }) => ({ q: q + pq, r: r + pr, s: s + ps })); for ([coords, v] of buildingHexes) grid.delete(coords); drawHexes(gridContainer, grid, true); return new Map([...grid, ...buildingHexes]); // return { id, grid, buildings }; } const horzMapVect = function(coords) { return vectorAdd(coords, { q: 1, r: 0, s: -1 }, 33); } const vertMapVect = function(coords) { return vectorAdd(coords, { q: 1, r: -2, s: 1 }, 13); } function vectorAdd({ q, r, s }, { q: dq, r: dr, s: ds }, scalar) { return ({ q: q - dq * scalar, r: r + dr * scalar, s: s - ds * scalar }); } function findMult(arr) { if (arr.length % 2) return arr.map((v, index) => { const row = v.length % 2 ? v.map((rv, rindex) => [Math.floor(rindex - v.length / 2) + 1, rv]) : v.map((rv, rindex) => [Math.floor(rindex - v.length / 2), rv]).map(([rm, rv]) => [rm > -1 ? rm + 1 : rm, rv]); return [Math.floor(index - arr.length / 2) + 1, row]; }); else return arr.map((v, index) => { const row = v.length % 2 ? v.map((rv, rindex) => [Math.floor(rindex - v.length / 2) + 1, rv]) : v.map((rv, rindex) => [Math.floor(rindex - v.length / 2), rv]).map(([rm, rv]) => [rm > -1 ? rm + 1 : rm, rv]); return [Math.floor(index - arr.length / 2), row]; }).map(([m, v]) => [m > -1 ? m + 1 : m, v]); } function findScalar(arr) { let pos; let neg; if (arr.length % 2) { pos = -13; neg = -13; } else { pos = -7; neg = -6; } return arr.map(([mult, v]) => { let hpos, hneg; if (v.length % 2) { hpos = 33; hneg = 33; } else { hpos = 16; hneg = 17; } const row = v.map(([hmult, hv]) => [hmult < 0 ? hmult * hneg : hmult * hpos, hv]); return [mult < 0 ? mult * neg : mult * pos, row]; }); } // mapsheet10 = drawMapsheet(gameboard, mapsheet10, { q: -1, r: 0, s: 1 }); let sheets = []; // sheets = [[mapsheet30]]; // drawHexes(gameboard, buildingHexes.bld4, true); // drawHexes(gameboard, translateCoords(buildingHexes.bld4, rotate180({ q: -9, r: -6, s: 15 })), true); // drawHexes(gameboard, translateCoords(buildingHexes.bld4, rotate180({ q: 0, r: 0, s: 0 })), true); const buildingTemplate = document.querySelector(`defs #building4`); const { x, y } = radialToScreenCoords({ q: 0, r: 0, s: 0 }); const buildingStructure = document.createElementNS(xmlns, 'g'); buildingStructure.classList.add('structure'); buildingStructure.classList.add('building'); buildingStructure.setAttributeNS(null, 'transform', `translate(${x}, ${y}) rotate(180)`); // gameboard.appendChild(buildingStructure); for (let child of buildingTemplate.children) { const use = document.createElementNS(xmlns, 'use'); use.setAttributeNS(null, 'href', `#${child.id}`); child.classList.forEach(className => use.classList.add(className)); buildingStructure.appendChild(use); } // sheets = [[mapsheet20], [mapsheet30]]; // sheets = [[mapsheet20], [mapsheet10], [mapsheet30]]; sheets = [[mapsheet10]]; // const sheets = [ // [mapsheet2, mapsheet1], // [mapsheet3, mapsheet4] // ]; findScalar(findMult(sheets)).forEach(([vscalar, row]) => { const vertMapVect = function(coords) { return vectorAdd(coords, { q: 1, r: -2, s: 1 }, vscalar); } row.forEach(([hscalar, ms]) => { const horzMapVect = function(coords) { return vectorAdd(coords, { q: 1, r: 0, s: -1 }, hscalar); } ms = drawMapsheet(gameboard, ms, horzMapVect(vertMapVect({ q: 0, r: 0, s: 0 }))); }) }); const circle = document.createElementNS(xmlns, 'circle'); circle.setAttributeNS(null, 'r', 5); circle.setAttributeNS(null, 'fill', 'green'); circle.setAttributeNS(null, 'stroke', 'gold'); circle.setAttributeNS(null, 'stroke-width', 1); const circle1 = circle.cloneNode(); circle1.setAttributeNS(null, 'fill', 'blue'); const circle2 = circle.cloneNode(); circle2.setAttributeNS(null, 'fill', 'red'); // mapsheet2.get('0,0,0,0').appendChild(circle); // mapsheet2.get('0,6,-6,1').appendChild(circle1); // mapsheet2.get('0,9,-9,0').appendChild(circle2); function addGroup(container, className) { const g = document.createElementNS(xmlns, 'g'); g.classList.add(className); container.appendChild(g); return g; }