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
FrameSequence.cs
1using System.Collections;
2using System.Runtime.Serialization;
4using Newtonsoft.Json;
5
7
11public struct FrameSequence : IEnumerable<Frame>
12{
13 #region fields
14
18 [JsonProperty] public CycleType SequenceCycleType;
19
20 [JsonProperty] private List<Frame> frameList;
21
22 [JsonProperty] private List<double?>? frameDurations;
23
27 public double? GetDurationSeconds(int index) =>
28 frameDurations is not null && index < frameDurations.Count ? frameDurations[index] : null;
29
33 public void SetDurationSeconds(int index, double? seconds)
34 {
35 if (index < 0 || index >= FrameCount)
36 throw new ArgumentOutOfRangeException(nameof(index));
37
38 if (seconds is { } duration && (!double.IsFinite(duration) || duration <= 0))
39 throw new ArgumentOutOfRangeException(nameof(seconds));
40
41 // Copy on write: cloned cycles share images, but editing timing must not change the original.
42 frameDurations = frameDurations is null ? new List<double?>() : new(frameDurations);
43
44 while (frameDurations.Count < FrameCount)
45 frameDurations.Add(null);
46
47 frameDurations[index] = seconds;
48 }
49
50 internal void SetDurations(IEnumerable<double?> durations)
51 {
52 var values = durations.ToList();
53
54 if (values.Count != FrameCount || values.Any(v => v is { } d && (!double.IsFinite(d) || d <= 0)))
55 throw new ArgumentException(
56 "Durations must match the sequence and be positive finite values or null.",
57 nameof(durations));
58
59 frameDurations = values.Any(v => v.HasValue) ? values : null;
60 }
61
62 private int currentFrameIdx;
63 private int curFrameIncrement;
64 private bool cycleFinished;
65
66 #endregion fields
67
68 #region constructors / finalizer
69
74 public FrameSequence(Frame frame)
75 {
76 frameList = new List<Frame>();
77 frameList.Add(frame);
79 currentFrameIdx = 0;
80 curFrameIncrement = 1;
81 cycleFinished = true;
82 }
83
88 public FrameSequence(List<Frame> frames)
89 {
90 frameList = frames;
92 currentFrameIdx = 0;
93 curFrameIncrement = 1;
94 cycleFinished = true;
95 }
96
97 [OnDeserialized()]
98 private void OnDeserialized(StreamingContext context)
99 {
100 currentFrameIdx = 0;
101 curFrameIncrement = 1;
102 cycleFinished = true;
103 }
104
105 #endregion constructors / finalizer
106
107 #region properties
108
112 [JsonIgnore]
113 public bool CycleFinished
114 {
115 get { return cycleFinished; }
116 }
117
121 [JsonIgnore]
122 public int FrameCount
123 {
124 get
125 {
126 if (frameList == null)
127 SetDefaults();
128
129 return frameList.Count;
130 }
131 }
132
136 [JsonIgnore]
138 {
139 get
140 {
141 if (frameList == null)
142 SetDefaults();
143
144 return frameList[currentFrameIdx];
145 }
146 }
147
151 [JsonIgnore]
153 {
154 get { return currentFrameIdx; }
155 }
156
160 [JsonIgnore]
161 public IList<Frame> FrameList
162 {
163 get { return frameList.AsReadOnly(); }
164 }
165
166 #endregion properties
167
168 #region public methods
169
179 Tilesheet bmp,
180 string regionName,
181 int xTile,
182 int yTile)
183 {
184 return AddFrame(
185 new Frame(
186 bmp,
187 regionName,
188 xTile,
189 yTile));
190 }
191
199 public Frame AddFrame(Tilesheet bmp, int xTile, int yTile)
200 {
201 return AddFrame(
202 new Frame(
203 bmp,
205 xTile,
206 yTile));
207 }
208
214 public Frame AddFrame(Frame frame)
215 {
216 if (frameList == null)
217 SetDefaults();
218
219 frameList.Add(frame);
220 return frame;
221 }
222
227 public void RemoveFrame(int idx)
228 {
229 if (idx < frameList.Count)
230 {
231 frameList.RemoveAt(idx);
232
233 if (frameDurations is not null && idx < frameDurations.Count)
234 {
235 frameDurations = new(frameDurations);
236 frameDurations.RemoveAt(idx);
237 }
238 }
239 }
240
244 public void Reset()
245 {
246 currentFrameIdx = 0;
247 curFrameIncrement = 1;
248 }
249
250 #endregion public methods
251
252 #region internal methods
253
254 internal void StopCycle()
255 {
256 cycleFinished = true;
257 }
258
259 internal Frame AdvanceFrame()
260 {
261 switch (SequenceCycleType)
262 {
263 case CycleType.PingPong:
264 currentFrameIdx += curFrameIncrement;
265
266 if ((currentFrameIdx <= 0) || (currentFrameIdx >= frameList.Count - 1))
267 curFrameIncrement *= -1;
268
269 if (currentFrameIdx < 0)
270 currentFrameIdx = 0;
271
272 if (currentFrameIdx > frameList.Count - 1)
273 currentFrameIdx = frameList.Count - 1;
274
275 cycleFinished = false;
276 break;
277
278 case CycleType.Repeating:
279 if (++currentFrameIdx >= frameList.Count)
280 currentFrameIdx = 0;
281
282 cycleFinished = false;
283 break;
284
285 case CycleType.Simple:
286 if (++currentFrameIdx > frameList.Count - 1)
287 {
288 currentFrameIdx = frameList.Count - 1;
289 cycleFinished = true;
290 }
291 else
292 cycleFinished = false;
293
294 break;
295
296 default:
297 throw new InvalidOperationException("Invalid CycleType: " + SequenceCycleType.ToString());
298 }
299
300 return frameList[currentFrameIdx];
301 }
302
303 #endregion internal methods
304
305 #region private methods
306
307 private void SetDefaults()
308 {
309 frameList = new List<Frame>();
311 currentFrameIdx = 0;
312 curFrameIncrement = 1;
313 cycleFinished = true;
314 }
315
316 #endregion private methods
317
318 #region indexers
319
325 public Frame this[int frameIdx]
326 {
327 get { return frameList[frameIdx]; }
328 }
329
330 #endregion indexers
331
332 #region IEnumerable Members
333
338 public IEnumerator GetEnumerator()
339 {
340 for (int i = 0; i < frameList.Count; i++)
341 yield return frameList[i];
342 }
343
348 IEnumerator<Frame> IEnumerable<Frame>.GetEnumerator()
349 {
350 return frameList.GetEnumerator();
351 }
352
353 #endregion IEnumerable Members
354}
Represents a rectangular region within a tilesheet that contains a grid of frames.
static readonly string DefaultRegionName
The default name assigned to tilesheet regions when no name is specified.
Represents a tilesheet image and metadata for rendering tiles.
Definition Tilesheet.cs:15
CycleType
Simple is self-terminating; the other two are repeating.
Definition CycleType.cs:11
double? GetDurationSeconds(int index)
Gets the optional display duration for a frame occurrence.
void RemoveFrame(int idx)
Removes the frame at the specified index from the sequence.
Frame AddFrame(Frame frame)
Adds an existing frame to the sequence.
Frame AddFrame(Tilesheet bmp, int xTile, int yTile)
Creates and adds a new frame to the sequence using tilesheet coordinates.
void Reset()
Resets the sequence to its initial state, starting from the first frame.
IList< Frame > FrameList
Gets a read-only view of the list of frames in the sequence.
Frame CurrentFrame
Gets the frame currently active in the animation sequence.
FrameSequence(Frame frame)
Initializes a new instance of the FrameSequence struct with a single frame.
int FrameCount
Gets the total number of frames in the sequence.
CycleType SequenceCycleType
The type of cycle pattern used when animating through the frame sequence.
void SetDurationSeconds(int index, double? seconds)
Sets a positive display duration, or null to use the cycle default.
Frame AddFrame(Tilesheet bmp, string regionName, int xTile, int yTile)
Creates and adds a new frame to the sequence using tilesheet coordinates.
FrameSequence(List< Frame > frames)
Initializes a new instance of the FrameSequence struct with a collection of frames.
int CurrentFrameIdx
Gets the zero-based index of the current frame in the sequence.
IEnumerator GetEnumerator()
Returns an enumerator that iterates through the frame sequence.
bool CycleFinished
Gets a value indicating whether the animation cycle has finished playing through the sequence.
Represents the source Tilesheet and its coordinates to render on a destination.
Definition Frame.cs:12