10internal sealed class HexAxialPointedTop : ISceneLayerCoordinates
18 public Point GetAnchorPixelAtSceneLayerCoordinates(SceneLayer sceneLayer, PointF gp)
23 float offsetX = Stagger(gp.Y, width / 2);
26 float x = -origin.X + (gp.X * width) + offsetX;
27 float y = -origin.Y + (gp.Y * height * 0.75f);
34 public PointF GetContinuousGridAtPixel(SceneLayer sceneLayer, PointF pixel)
37 float row = (pixel.Y + origin.Y) / (sceneLayer.
TileHeight * 0.75f);
38 float other = (pixel.X + origin.X - Stagger(row, sceneLayer.
TileWidth / 2)) / sceneLayer.
TileWidth;
39 return new PointF(other, row);
44 private static float Stagger(
float coordinate,
int halfTile)
46 int whole = (int)MathF.Floor(coordinate);
47 float fraction = coordinate - whole;
48 return halfTile * ((whole & 1) == 0 ? fraction : 1f - fraction);
58 public PointF GetSceneLayerCoordinatesAtPixel(SceneLayer sceneLayer, PointF pixelPt)
66 float fy = (pixelPt.Y + originY) / (H * 0.75f);
67 int approxRow = (int)Math.Round(fy);
69 int baseX = -originX + ((approxRow & 1) == 0 ? 0 :
W / 2);
70 float fx = (pixelPt.X - baseX) / (
float)
W;
71 int approxCol = (int)Math.Round(fx);
73 var best =
new Point(approxCol, approxRow);
74 float bestDist =
float.MaxValue;
76 foreach (var cand
in NeighborsPointedTop(approxCol, approxRow, includeSelf:
true))
78 var poly = HexPolygonPointedTop(
82 includeOverhang:
false);
85 var pInt =
new Point((
int)Math.Round(pixelPt.X), (
int)Math.Round(pixelPt.Y));
87 if (PointInPolygon(poly, pInt))
88 return new PointF(cand.X, cand.Y);
90 float cx = -sceneLayer.OriginPx.X + cand.X * (float)
W + ((cand.Y & 1) == 0 ? 0 :
W / 2f) +
W / 2f;
91 float cy = -sceneLayer.OriginPx.Y + cand.Y * (H * 0.75f) + H / 2f;
92 float d = (cx - pixelPt.X) * (cx - pixelPt.X) + (cy - pixelPt.Y) * (cy - pixelPt.Y);
101 return new PointF(best.X, best.Y);
111 public List<SceneLayerTile> GetSceneLayerTilesInPixelRange(
112 SceneLayer sceneLayer,
113 Rectangle worldPixelRange,
114 bool includeOverhang)
116 var result =
new List<SceneLayerTile>();
119 var candidateRange = TileBounds.GetTransformedTileCandidateRange(sceneLayer, worldPixelRange, includeOverhang);
121 int minRow = (int)Math.Floor((candidateRange.Top + sceneLayer.
OriginPx.Y) / (H * 0.75f)) - 2;
122 int maxRow = (int)Math.Ceiling((candidateRange.Bottom + sceneLayer.
OriginPx.Y) / (H * 0.75f)) + 2;
124 for (
int row = minRow; row <= maxRow; row++)
126 int xOffset = ((row & 1) == 0 ? 0 :
W / 2);
127 int minCol = (int)Math.Floor((candidateRange.Left + sceneLayer.
OriginPx.X - xOffset) / (
float)
W) - 2;
128 int maxCol = (int)Math.Ceiling((candidateRange.Right + sceneLayer.
OriginPx.X - xOffset) / (
float)
W) + 2;
130 for (
int col = minCol; col <= maxCol; col++)
132 var gp = sceneLayer[col, row];
137 var r = GetPixelRangeForTile(gp, includeOverhang);
139 if (r.IntersectsWith(worldPixelRange))
153 public Rectangle GetPixelRangeForTile(Tile tile,
bool includeOverhang)
155 var p = GetAnchorPixelAtSceneLayerCoordinates(tile.SceneLayer, tile.SceneLayerCoordinates);
156 int W = tile.SceneLayer.TileWidth;
157 int H = tile.SceneLayer.TileHeight;
165 return TileBounds.ApplyTileGeometry(rect, tile, includeOverhang);
174 public Rectangle GetPixelRangeForTileList(List<Tile> tileList,
bool includeOverhang)
178 foreach (var t
in tileList)
180 var r = GetPixelRangeForTile(t, includeOverhang);
181 ret = ret.IsEmpty ? r :
Rectangle.Union(ret, r);
194 public SceneLayerTile GetAdjacentSceneLayerTile(SceneLayerTile gp,
CardinalDirections dir)
198 bool even = (y & 1) == 0;
203 CardinalDirections.N => m.ResolveWrappedTile(x, y - 1),
204 CardinalDirections.S => m.ResolveWrappedTile(x, y + 1),
205 CardinalDirections.NE => m.ResolveWrappedTile(x + (even ? 0 : 1), y - 1),
206 CardinalDirections.NW => m.ResolveWrappedTile(x - (even ? 1 : 0), y - 1),
207 CardinalDirections.SE => m.ResolveWrappedTile(x + (even ? 0 : 1), y + 1),
208 CardinalDirections.SW => m.ResolveWrappedTile(x - (even ? 1 : 0), y + 1),
209 CardinalDirections.E =>
null,
210 CardinalDirections.W =>
null,
221 public Point[] GetPolygonPts(Tile tile,
bool includeOverhang)
223 return HexPolygonPointedTop(
225 (
int)tile.SceneLayerCoordinates.X,
226 (
int)tile.SceneLayerCoordinates.Y,
237 public PointF FindEquivalentSceneLayerCoordinates(PointF valColRow,
int xUpperBound,
int yUpperBound)
239 float modX = valColRow.X % (xUpperBound + 1);
240 float modY = valColRow.Y % (yUpperBound + 1);
243 modX += xUpperBound + 1;
246 modY += yUpperBound + 1;
248 return new PointF(modX, modY);
252 private static IEnumerable<Point> NeighborsPointedTop(
int col,
int row,
bool includeSelf)
255 yield
return new Point(col, row);
257 bool even = (row & 1) == 0;
258 yield
return new Point(col, row - 1);
259 yield
return new Point(col, row + 1);
260 yield
return new Point(col + (even ? 0 : 1), row - 1);
261 yield
return new Point(col - (even ? 1 : 0), row - 1);
262 yield
return new Point(col + (even ? 0 : 1), row + 1);
263 yield
return new Point(col - (even ? 1 : 0), row + 1);
266 private static Point[] HexPolygonPointedTop(
267 SceneLayer sceneLayer,
270 bool includeOverhang)
275 int originX = sceneLayer.
OriginPx.X;
276 int originY = sceneLayer.
OriginPx.Y;
279 -originX + col *
W + ((row & 1) == 0 ? 0 :
W / 2),
280 -originY + (
int)Math.Floor(row * (H * 0.75f)));
288 var ohRect = TileBounds.ApplyOverhang(
305 new Point(x +
W / 2, y),
306 new Point(x +
W, y + H / 4),
307 new Point(x +
W, y + 3 * H / 4),
308 new Point(x +
W / 2, y + H),
309 new Point(x, y + 3 * H / 4),
310 new Point(x, y + H / 4)
314 private static bool PointInPolygon(Point[] poly, Point p)
317 int j = poly.Length - 1;
319 for (
int i = 0; i < poly.Length; i++)
321 bool intersect = ((poly[i].Y > p.Y) != (poly[j].Y > p.Y)) &&
322 (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.