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
IsometricAxialCoordinates.cs
1using System.Drawing;
3
5
12internal sealed class IsometricAxialCoordinates : ISceneLayerCoordinates
13{
14 // Precompute half sizes repeatedly used
15 private static void WH(
16 SceneLayer m,
17 out int W,
18 out int H,
19 out float halfW,
20 out float halfH)
21 {
22 W = m.TileWidth;
23 H = m.TileHeight;
24 halfW = W * 0.5f;
25 halfH = H * 0.5f;
26 }
27
34 public Point GetAnchorPixelAtSceneLayerCoordinates(SceneLayer sceneLayer, PointF gp)
35 {
36 WH(
37 sceneLayer,
38 out int W,
39 out _,
40 out float halfW,
41 out float halfH);
42
43 int originX = sceneLayer.OriginPx.X;
44 int originY = sceneLayer.OriginPx.Y;
45
46 float px = -originX + gp.X * W + gp.Y * halfW;
47 float py = -originY + gp.Y * halfH;
48
49 return new Point((int)Math.Floor(px), (int)Math.Floor(py));
50 }
51
58 public PointF GetSceneLayerCoordinatesAtPixel(SceneLayer sceneLayer, PointF pixelPt)
59 {
60 WH(
61 sceneLayer,
62 out int W,
63 out _,
64 out float halfW,
65 out float halfH);
66
67 int originX = sceneLayer.OriginPx.X;
68 int originY = sceneLayer.OriginPx.Y;
69
70 float gyF = (pixelPt.Y + originY) / halfH;
71 float gxF = (pixelPt.X + originX - gyF * halfW) / W;
72
73 return new PointF(gxF, gyF);
74 }
75
83 public List<SceneLayerTile> GetSceneLayerTilesInPixelRange(
84 SceneLayer sceneLayer,
85 Rectangle worldPixelRange,
86 bool includeOverhang)
87 {
88 var result = new List<SceneLayerTile>();
89
90 WH(
91 sceneLayer,
92 out _,
93 out _,
94 out _,
95 out _);
96
97 var candidateRange = TileBounds.GetTransformedTileCandidateRange(sceneLayer, worldPixelRange, includeOverhang);
98
99 // Corner → coarse grid bounds (continuous)
100 var ul = GetSceneLayerCoordinatesAtPixel(sceneLayer, new PointF(candidateRange.Left, candidateRange.Top));
101 var ur = GetSceneLayerCoordinatesAtPixel(sceneLayer, new PointF(candidateRange.Right, candidateRange.Top));
102 var ll = GetSceneLayerCoordinatesAtPixel(sceneLayer, new PointF(candidateRange.Left, candidateRange.Bottom));
103 var lr = GetSceneLayerCoordinatesAtPixel(sceneLayer, new PointF(candidateRange.Right, candidateRange.Bottom));
104
105 int minX = (int)System.Math.Floor(System.Math.Min(System.Math.Min(ul.X, ur.X), System.Math.Min(ll.X, lr.X))) -
106 2;
107
108 int maxX = (int)System.Math.Ceiling(System.Math.Max(System.Math.Max(ul.X, ur.X), System.Math.Max(ll.X, lr.X))) +
109 2;
110
111 int minY = (int)System.Math.Floor(System.Math.Min(System.Math.Min(ul.Y, ur.Y), System.Math.Min(ll.Y, lr.Y))) -
112 2;
113
114 int maxY = (int)System.Math.Ceiling(System.Math.Max(System.Math.Max(ul.Y, ur.Y), System.Math.Max(ll.Y, lr.Y))) +
115 2;
116
117 // Clamp to layer bounds (no wrapping)
118 int cols = sceneLayer.GridColumnCount;
119 int rows = sceneLayer.GridRowCount;
120
121 int xStart = System.Math.Max(minX, 0);
122 int xEnd = System.Math.Min(maxX, cols - 1);
123 int yStart = System.Math.Max(minY, 0);
124 int yEnd = System.Math.Min(maxY, rows - 1);
125
126 if (xStart > xEnd || yStart > yEnd)
127 return result;
128
129 for (int y = yStart; y <= yEnd; y++)
130 {
131 for (int x = xStart; x <= xEnd; x++)
132 {
133 var gp = sceneLayer[x, y];
134
135 if (gp == null)
136 continue;
137
138 var r = GetPixelRangeForTile(gp, includeOverhang);
139
140 if (r.IntersectsWith(worldPixelRange))
141 result.Add(gp);
142 }
143 }
144
145 return result;
146 }
147
154 public Rectangle GetPixelRangeForTile(Tile tile, bool includeOverhang)
155 {
156 WH(
157 tile.SceneLayer,
158 out int W,
159 out int H,
160 out float halfW,
161 out _);
162
163 // Top vertex of the diamond
164 var top = GetAnchorPixelAtSceneLayerCoordinates(tile.SceneLayer, tile.SceneLayerCoordinates);
165
166 // Diamond fits exactly in W×H box whose top-left is (top.X - W/2, top.Y)
167 var rect = new Rectangle(
168 top.X - (int)halfW,
169 top.Y,
170 W,
171 H);
172
173 return TileBounds.ApplyTileGeometry(rect, tile, includeOverhang);
174 }
175
182 public Rectangle GetPixelRangeForTileList(List<Tile> tileList, bool includeOverhang)
183 {
184 Rectangle ret = Rectangle.Empty;
185
186 foreach (var t in tileList)
187 {
188 var r = GetPixelRangeForTile(t, includeOverhang);
189 ret = ret.IsEmpty ? r : Rectangle.Union(ret, r);
190 }
191
192 return ret;
193 }
194
201 public SceneLayerTile GetAdjacentSceneLayerTile(SceneLayerTile gp, CardinalDirections dir)
202 {
203 var m = gp.SceneLayer;
204 int x = gp.GridCoordinatesAbs.X;
205 int y = gp.GridCoordinatesAbs.Y;
206
207 // Square-like adjacency over the rectangular index space
208 return dir switch
209 {
210 CardinalDirections.N => m.ResolveWrappedTile(x, y - 1),
211 CardinalDirections.S => m.ResolveWrappedTile(x, y + 1),
212 CardinalDirections.E => m.ResolveWrappedTile(x + 1, y),
213 CardinalDirections.W => m.ResolveWrappedTile(x - 1, y),
214 CardinalDirections.NE => m.ResolveWrappedTile(x + 1, y - 1),
215 CardinalDirections.NW => m.ResolveWrappedTile(x - 1, y - 1),
216 CardinalDirections.SE => m.ResolveWrappedTile(x + 1, y + 1),
217 CardinalDirections.SW => m.ResolveWrappedTile(x - 1, y + 1),
218 _ => null
219 };
220 }
221
228 public Point[] GetPolygonPts(Tile tile, bool includeOverhang)
229 {
230 WH(
231 tile.SceneLayer,
232 out int W,
233 out int H,
234 out _,
235 out _);
236
237 var top = GetAnchorPixelAtSceneLayerCoordinates(tile.SceneLayer, tile.SceneLayerCoordinates);
238 var oh = includeOverhang ? tile.Overhang : Spacing.None;
239
240 // Diamond vertices (top, right, bottom, left)
241 return new[]
242 {
243 new Point(top.X, top.Y - oh.Top),
244 new Point(top.X + W / 2 + oh.Right, top.Y + H / 2),
245 new Point(top.X, top.Y + H + oh.Bottom),
246 new Point(top.X - W / 2 - oh.Left, top.Y + H / 2)
247 };
248 }
249
257 public PointF FindEquivalentSceneLayerCoordinates(PointF valColRow, int xUpperBound, int yUpperBound)
258 {
259 // For now, keep the simple torus mapping; if you prefer no wrap, clamp upstream.
260 float modX = valColRow.X % (xUpperBound + 1);
261 float modY = valColRow.Y % (yUpperBound + 1);
262
263 if (modX < 0)
264 modX += xUpperBound + 1;
265
266 if (modY < 0)
267 modY += yUpperBound + 1;
268
269 return new PointF(modX, modY);
270 }
271}
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 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.
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