10internal sealed class HexAxialFlatTopCoordinates : ISceneLayerCoordinates
18 public Point GetAnchorPixelAtSceneLayerCoordinates(SceneLayer sceneLayer, PointF gp)
23 float offsetY = Stagger(gp.X, height / 2);
25 float x = -origin.X + (gp.X * width * 0.75f);
26 float y = -origin.Y + (gp.Y * height) + offsetY;
28 return new Point((
int)MathF.Floor(x), (
int)MathF.Floor(y));
31 public PointF GetContinuousGridAtPixel(SceneLayer sceneLayer, PointF pixel)
34 float column = (pixel.X + origin.X) / (sceneLayer.
TileWidth * 0.75f);
35 float other = (pixel.Y + origin.Y - Stagger(column, sceneLayer.
TileHeight / 2)) / sceneLayer.
TileHeight;
36 return new PointF(column, other);
41 private static float Stagger(
float coordinate,
int halfTile)
43 int whole = (int)MathF.Floor(coordinate);
44 float fraction = coordinate - whole;
45 return halfTile * ((whole & 1) == 0 ? fraction : 1f - fraction);
55 public PointF GetSceneLayerCoordinatesAtPixel(SceneLayer sceneLayer, PointF pixelPt)
60 float originX = sceneLayer.
OriginPx.X;
61 float originY = sceneLayer.
OriginPx.Y;
66 float dxFromCol0Center = pixelPt.X - (-originX +
W * 0.5f);
69 float colF = dxFromCol0Center / (
W * 0.75f);
70 int approxCol = (int)Math.Round(colF);
79 + ((approxCol & 1) == 0 ? 0f : H * 0.5f);
81 float dyFromRow0Center = pixelPt.Y - baseCenterY;
82 float rowF = dyFromRow0Center / H;
83 int approxRow = (int)Math.Round(rowF);
85 var best =
new Point(approxCol, approxRow);
86 float bestDist =
float.MaxValue;
90 foreach (var cand
in NeighborsFlatTop(approxCol, approxRow, includeSelf:
true))
92 var poly = HexPolygonFlatTop(
96 includeOverhang:
false);
98 var pInt =
new Point((
int)Math.Round(pixelPt.X), (
int)Math.Round(pixelPt.Y));
100 if (PointInPolygon(poly, pInt))
101 return new PointF(cand.X, cand.Y);
104 float cx = -originX + cand.X * (
W * 0.75f) +
W / 2f;
106 float cy = -originY + cand.Y * (float)H
107 + ((cand.X & 1) == 0 ? 0 : H / 2) + H / 2f;
109 float dx = cx - pixelPt.X;
110 float dy = cy - pixelPt.Y;
111 float d = dx * dx + dy * dy;
120 return new PointF(best.X, best.Y);
130 public List<SceneLayerTile> GetSceneLayerTilesInPixelRange(
131 SceneLayer sceneLayer,
132 Rectangle worldPixelRange,
133 bool includeOverhang)
135 var result =
new List<SceneLayerTile>();
139 int originX = sceneLayer.
OriginPx.X;
140 int originY = sceneLayer.
OriginPx.Y;
141 var candidateRange = TileBounds.GetTransformedTileCandidateRange(sceneLayer, worldPixelRange, includeOverhang);
143 int minCol = (int)Math.Floor((candidateRange.Left + originX) / (
W * 0.75f)) - 2;
144 int maxCol = (int)Math.Ceiling((candidateRange.Right + originX) / (
W * 0.75f)) + 2;
146 for (
int col = minCol; col <= maxCol; col++)
148 int yOffset = ((col & 1) == 0 ? 0 : H / 2);
149 int minRow = (int)Math.Floor((candidateRange.Top + originY - yOffset) / (
float)H) - 2;
150 int maxRow = (int)Math.Ceiling((candidateRange.Bottom + originY - yOffset) / (
float)H) + 2;
152 for (
int row = minRow; row <= maxRow; row++)
154 var gp = sceneLayer[col, row];
159 var r = GetPixelRangeForTile(gp, includeOverhang);
161 if (r.IntersectsWith(worldPixelRange))
175 public Rectangle GetPixelRangeForTile(Tile tile,
bool includeOverhang)
177 var p = GetAnchorPixelAtSceneLayerCoordinates(tile.SceneLayer, tile.SceneLayerCoordinates);
178 int W = tile.SceneLayer.TileWidth;
179 int H = tile.SceneLayer.TileHeight;
187 return TileBounds.ApplyTileGeometry(rect, tile, includeOverhang);
196 public Rectangle GetPixelRangeForTileList(List<Tile> tileList,
bool includeOverhang)
200 foreach (var t
in tileList)
202 var r = GetPixelRangeForTile(t, includeOverhang);
203 ret = ret.IsEmpty ? r :
Rectangle.Union(ret, r);
216 public SceneLayerTile GetAdjacentSceneLayerTile(SceneLayerTile gp,
CardinalDirections dir)
220 bool even = (x & 1) == 0;
225 CardinalDirections.E => m.ResolveWrappedTile(x + 1, y),
226 CardinalDirections.W => m.ResolveWrappedTile(x - 1, y),
227 CardinalDirections.NE => m.ResolveWrappedTile(x + 1, y - (even ? 1 : 0)),
228 CardinalDirections.SE => m.ResolveWrappedTile(x + 1, y + (even ? 0 : 1)),
229 CardinalDirections.NW => m.ResolveWrappedTile(x - 1, y - (even ? 1 : 0)),
230 CardinalDirections.SW => m.ResolveWrappedTile(x - 1, y + (even ? 0 : 1)),
231 CardinalDirections.N =>
null,
232 CardinalDirections.S =>
null,
243 public Point[] GetPolygonPts(Tile tile,
bool includeOverhang)
245 return HexPolygonFlatTop(
247 (
int)tile.SceneLayerCoordinates.X,
248 (
int)tile.SceneLayerCoordinates.Y,
259 public PointF FindEquivalentSceneLayerCoordinates(PointF valColRow,
int xUpperBound,
int yUpperBound)
262 float modX = valColRow.X % (xUpperBound + 1);
263 float modY = valColRow.Y % (yUpperBound + 1);
266 modX += xUpperBound + 1;
269 modY += yUpperBound + 1;
271 return new PointF(modX, modY);
276 private static IEnumerable<Point> NeighborsFlatTop(
int col,
int row,
bool includeSelf)
279 yield
return new Point(col, row);
281 bool even = (col & 1) == 0;
282 yield
return new Point(col + 1, row);
283 yield
return new Point(col - 1, row);
284 yield
return new Point(col + 1, row + (even ? 0 : 1));
285 yield
return new Point(col + 1, row - (even ? 1 : 0));
286 yield
return new Point(col - 1, row + (even ? 0 : 1));
287 yield
return new Point(col - 1, row - (even ? 1 : 0));
290 private static Point[] HexPolygonFlatTop(
291 SceneLayer sceneLayer,
294 bool includeOverhang)
299 int originX = sceneLayer.
OriginPx.X;
300 int originY = sceneLayer.
OriginPx.Y;
303 -originX + (
int)Math.Floor(col * (
W * 0.75f)),
304 -originY + row * H + ((col & 1) == 0 ? 0 : H / 2));
312 var ohRect = TileBounds.ApplyOverhang(
329 new Point(x +
W / 4, y),
330 new Point(x + 3 *
W / 4, y),
331 new Point(x +
W, y + H / 2),
332 new Point(x + 3 *
W / 4, y + H),
333 new Point(x +
W / 4, y + H),
334 new Point(x, y + H / 2)
338 private static bool PointInPolygon(Point[] poly, Point p)
341 int j = poly.Length - 1;
343 for (
int i = 0; i < poly.Length; i++)
345 bool intersect = ((poly[i].Y > p.Y) != (poly[j].Y > p.Y)) &&
346 (p.X < (poly[j].X - poly[i].X) * (p.Y - poly[i].Y) / (
float)(poly[j].Y - poly[i].Y) +
Point GridCoordinatesAbs
Gets the absolute grid coordinates of this tile within its parent scene layer as integer values.
override SceneLayer SceneLayer
Gets the parent scene layer that contains this tile.
Point OriginPx
Gets or sets the world-space pixel origin of this layer's (0,0) tile position.
int TileHeight
Gets or sets the height of each tile in this layer, measured in pixels.
int TileWidth
Gets or sets the width of each tile in this layer, measured in pixels.
CardinalDirections
Represents the eight cardinal and intercardinal directions of the compass.
@ Rectangle
Particles are spawned uniformly within a rectangular area. This is the default distribution.
record struct Spacing([property:JsonProperty("left")] int Left, [property:JsonProperty("top")] int Top, [property:JsonProperty("right")] int Right, [property:JsonProperty("bottom")] int Bottom)
Represents dimensions in pixels when spacing Tilesheets.