Gondwana Game Engine Development (master) - 2.7.0
Gondwana is a cross-platform 2.5D game and rendering engine written in C#/.NET 8. It provides fine-grained control over rendering, timing, and scene composition, with built-in support for parallax, z-ordering, pixel overhang, collision detection, and particle effects. Gondwana targets desktop and web platforms using SkiaSharp for graphics and pluggable audio backends for desktop and browser targets.
Loading...
Searching...
No Matches
HexAxialFlatTopCoordinates.cs
1using System.Drawing;
3
5
10internal sealed class HexAxialFlatTopCoordinates : ISceneLayerCoordinates
11{
18 public Point GetAnchorPixelAtSceneLayerCoordinates(SceneLayer sceneLayer, PointF gp)
19 {
20 int width = sceneLayer.TileWidth;
21 int height = sceneLayer.TileHeight;
22
23 float offsetY = Stagger(gp.X, height / 2);
24 var origin = sceneLayer.OriginPx;
25 float x = -origin.X + (gp.X * width * 0.75f);
26 float y = -origin.Y + (gp.Y * height) + offsetY;
27
28 return new Point((int)MathF.Floor(x), (int)MathF.Floor(y));
29 }
30
31 public PointF GetContinuousGridAtPixel(SceneLayer sceneLayer, PointF pixel)
32 {
33 var origin = sceneLayer.OriginPx;
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);
37 }
38
39 // Between adjacent rows or columns, interpolate the same even-offset
40 // stagger for both the drawable anchor and its continuous inverse.
41 private static float Stagger(float coordinate, int halfTile)
42 {
43 int whole = (int)MathF.Floor(coordinate);
44 float fraction = coordinate - whole;
45 return halfTile * ((whole & 1) == 0 ? fraction : 1f - fraction);
46 }
47
55 public PointF GetSceneLayerCoordinatesAtPixel(SceneLayer sceneLayer, PointF pixelPt)
56 {
57 int W = sceneLayer.TileWidth;
58 int H = sceneLayer.TileHeight;
59
60 float originX = sceneLayer.OriginPx.X;
61 float originY = sceneLayer.OriginPx.Y;
62
63 // --- 1) Coarse column estimate using centers, not box top-left ---
64
65 // Center of column 0 is at originX + W/2
66 float dxFromCol0Center = pixelPt.X - (-originX + W * 0.5f);
67
68 // Columns advance by 0.75 * W horizontally
69 float colF = dxFromCol0Center / (W * 0.75f);
70 int approxCol = (int)Math.Round(colF);
71
72 // --- 2) Coarse row estimate using centers and parity ---
73
74 // Center Y of row 0 in this column:
75 // even col: originY + H/2
76 // odd col: originY + H/2 + H/2 = originY + H
77 float baseCenterY =
78 -originY + H * 0.5f
79 + ((approxCol & 1) == 0 ? 0f : H * 0.5f);
80
81 float dyFromRow0Center = pixelPt.Y - baseCenterY;
82 float rowF = dyFromRow0Center / H;
83 int approxRow = (int)Math.Round(rowF);
84
85 var best = new Point(approxCol, approxRow);
86 float bestDist = float.MaxValue;
87
88 // --- 3) Refine via polygon hit-test + nearest center ---
89
90 foreach (var cand in NeighborsFlatTop(approxCol, approxRow, includeSelf: true))
91 {
92 var poly = HexPolygonFlatTop(
93 sceneLayer,
94 cand.X,
95 cand.Y,
96 includeOverhang: false);
97
98 var pInt = new Point((int)Math.Round(pixelPt.X), (int)Math.Round(pixelPt.Y));
99
100 if (PointInPolygon(poly, pInt))
101 return new PointF(cand.X, cand.Y);
102
103 // Center of candidate hex
104 float cx = -originX + cand.X * (W * 0.75f) + W / 2f;
105
106 float cy = -originY + cand.Y * (float)H
107 + ((cand.X & 1) == 0 ? 0 : H / 2) + H / 2f;
108
109 float dx = cx - pixelPt.X;
110 float dy = cy - pixelPt.Y;
111 float d = dx * dx + dy * dy;
112
113 if (d < bestDist)
114 {
115 bestDist = d;
116 best = cand;
117 }
118 }
119
120 return new PointF(best.X, best.Y);
121 }
122
130 public List<SceneLayerTile> GetSceneLayerTilesInPixelRange(
131 SceneLayer sceneLayer,
132 Rectangle worldPixelRange,
133 bool includeOverhang)
134 {
135 var result = new List<SceneLayerTile>();
136 int W = sceneLayer.TileWidth;
137 int H = sceneLayer.TileHeight;
138
139 int originX = sceneLayer.OriginPx.X;
140 int originY = sceneLayer.OriginPx.Y;
141 var candidateRange = TileBounds.GetTransformedTileCandidateRange(sceneLayer, worldPixelRange, includeOverhang);
142
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;
145
146 for (int col = minCol; col <= maxCol; col++)
147 {
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;
151
152 for (int row = minRow; row <= maxRow; row++)
153 {
154 var gp = sceneLayer[col, row];
155
156 if (gp == null)
157 continue;
158
159 var r = GetPixelRangeForTile(gp, includeOverhang);
160
161 if (r.IntersectsWith(worldPixelRange))
162 result.Add(gp);
163 }
164 }
165
166 return result;
167 }
168
175 public Rectangle GetPixelRangeForTile(Tile tile, bool includeOverhang)
176 {
177 var p = GetAnchorPixelAtSceneLayerCoordinates(tile.SceneLayer, tile.SceneLayerCoordinates);
178 int W = tile.SceneLayer.TileWidth;
179 int H = tile.SceneLayer.TileHeight;
180
181 var rect = new Rectangle(
182 p.X,
183 p.Y,
184 W,
185 H); // hex image fits W x H box
186
187 return TileBounds.ApplyTileGeometry(rect, tile, includeOverhang);
188 }
189
196 public Rectangle GetPixelRangeForTileList(List<Tile> tileList, bool includeOverhang)
197 {
198 Rectangle ret = Rectangle.Empty;
199
200 foreach (var t in tileList)
201 {
202 var r = GetPixelRangeForTile(t, includeOverhang);
203 ret = ret.IsEmpty ? r : Rectangle.Union(ret, r);
204 }
205
206 return ret;
207 }
208
216 public SceneLayerTile GetAdjacentSceneLayerTile(SceneLayerTile gp, CardinalDirections dir)
217 {
218 int x = gp.GridCoordinatesAbs.X;
219 int y = gp.GridCoordinatesAbs.Y;
220 bool even = (x & 1) == 0;
221 var m = gp.SceneLayer;
222
223 return dir switch
224 {
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, // not a direct neighbor in hex grid
232 CardinalDirections.S => null,
233 _ => null
234 };
235 }
236
243 public Point[] GetPolygonPts(Tile tile, bool includeOverhang)
244 {
245 return HexPolygonFlatTop(
246 tile.SceneLayer,
247 (int)tile.SceneLayerCoordinates.X,
248 (int)tile.SceneLayerCoordinates.Y,
249 includeOverhang);
250 }
251
259 public PointF FindEquivalentSceneLayerCoordinates(PointF valColRow, int xUpperBound, int yUpperBound)
260 {
261 // wrap like square; hex maps are usually finite with wrap disabled, but keep parity sane
262 float modX = valColRow.X % (xUpperBound + 1);
263 float modY = valColRow.Y % (yUpperBound + 1);
264
265 if (modX < 0)
266 modX += xUpperBound + 1;
267
268 if (modY < 0)
269 modY += yUpperBound + 1;
270
271 return new PointF(modX, modY);
272 }
273
274 #region helpers
275
276 private static IEnumerable<Point> NeighborsFlatTop(int col, int row, bool includeSelf)
277 {
278 if (includeSelf)
279 yield return new Point(col, row);
280
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));
288 }
289
290 private static Point[] HexPolygonFlatTop(
291 SceneLayer sceneLayer,
292 int col,
293 int row,
294 bool includeOverhang)
295 {
296 int W = sceneLayer.TileWidth;
297 int H = sceneLayer.TileHeight;
298
299 int originX = sceneLayer.OriginPx.X;
300 int originY = sceneLayer.OriginPx.Y;
301
302 var p = new Point(
303 -originX + (int)Math.Floor(col * (W * 0.75f)),
304 -originY + row * H + ((col & 1) == 0 ? 0 : H / 2));
305
306 var rect = new Rectangle(
307 p.X,
308 p.Y,
309 W,
310 H);
311
312 var ohRect = TileBounds.ApplyOverhang(
313 rect,
314 includeOverhang
315 ? new Spacing(
316 0,
317 0,
318 0,
319 0)
320 : Spacing.None,
321 includeOverhang); // polygon uses base box; overhang impacts range, not shape
322
323 int x = ohRect.X;
324 int y = ohRect.Y;
325
326 // Flat-top vertices from bounding box
327 return new[]
328 {
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)
335 };
336 }
337
338 private static bool PointInPolygon(Point[] poly, Point p)
339 {
340 bool inside = false;
341 int j = poly.Length - 1;
342
343 for (int i = 0; i < poly.Length; i++)
344 {
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) +
347 poly[i].X);
348
349 if (intersect)
350 inside = !inside;
351
352 j = i;
353 }
354
355 return inside;
356 }
357
358 #endregion helpers
359}
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.
Definition Spacing.cs:12