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
OrthogonalCoordinates.cs
1using System.Drawing;
3
5
10internal sealed class OrthogonalCoordinates : ISceneLayerCoordinates
11{
18 public Point GetAnchorPixelAtSceneLayerCoordinates(SceneLayer sceneLayer, PointF layerPoint)
19 {
20 int W = sceneLayer.TileWidth;
21 int H = sceneLayer.TileHeight;
22
23 var origin = sceneLayer.OriginPx;
24
25 int x = (int)(W * layerPoint.X) - origin.X;
26 int y = (int)(H * layerPoint.Y) - origin.Y;
27
28 return new Point(x, y);
29 }
30
37 public PointF GetSceneLayerCoordinatesAtPixel(SceneLayer sceneLayer, PointF pixelPt)
38 {
39 int W = sceneLayer.TileWidth;
40 int H = sceneLayer.TileHeight;
41
42 int originX = sceneLayer.OriginPx.X;
43 int originY = sceneLayer.OriginPx.Y;
44
45 float gx = (pixelPt.X + originX) / W;
46 float gy = (pixelPt.Y + originY) / H;
47
48 return new PointF(gx, gy);
49 }
50
59 public List<SceneLayerTile> GetSceneLayerTilesInPixelRange(
60 SceneLayer sceneLayer,
61 Rectangle worldPixelRange,
62 bool includeOverhang)
63 {
64 var retVal = new List<SceneLayerTile>();
65 var candidateRange = TileBounds.GetTransformedTileCandidateRange(sceneLayer, worldPixelRange, includeOverhang);
66
67 // 1) Find coarse grid bounds via inverse transform (unchanged)
68 PointF ptUL = GetSceneLayerCoordinatesAtPixel(sceneLayer, new PointF(candidateRange.Left, candidateRange.Top));
69
70 PointF ptBR = GetSceneLayerCoordinatesAtPixel(
71 sceneLayer,
72 new PointF(candidateRange.Right - 1, candidateRange.Bottom - 1));
73
74 int minY = (int)Math.Floor(ptUL.Y) - 1;
75 int maxY = (int)Math.Ceiling(ptBR.Y) + 1;
76 int minX = (int)Math.Floor(ptUL.X) - 1;
77 int maxX = (int)Math.Ceiling(ptBR.X) + 1;
78
79 // 2) Scan candidate grid cells and include if their overhang-aware rect intersects
80 for (int y = minY; y <= maxY; y++)
81 {
82 for (int x = minX; x <= maxX; x++)
83 {
84 var gPt = sceneLayer[x, y];
85
86 if (gPt == null)
87 continue;
88
89 // Overhang-aware pixel rect
90 var rect = GetPixelRangeForTile(gPt, includeOverhang);
91
92 if (rect.IntersectsWith(worldPixelRange))
93 retVal.Add(gPt);
94 }
95 }
96
97 return retVal;
98 }
99
106 public Rectangle GetPixelRangeForTile(Tile tile, bool includeOverhang)
107 {
108 var layer = tile.SceneLayer;
109 int W = layer.TileWidth;
110 int H = layer.TileHeight;
111
112 int originX = layer.OriginPx.X;
113 int originY = layer.OriginPx.Y;
114
115 var baseRect = new Rectangle
116 {
117 X = (int)(W * tile.SceneLayerCoordinates.X) - originX,
118 Y = (int)(H * tile.SceneLayerCoordinates.Y) - originY,
119 Width = W,
120 Height = H
121 };
122
123 // Apply full overhang (Left/Top/Right/Bottom)
124 return TileBounds.ApplyTileGeometry(baseRect, tile, includeOverhang);
125 }
126
133 public Rectangle GetPixelRangeForTileList(List<Tile> tileList, bool includeOverhang)
134 {
135 Rectangle retVal = Rectangle.Empty;
136
137 foreach (Tile tile in tileList)
138 {
139 var rect = GetPixelRangeForTile(tile, includeOverhang);
140 retVal = retVal.IsEmpty ? rect : Rectangle.Union(retVal, rect);
141 }
142
143 return retVal;
144 }
145
152 public SceneLayerTile GetAdjacentSceneLayerTile(SceneLayerTile layerPoint, CardinalDirections direction)
153 {
154 SceneLayer sceneLayer = layerPoint.SceneLayer;
155
156 switch (direction)
157 {
158 case CardinalDirections.N:
159 return sceneLayer.ResolveWrappedTile(
160 layerPoint.GridCoordinatesAbs.X,
161 layerPoint.GridCoordinatesAbs.Y - 1);
162
163 case CardinalDirections.NE:
164 return sceneLayer.ResolveWrappedTile(
165 layerPoint.GridCoordinatesAbs.X + 1,
166 layerPoint.GridCoordinatesAbs.Y - 1);
167
168 case CardinalDirections.E:
169 return sceneLayer.ResolveWrappedTile(
170 layerPoint.GridCoordinatesAbs.X + 1,
171 layerPoint.GridCoordinatesAbs.Y);
172
173 case CardinalDirections.SE:
174 return sceneLayer.ResolveWrappedTile(
175 layerPoint.GridCoordinatesAbs.X + 1,
176 layerPoint.GridCoordinatesAbs.Y + 1);
177
178 case CardinalDirections.S:
179 return sceneLayer.ResolveWrappedTile(
180 layerPoint.GridCoordinatesAbs.X,
181 layerPoint.GridCoordinatesAbs.Y + 1);
182
183 case CardinalDirections.SW:
184 return sceneLayer.ResolveWrappedTile(
185 layerPoint.GridCoordinatesAbs.X - 1,
186 layerPoint.GridCoordinatesAbs.Y + 1);
187
188 case CardinalDirections.W:
189 return sceneLayer.ResolveWrappedTile(
190 layerPoint.GridCoordinatesAbs.X - 1,
191 layerPoint.GridCoordinatesAbs.Y);
192
193 case CardinalDirections.NW:
194 return sceneLayer.ResolveWrappedTile(
195 layerPoint.GridCoordinatesAbs.X - 1,
196 layerPoint.GridCoordinatesAbs.Y - 1);
197
198 default:
199 return null;
200 }
201 }
202
209 public Point[] GetPolygonPts(Tile tile, bool includeOverhang)
210 {
211 // Square polygon using the overhang-aware rect
212 var r = GetPixelRangeForTile(tile, includeOverhang);
213
214 return new[]
215 {
216 new Point(r.Left, r.Top),
217 new Point(r.Right, r.Top),
218 new Point(r.Right, r.Bottom),
219 new Point(r.Left, r.Bottom)
220 };
221 }
222
230 public PointF FindEquivalentSceneLayerCoordinates(PointF valColRow, int xUpperBound, int yUpperBound)
231 {
232 float modX = valColRow.X % (xUpperBound + 1);
233 float modY = valColRow.Y % (yUpperBound + 1);
234
235 if (modX < 0)
236 modX += xUpperBound + 1;
237
238 if (modY < 0)
239 modY += yUpperBound + 1;
240
241 return new PointF(modX, modY);
242 }
243}
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.
SceneLayerTile? ResolveWrappedTile(int column, int row)
Resolves a virtual grid cell to its canonical tile. Only enabled axes wrap; an out-of-range disabled ...
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.
@ SceneLayer
Performs direct drawing within a scene layer using world-space coordinates.