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
HexAxialPointedTop.cs
1using System.Drawing;
3
5
10internal sealed class HexAxialPointedTop : ISceneLayerCoordinates
11{
18 public Point GetAnchorPixelAtSceneLayerCoordinates(SceneLayer sceneLayer, PointF gp)
19 {
20 int width = sceneLayer.TileWidth;
21 int height = sceneLayer.TileHeight;
22
23 float offsetX = Stagger(gp.Y, width / 2);
24 var origin = sceneLayer.OriginPx;
25
26 float x = -origin.X + (gp.X * width) + offsetX;
27 float y = -origin.Y + (gp.Y * height * 0.75f);
28
29 return new Point(
30 (int)MathF.Floor(x),
31 (int)MathF.Floor(y));
32 }
33
34 public PointF GetContinuousGridAtPixel(SceneLayer sceneLayer, PointF pixel)
35 {
36 var origin = sceneLayer.OriginPx;
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);
40 }
41
42 // Between adjacent rows or columns, interpolate the same even-offset
43 // stagger for both the drawable anchor and its continuous inverse.
44 private static float Stagger(float coordinate, int halfTile)
45 {
46 int whole = (int)MathF.Floor(coordinate);
47 float fraction = coordinate - whole;
48 return halfTile * ((whole & 1) == 0 ? fraction : 1f - fraction);
49 }
50
58 public PointF GetSceneLayerCoordinatesAtPixel(SceneLayer sceneLayer, PointF pixelPt)
59 {
60 int W = sceneLayer.TileWidth;
61 int H = sceneLayer.TileHeight;
62
63 int originX = sceneLayer.OriginPx.X;
64 int originY = sceneLayer.OriginPx.Y;
65
66 float fy = (pixelPt.Y + originY) / (H * 0.75f);
67 int approxRow = (int)Math.Round(fy);
68
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);
72
73 var best = new Point(approxCol, approxRow);
74 float bestDist = float.MaxValue;
75
76 foreach (var cand in NeighborsPointedTop(approxCol, approxRow, includeSelf: true))
77 {
78 var poly = HexPolygonPointedTop(
79 sceneLayer,
80 cand.X,
81 cand.Y,
82 includeOverhang: false);
83
84 // Only here: round once for PIP test
85 var pInt = new Point((int)Math.Round(pixelPt.X), (int)Math.Round(pixelPt.Y));
86
87 if (PointInPolygon(poly, pInt))
88 return new PointF(cand.X, cand.Y);
89
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);
93
94 if (d < bestDist)
95 {
96 bestDist = d;
97 best = cand;
98 }
99 }
100
101 return new PointF(best.X, best.Y);
102 }
103
111 public List<SceneLayerTile> GetSceneLayerTilesInPixelRange(
112 SceneLayer sceneLayer,
113 Rectangle worldPixelRange,
114 bool includeOverhang)
115 {
116 var result = new List<SceneLayerTile>();
117 int W = sceneLayer.TileWidth;
118 int H = sceneLayer.TileHeight;
119 var candidateRange = TileBounds.GetTransformedTileCandidateRange(sceneLayer, worldPixelRange, includeOverhang);
120
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;
123
124 for (int row = minRow; row <= maxRow; row++)
125 {
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;
129
130 for (int col = minCol; col <= maxCol; col++)
131 {
132 var gp = sceneLayer[col, row];
133
134 if (gp == null)
135 continue;
136
137 var r = GetPixelRangeForTile(gp, includeOverhang);
138
139 if (r.IntersectsWith(worldPixelRange))
140 result.Add(gp);
141 }
142 }
143
144 return result;
145 }
146
153 public Rectangle GetPixelRangeForTile(Tile tile, bool includeOverhang)
154 {
155 var p = GetAnchorPixelAtSceneLayerCoordinates(tile.SceneLayer, tile.SceneLayerCoordinates);
156 int W = tile.SceneLayer.TileWidth;
157 int H = tile.SceneLayer.TileHeight;
158
159 var rect = new Rectangle(
160 p.X,
161 p.Y,
162 W,
163 H);
164
165 return TileBounds.ApplyTileGeometry(rect, tile, includeOverhang);
166 }
167
174 public Rectangle GetPixelRangeForTileList(List<Tile> tileList, bool includeOverhang)
175 {
176 Rectangle ret = Rectangle.Empty;
177
178 foreach (var t in tileList)
179 {
180 var r = GetPixelRangeForTile(t, includeOverhang);
181 ret = ret.IsEmpty ? r : Rectangle.Union(ret, r);
182 }
183
184 return ret;
185 }
186
194 public SceneLayerTile GetAdjacentSceneLayerTile(SceneLayerTile gp, CardinalDirections dir)
195 {
196 int x = gp.GridCoordinatesAbs.X;
197 int y = gp.GridCoordinatesAbs.Y;
198 bool even = (y & 1) == 0;
199 var m = gp.SceneLayer;
200
201 return dir switch
202 {
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,
211 _ => null
212 };
213 }
214
221 public Point[] GetPolygonPts(Tile tile, bool includeOverhang)
222 {
223 return HexPolygonPointedTop(
224 tile.SceneLayer,
225 (int)tile.SceneLayerCoordinates.X,
226 (int)tile.SceneLayerCoordinates.Y,
227 includeOverhang);
228 }
229
237 public PointF FindEquivalentSceneLayerCoordinates(PointF valColRow, int xUpperBound, int yUpperBound)
238 {
239 float modX = valColRow.X % (xUpperBound + 1);
240 float modY = valColRow.Y % (yUpperBound + 1);
241
242 if (modX < 0)
243 modX += xUpperBound + 1;
244
245 if (modY < 0)
246 modY += yUpperBound + 1;
247
248 return new PointF(modX, modY);
249 }
250
251 // Helpers ---------------------------------------------------
252 private static IEnumerable<Point> NeighborsPointedTop(int col, int row, bool includeSelf)
253 {
254 if (includeSelf)
255 yield return new Point(col, row);
256
257 bool even = (row & 1) == 0;
258 yield return new Point(col, row - 1); // N
259 yield return new Point(col, row + 1); // S
260 yield return new Point(col + (even ? 0 : 1), row - 1); // NE
261 yield return new Point(col - (even ? 1 : 0), row - 1); // NW
262 yield return new Point(col + (even ? 0 : 1), row + 1); // SE
263 yield return new Point(col - (even ? 1 : 0), row + 1); // SW
264 }
265
266 private static Point[] HexPolygonPointedTop(
267 SceneLayer sceneLayer,
268 int col,
269 int row,
270 bool includeOverhang)
271 {
272 int W = sceneLayer.TileWidth;
273 int H = sceneLayer.TileHeight;
274
275 int originX = sceneLayer.OriginPx.X;
276 int originY = sceneLayer.OriginPx.Y;
277
278 var p = new Point(
279 -originX + col * W + ((row & 1) == 0 ? 0 : W / 2),
280 -originY + (int)Math.Floor(row * (H * 0.75f)));
281
282 var rect = new Rectangle(
283 p.X,
284 p.Y,
285 W,
286 H);
287
288 var ohRect = TileBounds.ApplyOverhang(
289 rect,
290 includeOverhang
291 ? new Spacing(
292 0,
293 0,
294 0,
295 0)
296 : Spacing.None,
297 includeOverhang);
298
299 int x = ohRect.X;
300 int y = ohRect.Y;
301
302 // Pointed-top vertices from bounding box
303 return new[]
304 {
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)
311 };
312 }
313
314 private static bool PointInPolygon(Point[] poly, Point p)
315 {
316 bool inside = false;
317 int j = poly.Length - 1;
318
319 for (int i = 0; i < poly.Length; i++)
320 {
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) +
323 poly[i].X);
324
325 if (intersect)
326 inside = !inside;
327
328 j = i;
329 }
330
331 return inside;
332 }
333}
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