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
TilesheetDefinitionValidator.cs
2
4
6public static class TilesheetDefinitionValidator
7{
13 public static IReadOnlyList<string> Validate(
14 TilesheetDefinition definition,
15 int? imageWidth = null,
16 int? imageHeight = null)
17 {
18 ArgumentNullException.ThrowIfNull(definition);
19 var errors = new List<string>();
20 var names = new HashSet<string>(StringComparer.OrdinalIgnoreCase);
21
22 foreach (var region in definition.Regions ?? [])
23 {
24 if (region is null)
25 {
26 errors.Add("Regions contains a null entry.");
27 continue;
28 }
29
30 var label = $"Region '{region.Name}'";
31
32 if (string.IsNullOrWhiteSpace(region.Name))
33 errors.Add($"{label}: name is empty.");
34
35 if (!names.Add(region.Name))
36 errors.Add($"{label}: duplicate name.");
37
38 if (region.Area.Width <= 0 || region.Area.Height <= 0 || region.TileSize.Width <= 0 ||
39 region.TileSize.Height <= 0)
40 {
41 errors.Add($"{label}: area and tile dimensions must be positive.");
42 continue;
43 }
44
45 if (region.Area.X < 0 || region.Area.Y < 0 ||
46 (imageWidth.HasValue && (long)region.Area.X + region.Area.Width > imageWidth) ||
47 (imageHeight.HasValue && (long)region.Area.Y + region.Area.Height > imageHeight))
48 errors.Add($"{label}: area is outside the source image.");
49
50 if (Negative(region.TilePadding))
51 errors.Add($"{label}: tile padding cannot be negative.");
52
53 if (region.RegionMargin.Left < 0 || region.RegionMargin.Top < 0 ||
54 region.RegionMargin.Right < -region.TilePadding.Right ||
55 region.RegionMargin.Bottom < -region.TilePadding.Bottom)
56 errors.Add(
57 $"{label}: leading margins cannot be negative; trailing margins may only cancel trailing tile padding.");
58
59 if (Negative(region.Overhang))
60 errors.Add($"{label}: overhang cannot be negative.");
61
62 var (columns, rows) = GridSize(region);
63
64 if (columns <= 0 || rows <= 0)
65 errors.Add($"{label}: layout contains no complete frames.");
66
67 CheckCollision(
68 region.CollisionAdjust,
69 region,
70 label,
71 errors);
72
73 if (!Enum.IsDefined(region.CollisionType))
74 errors.Add($"{label}: unknown collision type.");
75
76 var coordinates = new HashSet<(int, int)>();
77
78 foreach (var frame in region.Frames ?? [])
79 {
80 if (frame is null)
81 {
82 errors.Add($"{label}: null frame metadata.");
83 continue;
84 }
85
86 var frameLabel = $"{label}, frame ({frame.XTile}, {frame.YTile})";
87
88 if (!coordinates.Add((frame.XTile, frame.YTile)))
89 errors.Add($"{frameLabel}: duplicate frame metadata.");
90
91 if (frame.XTile < 0 || frame.YTile < 0 || frame.XTile >= columns || frame.YTile >= rows)
92 errors.Add($"{frameLabel}: coordinates are outside the frame grid.");
93
94 if (frame.CollisionAdjust is { } adjust)
95 CheckCollision(
96 adjust,
97 region,
98 frameLabel,
99 errors);
100
101 if (frame.CollisionType is { } type && !Enum.IsDefined(type))
102 errors.Add($"{frameLabel}: unknown collision type.");
103 }
104 }
105
106 return errors;
107 }
108
112 public static (long Columns, long Rows) GridSize(TilesheetRegionDefinition region)
113 {
114 long width = (long)region.TileSize.Width + region.TilePadding.Left + region.TilePadding.Right;
115 long height = (long)region.TileSize.Height + region.TilePadding.Top + region.TilePadding.Bottom;
116
117 return (
118 width <= 0
119 ? 0
120 : Math.Max(0, ((long)region.Area.Width - region.RegionMargin.Left - region.RegionMargin.Right) / width),
121 height <= 0
122 ? 0
123 : Math.Max(
124 0,
125 ((long)region.Area.Height - region.RegionMargin.Top - region.RegionMargin.Bottom) / height));
126 }
127
128 private static bool Negative(Spacing spacing) =>
129 spacing.Left < 0 || spacing.Right < 0 || spacing.Top < 0 || spacing.Bottom < 0;
130
131 private static void CheckCollision(
132 CollisionAdjust adjust,
133 TilesheetRegionDefinition region,
134 string label,
135 List<string> errors)
136 {
137 // Negative adjustments intentionally expand collision bounds. Only inverted
138 // geometry is impossible; zero-sized collision areas may disable collisions.
139 if ((long)adjust.Left + adjust.Right > region.TileSize.Width ||
140 (long)adjust.Top + adjust.Bottom > region.TileSize.Height)
141 errors.Add($"{label}: collision adjustments invert the frame's collision rectangle.");
142 }
143}
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.