Separate polygon sides from board radius for hex templates; fix template IsInBounds
- HexSides now stores radius+1 (for hex coordinate math); HexPolygonSides stores the corner count (3–10) - Board.IsTemplateBased: template boards use tile existence for IsInBounds/GetNeighbors instead of the hexagonal formula, so N-gon and other custom shapes work correctly - HexGrid.AllNeighbors: unfiltered 6-neighbor generator for template board use - ApplyHexConfig: generates a regular N-gon on the hex canvas — tiles outside the polygon are marked absent; N corner zones auto-generated for each player count 2..N - Admin builder UI: separate "Polygon Sides" (3–10 corners) and "Radius (tiles)" inputs - BoardTemplate entity and migration: HexPolygonSides column added to board_templates - BoardTemplateDto exposes HexPolygonSides to client Generated with [Claude Code](https://claude.ai/code) via [Happy](https://happy.engineering) Co-Authored-By: Claude <noreply@anthropic.com> Co-Authored-By: Happy <yesreply@happy.engineering>
This commit is contained in:
@@ -102,13 +102,19 @@ else
|
||||
@if (_template.GridType == "Hexagon")
|
||||
{
|
||||
<div class="mb-3">
|
||||
<label class="form-label fw-semibold">Hex Sides (N)</label>
|
||||
<label class="form-label fw-semibold">Polygon Sides</label>
|
||||
<input type="number" class="form-control" min="3" max="10"
|
||||
@bind="_pendingHexSides" />
|
||||
<div class="form-text">Diameter = 2N−1. Tiles: 3N²−3N+1.</div>
|
||||
@bind="_pendingHexPolySides" />
|
||||
<div class="form-text">Number of corners / player slots. 3 = triangle, 6 = hexagon, 10 = decagon.</div>
|
||||
</div>
|
||||
<button class="btn btn-outline-secondary btn-sm w-100" @onclick="ApplyHexSides">
|
||||
Apply Hex Sides
|
||||
<div class="mb-3">
|
||||
<label class="form-label fw-semibold">Radius (tiles)</label>
|
||||
<input type="number" class="form-control" min="2" max="15"
|
||||
@bind="_pendingHexRadius" />
|
||||
<div class="form-text">Board radius from center to corner. Diameter = 2R+1.</div>
|
||||
</div>
|
||||
<button class="btn btn-outline-secondary btn-sm w-100" @onclick="ApplyHexConfig">
|
||||
Apply
|
||||
</button>
|
||||
}
|
||||
else
|
||||
@@ -260,8 +266,9 @@ else
|
||||
<div class="card-header d-flex justify-content-between align-items-center">
|
||||
@if (_template.GridType == "Hexagon")
|
||||
{
|
||||
int _hn = _template.HexSides > 0 ? _template.HexSides : _pendingHexSides;
|
||||
<span>Canvas <span class="text-muted small">N=@_hn hex (@(2*_hn-1)×@(2*_hn-1))</span></span>
|
||||
int _hpoly = _template.HexPolygonSides > 0 ? _template.HexPolygonSides : _pendingHexPolySides;
|
||||
int _hradius = _template.HexSides > 1 ? _template.HexSides - 1 : _pendingHexRadius;
|
||||
<span>Canvas <span class="text-muted small">@_hpoly-gon, R=@_hradius (@(2*_hradius+1)×@(2*_hradius+1))</span></span>
|
||||
}
|
||||
else
|
||||
{
|
||||
@@ -276,7 +283,8 @@ else
|
||||
{
|
||||
const double HexR = 16.0;
|
||||
const double HexPad = 12.0;
|
||||
int hexN = _template.HexSides > 0 ? _template.HexSides : Math.Max(3, _pendingHexSides);
|
||||
// hexN = radius + 1 (for HexGrid coordinate math)
|
||||
int hexN = _template.HexSides > 0 ? _template.HexSides : (_pendingHexRadius + 1);
|
||||
int hexDim = 2 * hexN - 1;
|
||||
double svgW = HexR * Math.Sqrt(3) * (hexDim - 1) + 2 * HexR + 2 * HexPad;
|
||||
double svgH = HexR * 1.5 * (hexDim - 1) + 2 * HexR + 2 * HexPad;
|
||||
@@ -393,9 +401,10 @@ else
|
||||
// ── Template + in-memory state ────────────────────────────────────────────
|
||||
|
||||
private BoardTemplate _template = new();
|
||||
private int _pendingWidth = 8;
|
||||
private int _pendingHeight = 8;
|
||||
private int _pendingHexSides = 5;
|
||||
private int _pendingWidth = 8;
|
||||
private int _pendingHeight = 8;
|
||||
private int _pendingHexPolySides = 6; // polygon corner count (3–10)
|
||||
private int _pendingHexRadius = 5; // board radius in tiles (2–15)
|
||||
|
||||
// Only non-default tiles stored; key = "x,y"
|
||||
private Dictionary<string, TileDefinition> _tiles = new();
|
||||
@@ -439,10 +448,11 @@ else
|
||||
|
||||
if (_isNew)
|
||||
{
|
||||
_template = new BoardTemplate { CreatedAt = DateTimeOffset.UtcNow, UpdatedAt = DateTimeOffset.UtcNow };
|
||||
_pendingWidth = _template.Width;
|
||||
_pendingHeight = _template.Height;
|
||||
_pendingHexSides = 5;
|
||||
_template = new BoardTemplate { CreatedAt = DateTimeOffset.UtcNow, UpdatedAt = DateTimeOffset.UtcNow };
|
||||
_pendingWidth = _template.Width;
|
||||
_pendingHeight = _template.Height;
|
||||
_pendingHexPolySides = 6;
|
||||
_pendingHexRadius = 5;
|
||||
}
|
||||
else
|
||||
{
|
||||
@@ -453,10 +463,11 @@ else
|
||||
_loading = false;
|
||||
return;
|
||||
}
|
||||
_template = t;
|
||||
_pendingWidth = t.Width;
|
||||
_pendingHeight = t.Height;
|
||||
_pendingHexSides = t.HexSides > 0 ? t.HexSides : 5;
|
||||
_template = t;
|
||||
_pendingWidth = t.Width;
|
||||
_pendingHeight = t.Height;
|
||||
_pendingHexPolySides = t.HexPolygonSides > 0 ? t.HexPolygonSides : 6;
|
||||
_pendingHexRadius = t.HexSides > 1 ? t.HexSides - 1 : 5;
|
||||
DeserializeTiles(t.TilesJson);
|
||||
DeserializeZones(t.StartingZonesJson);
|
||||
}
|
||||
@@ -525,27 +536,87 @@ else
|
||||
});
|
||||
}
|
||||
|
||||
private void ApplyHexSides()
|
||||
/// <summary>
|
||||
/// Generate a regular N-gon of hex tiles and N corner starting zones.
|
||||
/// HexSides is stored as radius+1 (for HexGrid coordinate math).
|
||||
/// HexPolygonSides is stored as N (the corner count / polygon shape).
|
||||
/// </summary>
|
||||
private void ApplyHexConfig()
|
||||
{
|
||||
var n = Math.Clamp(_pendingHexSides, 3, 10);
|
||||
_pendingHexSides = n;
|
||||
_template.HexSides = n;
|
||||
int dim = 2 * n - 1;
|
||||
_template.Width = dim;
|
||||
_template.Height = dim;
|
||||
int N = Math.Clamp(_pendingHexPolySides, 3, 10);
|
||||
int R = Math.Clamp(_pendingHexRadius, 2, 15);
|
||||
int sides = R + 1; // HexGrid "sides" parameter = radius + 1
|
||||
int dim = 2 * R + 1;
|
||||
|
||||
var toRemoveTiles = _tiles.Keys
|
||||
.Where(k => { var p = k.Split(','); return !IsHexInBounds(int.Parse(p[0]), int.Parse(p[1]), n); })
|
||||
.ToList();
|
||||
foreach (var k in toRemoveTiles) _tiles.Remove(k);
|
||||
_pendingHexPolySides = N;
|
||||
_pendingHexRadius = R;
|
||||
_template.HexSides = sides;
|
||||
_template.HexPolygonSides = N;
|
||||
_template.Width = dim;
|
||||
_template.Height = dim;
|
||||
|
||||
foreach (var seatMap in _zones.Values)
|
||||
foreach (var tileSet in seatMap.Values)
|
||||
tileSet.RemoveWhere(k =>
|
||||
_tiles.Clear();
|
||||
_zones.Clear();
|
||||
|
||||
// Mark every tile that is inside the hexagonal arrangement but outside the N-gon as absent.
|
||||
// Tiles inside the N-gon are left as default (no entry = void tile = present).
|
||||
for (int x = 0; x < dim; x++)
|
||||
for (int y = 0; y < dim; y++)
|
||||
{
|
||||
int q = x - R, r = y - R;
|
||||
if (Math.Max(Math.Abs(q), Math.Max(Math.Abs(r), Math.Abs(q + r))) > R)
|
||||
continue; // outside hex bounding circle — not rendered
|
||||
|
||||
double cx = q + r * 0.5;
|
||||
double cy = r * Math.Sqrt(3.0) / 2.0;
|
||||
if (!NPolyInside(cx, cy, N, R))
|
||||
{
|
||||
var tile = GetOrCreateTile(x, y);
|
||||
tile.IsAbsent = true;
|
||||
}
|
||||
}
|
||||
|
||||
// N vertex positions (scaled hex Cartesian)
|
||||
var verts = new (double vx, double vy)[N];
|
||||
for (int k = 0; k < N; k++)
|
||||
{
|
||||
double angle = Math.PI / 2.0 + 2.0 * Math.PI * k / N;
|
||||
verts[k] = (R * Math.Cos(angle), R * Math.Sin(angle));
|
||||
}
|
||||
|
||||
// For each player count 2..N, pick evenly-spaced corners.
|
||||
int half = Math.Max(1, R / 4);
|
||||
for (int playerCount = 2; playerCount <= N; playerCount++)
|
||||
{
|
||||
var seatMap = new Dictionary<int, HashSet<string>>();
|
||||
for (int seat = 0; seat < playerCount; seat++)
|
||||
{
|
||||
int ci = (int)Math.Round((double)seat * N / playerCount) % N;
|
||||
var (vx, vy) = verts[ci];
|
||||
var zone = new HashSet<string>();
|
||||
|
||||
// Corner tile
|
||||
var corner = FindNearestNgonTile(vx, vy, R);
|
||||
zone.Add(TileKey(corner.x, corner.y));
|
||||
|
||||
// Edge tiles toward adjacent vertices
|
||||
for (int step = 1; step <= half; step++)
|
||||
{
|
||||
var p = k.Split(',');
|
||||
return !IsHexInBounds(int.Parse(p[0]), int.Parse(p[1]), n);
|
||||
});
|
||||
double t = (double)step / (half + 1);
|
||||
int nextCi = (ci + 1) % N;
|
||||
int prevCi = (ci - 1 + N) % N;
|
||||
var n1 = FindNearestNgonTile(vx + (verts[nextCi].vx - vx) * t,
|
||||
vy + (verts[nextCi].vy - vy) * t, R);
|
||||
var n2 = FindNearestNgonTile(vx + (verts[prevCi].vx - vx) * t,
|
||||
vy + (verts[prevCi].vy - vy) * t, R);
|
||||
zone.Add(TileKey(n1.x, n1.y));
|
||||
zone.Add(TileKey(n2.x, n2.y));
|
||||
}
|
||||
|
||||
seatMap[seat] = zone;
|
||||
}
|
||||
_zones[playerCount] = seatMap;
|
||||
}
|
||||
}
|
||||
|
||||
private void OnGridTypeChanged()
|
||||
@@ -554,27 +625,71 @@ else
|
||||
_zones.Clear();
|
||||
if (_template.GridType == "Hexagon")
|
||||
{
|
||||
_template.HexSides = _pendingHexSides;
|
||||
int dim = 2 * _pendingHexSides - 1;
|
||||
_template.Width = dim;
|
||||
_template.Height = dim;
|
||||
_template.HexSides = _pendingHexRadius + 1;
|
||||
_template.HexPolygonSides = _pendingHexPolySides;
|
||||
int dim = 2 * _pendingHexRadius + 1;
|
||||
_template.Width = dim;
|
||||
_template.Height = dim;
|
||||
}
|
||||
else
|
||||
{
|
||||
_template.HexSides = 0;
|
||||
_template.Width = _pendingWidth;
|
||||
_template.Height = _pendingHeight;
|
||||
_template.HexSides = 0;
|
||||
_template.HexPolygonSides = 6;
|
||||
_template.Width = _pendingWidth;
|
||||
_template.Height = _pendingHeight;
|
||||
}
|
||||
}
|
||||
|
||||
// ── Hex geometry helpers ──────────────────────────────────────────────────
|
||||
|
||||
private static bool IsHexInBounds(int x, int y, int N)
|
||||
/// <summary>
|
||||
/// True when offset coordinate (x,y) is within the hex grid of radius R=sides−1.
|
||||
/// Used for the SVG canvas to skip cells outside the bounding circle.
|
||||
/// </summary>
|
||||
private static bool IsHexInBounds(int x, int y, int sides)
|
||||
{
|
||||
int q = x - (N - 1);
|
||||
int r = y - (N - 1);
|
||||
int s = -q - r;
|
||||
return Math.Max(Math.Abs(q), Math.Max(Math.Abs(r), Math.Abs(s))) <= N - 1;
|
||||
int R = sides - 1;
|
||||
int q = x - R, r = y - R;
|
||||
return Math.Max(Math.Abs(q), Math.Max(Math.Abs(r), Math.Abs(q + r))) <= R;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// True when scaled hex-Cartesian point (cx,cy) is inside a regular N-gon with
|
||||
/// circumradius R. Uses cross-product test on each CCW edge (slightly expanded to
|
||||
/// include boundary tiles).
|
||||
/// </summary>
|
||||
private static bool NPolyInside(double cx, double cy, int N, double R)
|
||||
{
|
||||
double Rexp = R * 1.02; // slight expansion to include perimeter tiles
|
||||
for (int k = 0; k < N; k++)
|
||||
{
|
||||
double a1 = Math.PI / 2.0 + 2.0 * Math.PI * k / N;
|
||||
double a2 = Math.PI / 2.0 + 2.0 * Math.PI * (k + 1) / N;
|
||||
double v1x = Rexp * Math.Cos(a1), v1y = Rexp * Math.Sin(a1);
|
||||
double v2x = Rexp * Math.Cos(a2), v2y = Rexp * Math.Sin(a2);
|
||||
double cross = (v2x - v1x) * (cy - v1y) - (v2y - v1y) * (cx - v1x);
|
||||
if (cross < -1e-9) return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/// <summary>Find the nearest non-absent hex tile (in offset coords) to scaled-Cartesian (vx,vy).</summary>
|
||||
private (int x, int y) FindNearestNgonTile(double vx, double vy, int R)
|
||||
{
|
||||
(int x, int y) best = (R, R);
|
||||
double bestDist = double.MaxValue;
|
||||
for (int q = -R; q <= R; q++)
|
||||
for (int r = -R; r <= R; r++)
|
||||
{
|
||||
if (Math.Abs(q + r) > R) continue;
|
||||
int ox = q + R, oy = r + R;
|
||||
if (_tiles.TryGetValue(TileKey(ox, oy), out var td) && td.IsAbsent) continue;
|
||||
double tcx = q + r * 0.5;
|
||||
double tcy = r * Math.Sqrt(3.0) / 2.0;
|
||||
double d = (tcx - vx) * (tcx - vx) + (tcy - vy) * (tcy - vy);
|
||||
if (d < bestDist) { bestDist = d; best = (ox, oy); }
|
||||
}
|
||||
return best;
|
||||
}
|
||||
|
||||
private static double HexCellX(int x, int y, int N, double R, double pad)
|
||||
|
||||
Reference in New Issue
Block a user