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) {
return `${[q, r, s]}`;
}
function sameSigns(a, b) {
return a > -1 && b > -1 || a < 0 && b < 0;
}
function getNeighbors(coords) {
const [q, r, s] = coords.split(',').map(n => +n);
return [
toKey(q + 1, r, s - 1),
toKey(q - 1, r, s + 1),
toKey(q + 1, r - 1, s),
toKey(q - 1, r + 1, s),
toKey(q, r + 1, s - 1),
toKey(q, r - 1, s + 1),
]
}
function generateRadialCoords(l, { q, r, s }, radius) {
const origin = toKey(q, r, s);
const list = new Set(l);
const neighbors = new Set();
let next = new Set();
list.add(origin);
next.add(origin);
for (let i = 0; i < radius; i++) {
next.forEach(coords => {
getNeighbors(coords).forEach(n => {
list.add(n);
neighbors.add(n);
});
});
next = new Set(neighbors);
neighbors.clear();
}
return list;
}
function generateRadialCoordsRect({ rows, columns, odd = false, equal = true } = {}, { q = 0, r = 0, s = 0 } = {}) {
const list = new Set();
for (let row = 0; row < rows; row++) {
const alternating = row % 2;
const dr = row;
for (let col = 0; col < columns; col++) {
let dq = -(alternating + row) / 2 + alternating - col;
let ds = -(row + alternating) / 2 + col;
if (odd) {
dq = dq - alternating;
ds = ds + alternating;
}
list.add(toKey(q + dq, r + dr, s + ds));
}
if (!equal) {
if ((odd && !(row % 2)) || (!odd && row % 2)) {
let dq = -(alternating + row) / 2 + alternating - columns;
let ds = -(row + alternating) / 2 + columns;
if (odd) {
dq = dq - alternating;
ds = ds + alternating;
}
list.add(toKey(q + dq, r + dr, s + ds));
}
}
}
return list;
}
function drawHexes(el, list) {
list.forEach(key => {
const [q, r, s] = key.split(',').map(n => +n);
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;
y = r * vertSpacing;
const g = document.createElementNS(xmlns, 'g');
g.setAttributeNS(null, 'transform', `translate(${x}, ${y})`);
const use = document.createElementNS(xmlns, 'use');
use.setAttributeNS(null, 'href', '#hex');
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);
[use, qText, rText, sText].forEach(el => g.appendChild(el));
el.appendChild(g);
});
}
const grid = generateRadialCoordsRect({ rows: 25, columns: 33 }, { q: 22, r: -12, s: -10 });
const building1 = generateRadialCoordsRect({ rows: 5, columns: 3, equal: false }, { q: 7, r: -4, s: -3 });
const building2 = generateRadialCoordsRect({ rows: 4, columns: 3, odd: true }, { q: 0, r: -4, s: 4 });
const building3 = generateRadialCoords(new Map(), { q: -1, r: 2, s: -1 }, 1);
// generateRadialCoordsRect({ rows: 4, columns: 8, equal: false }, { q: 18, r: -8, s: -10 }).forEach(coords => grid.delete(coords));
// generateRadialCoordsRect({ rows: 8, columns: 2, equal: false }, { q: 9, r: -10, s: 1 }).forEach(coords => grid.delete(coords));
// generateRadialCoordsRect({ rows: 6, columns: 13, odd: true }, { q: 2, r: -8, s: 6 }).forEach(coords => grid.delete(coords));
// generateRadialCoordsRect({ rows: 1, columns: 10 }, { q: -1, r: -9, s: 10 }).forEach(coords => grid.delete(coords));
// generateRadialCoordsRect({ rows: 1, columns: 2 }, { q: -1, r: -2, s: 3 }).forEach(coords => grid.delete(coords));
// generateRadialCoordsRect({ rows: 1, columns: 6 }, { q: -8, r: -2, s: 10 }).forEach(coords => grid.delete(coords));
// generateRadialCoordsRect({ rows: 8, columns: 4, odd: true }, { q: 4, r: 3, s: -7 }).forEach(coords => grid.delete(coords));
// generateRadialCoordsRect({ rows: 8, columns: 4, odd: true }, { q: -5, r: 3, s: 2 }).forEach(coords => grid.delete(coords));
// generateRadialCoordsRect({ rows: 6, columns: 3, equal: false }, { q: -12, r: 3, s: 9 }).forEach(coords => grid.delete(coords));
// generateRadialCoordsRect({ rows: 8, columns: 4 }, { q: 10, r: 3, s: -13 }).forEach(coords => grid.delete(coords));
// generateRadialCoordsRect({ rows: 3, columns: 1 }, { q: 9, r: 7, s: -16 }).forEach(coords => grid.delete(coords));
// generateRadialCoordsRect({ rows: 5, columns: 1, odd: true }, { q: 5, r: 6, s: -11 }).forEach(coords => grid.delete(coords));
const buildingCoords = [
generateRadialCoordsRect({ rows: 4, columns: 8, equal: false }, { q: 18, r: -8, s: -10 }),
generateRadialCoordsRect({ rows: 8, columns: 2, equal: false }, { q: 9, r: -10, s: 1 }),
generateRadialCoordsRect({ rows: 6, columns: 13, odd: true }, { q: 2, r: -8, s: 6 }),
generateRadialCoordsRect({ rows: 1, columns: 10 }, { q: -1, r: -9, s: 10 }),
generateRadialCoordsRect({ rows: 1, columns: 2 }, { q: -1, r: -2, s: 3 }),
generateRadialCoordsRect({ rows: 1, columns: 6 }, { q: -8, r: -2, s: 10 }),
generateRadialCoordsRect({ rows: 8, columns: 4, odd: true }, { q: 4, r: 3, s: -7 }),
generateRadialCoordsRect({ rows: 8, columns: 4, odd: true }, { q: -5, r: 3, s: 2 }),
generateRadialCoordsRect({ rows: 6, columns: 3, equal: false }, { q: -12, r: 3, s: 9 }),
// hex building
// generateRadialCoordsRect({ rows: 8, columns: 4 }, { q: 10, r: 3, s: -13 }),
// generateRadialCoordsRect({ rows: 3, columns: 1 }, { q: 9, r: 7, s: -16 }),
// generateRadialCoordsRect({ rows: 5, columns: 1, odd: true }, { q: 5, r: 6, s: -11 }),
]
buildingCoords.forEach(building => building.forEach(coords => grid.delete(coords)));
// building2.forEach(coords => grid.delete(coords));
// building3.forEach(coords => grid.delete(coords));
// drawHexes(svg, generateRadialCoords(new Map(), { q: 0, r: 0, s: 0 }, 16));
// const rectEven = generateRadialCoordsRect({ rows: 2, columns: 2 }, { q: 9, r: -5, s: -4 });
// const rectOdd = generateRadialCoordsRect({ rows: 2, columns: 2, odd: true }, { q: 8, r: -2, s: -6 });
// const rectEvenUnevenCols = generateRadialCoordsRect({ rows: 2, columns: 1, equal: false}, { q: 6, r: 1, s: -7 });
// const rectOddUnevenCols = generateRadialCoordsRect({ rows: 2, columns: 1, odd: true, equal: false}, { q: 5, r: 4, s: -9 });
// drawHexes(svg, rectEven);
// drawHexes(svg, rectOdd);
// drawHexes(svg, rectEvenUnevenCols);
// drawHexes(svg, rectOddUnevenCols);
// drawHexes(svg, generateRadialCoordsRect({ rows: 7, columns: 6, odd: true, equal: false }, { q: 5, r: -5, s: 0 }));
const buildings = document.createElementNS(xmlns, 'g');
buildings.classList.add('buildings');
const bldElevationBasement = document.createElementNS(xmlns, 'g');
bldElevationBasement.classList.add('elevation-basement');
buildingCoords.forEach(building => drawHexes(bldElevationBasement, building));
const bldElevation1 = document.createElementNS(xmlns, 'g');
bldElevation1.classList.add('elevation-1');
buildingCoords.forEach(building => drawHexes(bldElevation1, building));
const bldElevation2 = document.createElementNS(xmlns, 'g');
bldElevation2.classList.add('elevation-2');
buildingCoords.forEach(building => drawHexes(bldElevation2, building));
const elevation1 = document.createElementNS(xmlns, 'g');
elevation1.classList.add('elevation-1');
drawHexes(elevation1, grid);
buildings.appendChild(bldElevationBasement);
buildings.appendChild(bldElevation1);
buildings.appendChild(bldElevation2);
const buildingBlah = svg.querySelector('#building2');
const bld2grid = document.createElementNS(xmlns, 'g');
drawHexes(bld2grid, generateRadialCoordsRect({ rows: 9, columns: 4, odd: true, equal: false }, { q: 4, r: -4, s: 0 }));
drawHexes(bld2grid, generateRadialCoordsRect({ rows: 5, columns: 1 }, { q: 3, r: -1, s: -2 }));
drawHexes(bld2grid, generateRadialCoordsRect({ rows: 5, columns: 1, odd: true }, { q: -2, r: -1, s: 3 }));
const bld2elvBasement = bld2grid.cloneNode(true);
bld2elvBasement.classList.add('elevation-basement');
buildingBlah.querySelector('.floor').after(bld2elvBasement);
const bld2elv1 = bld2grid.cloneNode(true);
bld2elv1.classList.add('elevation-1');
bld2elvBasement.after(bld2elv1);
const bld2elv2 = bld2grid.cloneNode(true);
bld2elv2.classList.add('elevation-2');
bld2elv1.after(bld2elv2);
const bld2elvRoof = bld2grid.cloneNode(true);
bld2elvRoof.classList.add('elevation-roof');
bld2elv2.after(bld2elvRoof);
buildingBlah.before(elevation1);
buildingBlah.before(buildings);