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
TileTransform.cs
1using System.Drawing;
3
4namespace Gondwana.Drawing;
5
33
35public static class TileTransformGeometry
36{
41 public static PointF TransformPoint(PointF point, TileTransform transform) => transform switch
42 {
43 TileTransform.Identity => point,
44 TileTransform.Rotate90 => new(-point.Y, point.X),
45 TileTransform.Rotate180 => new(-point.X, -point.Y),
46 TileTransform.Rotate270 => new(point.Y, -point.X),
47 TileTransform.FlipHorizontal => new(-point.X, point.Y),
48 TileTransform.FlipVertical => new(point.X, -point.Y),
49 TileTransform.FlipDiagonal => new(point.Y, point.X),
50 TileTransform.FlipAntiDiagonal => new(-point.Y, -point.X),
51 _ => throw new ArgumentOutOfRangeException(nameof(transform))
52 };
53
58 public static Size TransformSize(Size size, TileTransform transform)
59 {
60 var point = TransformPoint(new(size.Width, size.Height), transform);
61 return new((int)Math.Abs(point.X), (int)Math.Abs(point.Y));
62 }
63
68 public static Spacing TransformSpacing(Spacing value, TileTransform transform) => transform switch
69 {
70 TileTransform.Identity => value,
71 TileTransform.Rotate90 => new(
72 value.Bottom,
73 value.Left,
74 value.Top,
75 value.Right),
76 TileTransform.Rotate180 => new(
77 value.Right,
78 value.Bottom,
79 value.Left,
80 value.Top),
81 TileTransform.Rotate270 => new(
82 value.Top,
83 value.Right,
84 value.Bottom,
85 value.Left),
86 TileTransform.FlipHorizontal => new(
87 value.Right,
88 value.Top,
89 value.Left,
90 value.Bottom),
91 TileTransform.FlipVertical => new(
92 value.Left,
93 value.Bottom,
94 value.Right,
95 value.Top),
96 TileTransform.FlipDiagonal => new(
97 value.Top,
98 value.Left,
99 value.Bottom,
100 value.Right),
101 TileTransform.FlipAntiDiagonal => new(
102 value.Bottom,
103 value.Right,
104 value.Top,
105 value.Left),
106 _ => throw new ArgumentOutOfRangeException(nameof(transform))
107 };
108
113 public static CollisionAdjust TransformCollisionAdjust(CollisionAdjust value, TileTransform transform)
114 {
115 var edges = TransformSpacing(
116 new(
117 value.Left,
118 value.Top,
119 value.Right,
120 value.Bottom),
121 transform);
122
123 return new(
124 edges.Top,
125 edges.Bottom,
126 edges.Left,
127 edges.Right);
128 }
129
134 public static TileTransform Compose(TileTransform current, TileTransform operation)
135 {
136 var x = TransformPoint(TransformPoint(new(1, 0), current), operation);
137 var y = TransformPoint(TransformPoint(new(0, 1), current), operation);
138
139 foreach (var candidate in Enum.GetValues<TileTransform>())
140 if (TransformPoint(new(1, 0), candidate) == x && TransformPoint(new(0, 1), candidate) == y)
141 return candidate;
142
143 throw new InvalidOperationException("Orthogonal transforms must be closed under composition.");
144 }
145
151 public static Spacing ScaleOverhang(Size sourceSize, Size cellSize, Spacing sourceOverhang)
152 {
153 if (sourceOverhang.IsEmpty)
154 return sourceOverhang;
155
156 double footprintWidth = (double)sourceSize.Width - sourceOverhang.Left - sourceOverhang.Right;
157 double footprintHeight = (double)sourceSize.Height - sourceOverhang.Top - sourceOverhang.Bottom;
158
159 // Legacy definitions may not describe a positive source footprint.
160 // Preserve their absolute overhang on the affected axis.
161 double scaleX = sourceSize.Width > 0 && footprintWidth > 0
162 ? cellSize.Width / footprintWidth
163 : 1d;
164
165 double scaleY = sourceSize.Height > 0 && footprintHeight > 0
166 ? cellSize.Height / footprintHeight
167 : 1d;
168
169 return new Spacing(
170 ScaleEdge(sourceOverhang.Left, scaleX),
171 ScaleEdge(sourceOverhang.Top, scaleY),
172 ScaleEdge(sourceOverhang.Right, scaleX),
173 ScaleEdge(sourceOverhang.Bottom, scaleY));
174 }
175
176 private static int ScaleEdge(int edge, double scale) =>
177 (int)Math.Round(edge * scale, MidpointRounding.AwayFromZero);
178
185 public static Rectangle GetVisualBounds(
186 Rectangle cell,
187 Size sourceSize,
188 Spacing sourceOverhang,
189 TileTransform transform) =>
190 GetVisualBounds(cell, ScaleOverhang(sourceSize, cell.Size, sourceOverhang), transform);
191
197 public static Rectangle GetVisualBounds(Rectangle cell, Spacing sourceOverhang, TileTransform transform)
198 {
199 var size = TransformSize(cell.Size, transform);
200 int x = cell.X + (int)Math.Floor((cell.Width - size.Width) / 2d);
201 int y = cell.Y + (int)Math.Floor((cell.Height - size.Height) / 2d);
202 var edges = TransformSpacing(sourceOverhang, transform);
203
204 return Rectangle.FromLTRB(
205 x - edges.Left,
206 y - edges.Top,
207 x + size.Width + edges.Right,
208 y + size.Height + edges.Bottom);
209 }
210
216 public static PointF MapToDestination(PointF normalizedPoint, RectangleF destination, TileTransform transform)
217 {
218 var point = TransformPoint(new(normalizedPoint.X - .5f, normalizedPoint.Y - .5f), transform);
219
220 return new(
221 destination.X + (point.X + .5f) * destination.Width,
222 destination.Y + (point.Y + .5f) * destination.Height);
223 }
224}
@ Rectangle
Particles are spawned uniformly within a rectangular area. This is the default distribution.
TileTransform
The eight orthogonal orientations of artwork placed in a fixed scene cell.
@ FlipDiagonal
Reflection across the top-left to bottom-right diagonal (axis swap).
@ FlipVertical
Reflection across the horizontal axis.
@ Rotate90
Clockwise quarter turn.
@ Rotate270
Counterclockwise quarter turn.
@ FlipAntiDiagonal
Reflection across the top-right to bottom-left diagonal.
@ Identity
Original orientation.
@ FlipHorizontal
Reflection across the vertical axis.
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
int Top
Gets or sets the number of pixels by which to inset the top edge.
int Left
Gets or sets the number of pixels by which to inset the left edge.
int Bottom
Gets or sets the number of pixels by which to inset the bottom edge.
int Right
Gets or sets the number of pixels by which to inset the right edge.