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
IsometricRhombicCoordinates.cs
1using System.Drawing;
3
5
11internal sealed class IsometricRhombicCoordinates : ISceneLayerCoordinates
12{
19 public Point GetAnchorPixelAtSceneLayerCoordinates(SceneLayer sceneLayer, PointF gp)
20 {
21 int W = sceneLayer.TileWidth;
22 int H = sceneLayer.TileHeight;
23
24 int originX = sceneLayer.OriginPx.X;
25 int originY = sceneLayer.OriginPx.Y;
26
27 // gp is in grid-space (x,y) already.
28 float dx = gp.X;
29 float dy = gp.Y;
30
31 float px = (dx - dy) * (W / 2f) - originX;
32 float py = (dx + dy) * (H / 2f) - originY;
33
34 return new Point((int)Math.Floor(px), (int)Math.Floor(py));
35 }
36
43 public PointF GetSceneLayerCoordinatesAtPixel(SceneLayer sceneLayer, PointF pixelPt)
44 {
45 int W = sceneLayer.TileWidth;
46 int H = sceneLayer.TileHeight;
47
48 int originX = sceneLayer.OriginPx.X;
49 int originY = sceneLayer.OriginPx.Y;
50
51 float a = (pixelPt.X + originX) / (W / 2f);
52 float b = (pixelPt.Y + originY) / (H / 2f);
53
54 float dx = (a + b) / 2f;
55 float dy = (b - a) / 2f;
56
57 return new PointF(dx, dy);
58 }
59
67 public List<SceneLayerTile> GetSceneLayerTilesInPixelRange(
68 SceneLayer sceneLayer,
69 Rectangle worldPixelRange,
70 bool includeOverhang)
71 {
72 var result = new List<SceneLayerTile>();
73 var candidateRange = TileBounds.GetTransformedTileCandidateRange(sceneLayer, worldPixelRange, includeOverhang);
74 var ul = GetSceneLayerCoordinatesAtPixel(sceneLayer, new PointF(candidateRange.Left, candidateRange.Top));
75 var ur = GetSceneLayerCoordinatesAtPixel(sceneLayer, new PointF(candidateRange.Right, candidateRange.Top));
76 var ll = GetSceneLayerCoordinatesAtPixel(sceneLayer, new PointF(candidateRange.Left, candidateRange.Bottom));
77 var lr = GetSceneLayerCoordinatesAtPixel(sceneLayer, new PointF(candidateRange.Right, candidateRange.Bottom));
78
79 int minX = (int)Math.Floor(new[] { ul.X, ur.X, ll.X, lr.X }.Min()) - 1;
80 int maxX = (int)Math.Ceiling(new[] { ul.X, ur.X, ll.X, lr.X }.Max()) + 1;
81 int minY = (int)Math.Floor(new[] { ul.Y, ur.Y, ll.Y, lr.Y }.Min()) - 1;
82 int maxY = (int)Math.Ceiling(new[] { ul.Y, ur.Y, ll.Y, lr.Y }.Max()) + 1;
83
84 for (int y = minY; y <= maxY; y++)
85 {
86 for (int x = minX; x <= maxX; x++)
87 {
88 var gp = sceneLayer[x, y];
89
90 if (gp == null)
91 continue;
92
93 var r = GetPixelRangeForTile(gp, includeOverhang);
94
95 if (r.IntersectsWith(worldPixelRange))
96 result.Add(gp);
97 }
98 }
99
100 return result;
101 }
102
109 public Rectangle GetPixelRangeForTile(Tile tile, bool includeOverhang)
110 {
111 var top = GetAnchorPixelAtSceneLayerCoordinates(tile.SceneLayer, tile.SceneLayerCoordinates);
112 int W = tile.SceneLayer.TileWidth;
113 int H = tile.SceneLayer.TileHeight;
114
115 var rect = new Rectangle(
116 top.X - W / 2,
117 top.Y,
118 W,
119 H);
120
121 return TileBounds.ApplyTileGeometry(rect, tile, includeOverhang);
122 }
123
130 public Rectangle GetPixelRangeForTileList(List<Tile> tileList, bool includeOverhang)
131 {
132 Rectangle ret = Rectangle.Empty;
133
134 foreach (var t in tileList)
135 {
136 var r = GetPixelRangeForTile(t, includeOverhang);
137 ret = ret.IsEmpty ? r : Rectangle.Union(ret, r);
138 }
139
140 return ret;
141 }
142
149 public SceneLayerTile GetAdjacentSceneLayerTile(SceneLayerTile gp, CardinalDirections dir)
150 {
151 var m = gp.SceneLayer;
152 int x = gp.GridCoordinatesAbs.X;
153 int y = gp.GridCoordinatesAbs.Y;
154
155 return dir switch
156 {
157 CardinalDirections.N => m.ResolveWrappedTile(x, y - 1),
158 CardinalDirections.S => m.ResolveWrappedTile(x, y + 1),
159 CardinalDirections.E => m.ResolveWrappedTile(x + 1, y),
160 CardinalDirections.W => m.ResolveWrappedTile(x - 1, y),
161 CardinalDirections.NE => m.ResolveWrappedTile(x + 1, y - 1),
162 CardinalDirections.NW => m.ResolveWrappedTile(x - 1, y - 1),
163 CardinalDirections.SE => m.ResolveWrappedTile(x + 1, y + 1),
164 CardinalDirections.SW => m.ResolveWrappedTile(x - 1, y + 1),
165 _ => null
166 };
167 }
168
175 public Point[] GetPolygonPts(Tile tile, bool includeOverhang)
176 {
177 var top = GetAnchorPixelAtSceneLayerCoordinates(tile.SceneLayer, tile.SceneLayerCoordinates);
178 int W = tile.SceneLayer.TileWidth;
179 int H = tile.SceneLayer.TileHeight;
180 var oh = includeOverhang ? tile.Overhang : Spacing.None;
181
182 return new[]
183 {
184 new Point(top.X, top.Y - oh.Top),
185 new Point(top.X + W / 2 + oh.Right, top.Y + H / 2),
186 new Point(top.X, top.Y + H + oh.Bottom),
187 new Point(top.X - W / 2 - oh.Left, top.Y + H / 2)
188 };
189 }
190
198 public PointF FindEquivalentSceneLayerCoordinates(PointF valColRow, int xUpperBound, int yUpperBound)
199 {
200 float modX = valColRow.X % (xUpperBound + 1);
201 float modY = valColRow.Y % (yUpperBound + 1);
202
203 if (modX < 0)
204 modX += xUpperBound + 1;
205
206 if (modY < 0)
207 modY += yUpperBound + 1;
208
209 return new PointF(modX, modY);
210 }
211}
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