13internal readonly record
struct RenderTileCandidates(
14 List<SceneLayerTile> Tiles,
15 bool IsRenderOrdered);
21internal static class RenderTileQuery
23 internal static RenderTileCandidates GetCandidates(
24 ISceneLayerCoordinates coordinates,
26 Rectangle worldPixelRange,
29 var tiles =
new List<SceneLayerTile>();
42 return new(tiles, IsRenderOrdered(layer));
55 internal static bool VisitCandidates(
56 ISceneLayerCoordinates coordinates,
58 Rectangle worldPixelRange,
60 Func<SceneLayerTile, bool> visitor)
62 ArgumentNullException.ThrowIfNull(visitor);
64 return coordinates
switch
66 OrthogonalCoordinates =>
73 IsometricRhombicCoordinates =>
74 VisitIsometricRhombic(
80 IsometricAxialCoordinates =>
87 HexAxialFlatTopCoordinates =>
101 ObliqueRightCoordinates or ObliqueLeftCoordinates =>
108 _ => VisitExactFallback(
117 internal static bool IsRenderOrdered(SceneLayer layer) =>
118 layer.TransformedTiles.Count == 0 ||
121 internal static Rectangle GetFixedTileCellBounds(
129 if (layer.CoordinateSystem is OrthogonalCoordinates)
138 Point anchor = layer.CoordinateSystem.GetAnchorPixelAtSceneLayerCoordinates(
142 if (layer.CoordinateSystem is IsometricRhombicCoordinates or IsometricAxialCoordinates)
144 anchor.X - width / 2,
156 private static bool VisitOrthogonal(
157 ISceneLayerCoordinates coordinates,
160 bool includeOverhang,
161 Func<SceneLayerTile, bool> visitor)
163 var candidate = TileBounds.GetTransformedTileCandidateRange(layer, query, includeOverhang);
165 var ul = coordinates.GetSceneLayerCoordinatesAtPixel(
167 new PointF(candidate.Left, candidate.Top));
169 var br = coordinates.GetSceneLayerCoordinatesAtPixel(
171 new PointF(candidate.Right - 1, candidate.Bottom - 1));
173 int minX = Math.Max(0, (
int)Math.Floor(ul.X) - 1);
174 int maxX = Math.Min(layer.
GridColumnCount - 1, (
int)Math.Ceiling(br.X) + 1);
175 int minY = Math.Max(0, (
int)Math.Floor(ul.Y) - 1);
176 int maxY = Math.Min(layer.
GridRowCount - 1, (
int)Math.Ceiling(br.Y) + 1);
178 return VisitRowMajor(
187 private static bool VisitIsometricRhombic(
188 ISceneLayerCoordinates coordinates,
191 bool includeOverhang,
192 Func<SceneLayerTile, bool> visitor)
213 for (
int depth = minX + minY; depth <= maxX + maxY; depth++)
215 int xStart = Math.Max(minX, depth - maxY);
216 int xEnd = Math.Min(maxX, depth - minY);
218 for (
int x = xStart; x <= xEnd; x++)
222 if (layer[x, y] is { } tile && !visitor(tile))
230 private static bool VisitIsometricAxial(
231 ISceneLayerCoordinates coordinates,
234 bool includeOverhang,
235 Func<SceneLayerTile, bool> visitor)
248 return VisitRowMajorClamped(
257 private static bool VisitHexPointed(
258 ISceneLayerCoordinates coordinates,
261 bool includeOverhang,
262 Func<SceneLayerTile, bool> visitor)
264 var candidate = TileBounds.GetTransformedTileCandidateRange(layer, query, includeOverhang);
268 int minRow = Math.Max(
270 (
int)Math.Floor((candidate.Top + layer.
OriginPx.Y) / (height * 0.75f)) - 2);
272 int maxRow = Math.Min(
274 (
int)Math.Ceiling((candidate.Bottom + layer.
OriginPx.Y) / (height * 0.75f)) + 2);
279 for (
int row = minRow; row <= maxRow; row++)
281 int xOffset = (row & 1) == 0 ? 0 : width / 2;
283 int minCol = Math.Max(
286 (candidate.Left + layer.
OriginPx.X - xOffset) / (
float)width) - 2);
288 int maxCol = Math.Min(
291 (candidate.Right + layer.
OriginPx.X - xOffset) / (
float)width) + 2);
293 for (
int col = minCol; col <= maxCol; col++)
295 if (layer[col, row] is { } tile && !visitor(tile))
303 private static bool VisitHexFlat(
304 ISceneLayerCoordinates coordinates,
307 bool includeOverhang,
308 Func<SceneLayerTile, bool> visitor)
310 var candidate = TileBounds.GetTransformedTileCandidateRange(layer, query, includeOverhang);
313 int halfHeight = height / 2;
315 int minCol = Math.Max(
317 (
int)Math.Floor((candidate.Left + layer.
OriginPx.X) / (width * 0.75f)) - 2);
319 int maxCol = Math.Min(
321 (
int)Math.Ceiling((candidate.Right + layer.
OriginPx.X) / (width * 0.75f)) + 2);
327 (int)Math.Floor((candidate.Top + layer.
OriginPx.Y) / (
float)height) - 2;
330 (int)Math.Ceiling((candidate.Bottom + layer.
OriginPx.Y) / (
float)height) + 2;
334 (candidate.Top + layer.
OriginPx.Y - halfHeight) / (
float)height) - 2;
338 (candidate.Bottom + layer.
OriginPx.Y - halfHeight) / (
float)height) + 2;
340 int minRow = Math.Max(0, Math.Min(minEven, minOdd));
341 int maxRow = Math.Min(layer.
GridRowCount - 1, Math.Max(maxEven, maxOdd));
346 for (
int depth = minRow * 2; depth <= maxRow * 2 + 1; depth++)
348 for (
int col = minCol; col <= maxCol; col++)
350 int numerator = depth - (col & 1);
352 if ((numerator & 1) != 0)
355 int row = numerator / 2;
357 if (row < minRow || row > maxRow)
360 if (layer[col, row] is { } tile && !visitor(tile))
368 private static bool VisitOblique(
369 ISceneLayerCoordinates coordinates,
372 bool includeOverhang,
373 Func<SceneLayerTile, bool> visitor)
386 return VisitRowMajorClamped(
395 private static bool VisitExactFallback(
396 ISceneLayerCoordinates coordinates,
399 bool includeOverhang,
400 Func<SceneLayerTile, bool> visitor)
402 var exact = coordinates.GetSceneLayerTilesInPixelRange(
407 for (
int i = 0; i < exact.Count; i++)
409 if (!visitor(exact[i]))
416 private static void GetCornerBounds(
417 ISceneLayerCoordinates coordinates,
420 bool includeOverhang,
427 var candidate = TileBounds.GetTransformedTileCandidateRange(layer, query, includeOverhang);
429 var ul = coordinates.GetSceneLayerCoordinatesAtPixel(
431 new PointF(candidate.Left, candidate.Top));
433 var ur = coordinates.GetSceneLayerCoordinatesAtPixel(
435 new PointF(candidate.Right, candidate.Top));
437 var ll = coordinates.GetSceneLayerCoordinatesAtPixel(
439 new PointF(candidate.Left, candidate.Bottom));
441 var lr = coordinates.GetSceneLayerCoordinatesAtPixel(
443 new PointF(candidate.Right, candidate.Bottom));
446 (int)Math.Floor(Math.Min(Math.Min(ul.X, ur.X), Math.Min(ll.X, lr.X))) -
450 (int)Math.Ceiling(Math.Max(Math.Max(ul.X, ur.X), Math.Max(ll.X, lr.X))) +
454 (int)Math.Floor(Math.Min(Math.Min(ul.Y, ur.Y), Math.Min(ll.Y, lr.Y))) -
458 (int)Math.Ceiling(Math.Max(Math.Max(ul.Y, ur.Y), Math.Max(ll.Y, lr.Y))) +
462 private static bool VisitRowMajorClamped(
468 Func<SceneLayerTile, bool> visitor)
478 return VisitRowMajor(
487 private static bool VisitRowMajor(
493 Func<SceneLayerTile, bool> visitor)
495 if (minX > maxX || minY > maxY)
498 for (
int y = minY; y <= maxY; y++)
500 for (
int x = minX; x <= maxX; x++)
502 if (layer[x, y] is { } tile && !visitor(tile))
510 private static bool Clamp(
517 minX = Math.Max(0, minX);
519 minY = Math.Max(0, minY);
521 return minX <= maxX && minY <= maxY;
Point GridCoordinatesAbs
Gets the absolute grid coordinates of this tile within its parent scene layer as integer values.
Point OriginPx
Gets or sets the world-space pixel origin of this layer's (0,0) tile position.
int GridRowCount
Gets the number of rows (tiles high) in this layer's grid.
int TileHeight
Gets or sets the height of each tile in this layer, measured in pixels.
int GridColumnCount
Gets the number of columns (tiles wide) in this layer's grid.
int TileWidth
Gets or sets the width of each tile in this layer, measured in pixels.
@ HexAxialPointedTop
Hexagonal grid using axial coordinates with pointy-topped hexes.
@ Rectangle
Particles are spawned uniformly within a rectangular area. This is the default distribution.