Web Dev Solutions

Catalin Mititiuc

From dd8e7f04aaea205d2a3809d0ca1e26eb0a511c5e Mon Sep 17 00:00:00 2001 From: Catalin Mititiuc Date: Sun, 23 Jun 2024 19:23:33 -0700 Subject: WIP: add building structure --- src/radial.js | 160 ++++++++++++++++++++++++++++++++++++++++++++++++---------- 1 file changed, 132 insertions(+), 28 deletions(-) (limited to 'src/radial.js') diff --git a/src/radial.js b/src/radial.js index 5771099..9349bd1 100644 --- a/src/radial.js +++ b/src/radial.js @@ -328,10 +328,87 @@ const gameboard = svg.querySelector('.gameboard'); let mapsheet1 = { id: 'mapsheet1', - grid: mapsheet1hexCoords, + 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 }) + } + ] +}; + +let mapsheet20 = { + id: 'mapsheet2', + grid: new Map(mapsheetHexCoords), + buildings: [ + { + type: 'building2', + grid: new Map(buildingHexes.bld2), + position: ({ q, r, s }) => ({ q: q + 7, r: r + 7, s: s - 14 }) + }, + { + type: 'building6', + grid: new Map(buildingHexes.bld6), + position: ({ q, r, s }) => ({ q: q + 7, r: r - 6, s: s - 1 }) + }, + { + type: 'building1', + grid: new Map(buildingHexes.bld1), + position: ({ q, r, s }) => ({ q: q, r: r + 7, s: s - 7 }) + }, + { + type: 'building3', + grid: new Map(buildingHexes.bld3), + position: ({ q, r, s }) => ({ q: q - 14, r: r + 5, s: s + 9 }) + } + ] +}; + +let mapsheet30 = { + id: 'mapsheet3', + grid: new Map(mapsheetHexCoords), + buildings: [ + { + type: 'building2', + grid: new Map(buildingHexes.bld2), + position: reflectR({ q: 9, r: -6, s: -3 }) + }, + { + type: 'building6', + grid: new Map(buildingHexes.bld6), + position: reflectR({ q: 1, r: 6, s: -7 }) + }, + { + type: 'building1', + grid: new Map(buildingHexes.bld1), + position: ({ q, r, s }) => ({ q: q - 2, r: r - 5, s: s + 7 }) + }, + { + type: 'building3', + grid: new Map(buildingHexes.bld3), + position: reflectR({ q: 17, r: -7, s: -10 }) + } + ] +}; + +function reflectR(coords) { + return function ({ q, r, s }) { + return { + q: -s + coords.q, + r: -r + coords.r, + s: -q + coords.s, + transform: "scale(-1)" + }; + }; +} + let mapsheet2 = { id: 'mapsheet2', grid: mapsheet2hexCoords, @@ -350,7 +427,7 @@ let mapsheet4 = { buildings: mapsheet4BuildingCoords }; -function drawMapsheet(gameboard, { id, grid, buildings }) { +function drawMapsheet(gameboard, { id, grid, buildings }, { q: pq, r: pr, s: ps }) { const container = document.createElementNS(xmlns, 'g'); container.id = id; gameboard.appendChild(container); @@ -358,40 +435,64 @@ function drawMapsheet(gameboard, { id, grid, buildings }) { const gridContainer = document.createElementNS(xmlns, 'g'); gridContainer.classList.add('elevation-0'); container.appendChild(gridContainer); - drawHexes(gridContainer, grid, true); - const buildingHexes = buildings.reduce((acc, buildingCoords, index) => { + const buildingHexes = buildings.reduce((acc, building) => { const buildingContainer = document.createElementNS(xmlns, 'g'); buildingContainer.classList.add(`building`); - buildingContainer.classList.add(`building${index}`); + 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); - const hexes = translateCoords(buildingCoords, ({ q, r, s }) => ({ q, r, s, t: elevationLevel })); - drawHexes(hexContainer, hexes, true); - acc = new Map([...acc, ...hexes]); + 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 buildingStructure = document.createElementNS(xmlns, 'g'); + buildingStructure.classList.add('structure'); + buildingStructure.setAttributeNS(null, 'transform', `translate(${x}, ${y}) ${origin.transform ? origin.transform : ''}`); + 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(map) { - return vectorAdd(map, { q: 1, r: 0, s: -1 }, 33); +const horzMapVect = function(coords) { + return vectorAdd(coords, { q: 1, r: 0, s: -1 }, 33); } -const vertMapVect = function(map) { - return vectorAdd(map, { q: 1, r: -2, s: 1 }, 13); +const vertMapVect = function(coords) { + return vectorAdd(coords, { q: 1, r: -2, s: 1 }, 13); } -function vectorAdd(map, { q: dq, r: dr, s: ds }, scalar) { - return translateCoords(map, ({ q, r, s }) => ({ q: q - dq * scalar, r: r + dr * scalar, s: s - ds * scalar })); +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) { @@ -435,8 +536,16 @@ function findScalar(arr) { }); } -// const sheets = [[mapsheet2], [mapsheet3]]; -const sheets = [[mapsheet2], [mapsheet1], [mapsheet3]]; +// mapsheet10 = drawMapsheet(gameboard, mapsheet10, { q: -1, r: 0, s: 1 }); + +// const sheets = [[mapsheet10]]; + +// drawHexes(gameboard, buildingHexes.bld2, true); + +// drawHexes(addGroup(gameboard, 'part-1'), bld8, true); + +const sheets = [[mapsheet20], [mapsheet30]]; +// const sheets = [[mapsheet2], [mapsheet10], [mapsheet3]]; // const sheets = [[mapsheet4]]; // const sheets = [ @@ -444,24 +553,18 @@ const sheets = [[mapsheet2], [mapsheet1], [mapsheet3]]; // [mapsheet3, mapsheet4] // ]; -// mapsheet1 = drawMapsheet(gameboard, mapsheet1); - -// drawHexes(gameboard, buildingHexes.bld8, true); -// drawHexes(addGroup(gameboard, 'part-1'), bld8, true); - findScalar(findMult(sheets)).forEach(([vscalar, row]) => { - const vertMapVect = function(map) { - return vectorAdd(map, { q: 1, r: -2, s: 1 }, vscalar); + const vertMapVect = function(coords) { + return vectorAdd(coords, { q: 1, r: -2, s: 1 }, vscalar); } row.forEach(([hscalar, ms]) => { - const horzMapVect = function(map) { - return vectorAdd(map, { q: 1, r: 0, s: -1 }, hscalar); + const horzMapVect = function(coords) { + return vectorAdd(coords, { q: 1, r: 0, s: -1 }, hscalar); } - ms.grid = horzMapVect(vertMapVect(ms.grid)); - ms.buildings = ms.buildings.map(buildings => horzMapVect(vertMapVect(buildings))); - ms = drawMapsheet(gameboard, ms); + console.log(horzMapVect(vertMapVect({ q: 0, r: 0, s: 0 }))); + ms = drawMapsheet(gameboard, ms, horzMapVect(vertMapVect({ q: 0, r: 0, s: 0 }))); }) }); @@ -487,3 +590,4 @@ function addGroup(container, className) { container.appendChild(g); return g; } + -- cgit v1.2.3