Gondwana Game Engine Development (master) - 2.6.1
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
Cycle.cs
1using System.Runtime.Serialization;
4using Newtonsoft.Json;
5
7
12[JsonObject(IsReference = true)]
13public class Cycle : ICloneable, IDisposable
14{
15 [Newtonsoft.Json.JsonIgnore]
16 internal DefinitionProvenance? DefinitionProvenance { get; set; }
17
18 #region fields
19
23 [JsonProperty]
25
29 [JsonProperty]
30 public readonly string CycleKey;
31
35 [JsonProperty]
36 public readonly bool HideTileOnCycleEnd;
37
38 internal long _throttle = 0;
39
40 #endregion fields
41
42 #region constructors / destructor
43
51 public Cycle(FrameSequence sequence, double throttleTime, string cycleKey, bool hideTileOnCycleEnd = false)
52 {
53 Sequence = sequence;
54 ThrottleTime = throttleTime;
55 NextCycle = this;
56 CycleKey = cycleKey;
57 HideTileOnCycleEnd = hideTileOnCycleEnd;
58
59 if (Cycle._cycles.ContainsKey(cycleKey))
60 Cycle._cycles[cycleKey] = this;
61 else
62 Cycle._cycles.Add(cycleKey, this);
63 }
64
65 private Cycle(Cycle fromCycle)
66 {
67 Sequence = fromCycle.Sequence;
68 _throttle = fromCycle._throttle;
69 _throttleTime = fromCycle._throttleTime;
70 NextCycle = this;
71 CycleKey = fromCycle.CycleKey;
72 }
73
74 [OnDeserialized()]
75 private void OnDeserialized(StreamingContext context)
76 {
77 if (Cycle._cycles.ContainsKey(CycleKey))
78 Cycle._cycles[CycleKey] = this;
79 else
80 Cycle._cycles.Add(CycleKey, this);
81 }
82
83 #endregion constructors / destructor
84
85 #region public properties
86
87 private double _throttleTime;
88
90 [JsonIgnore]
92 Sequence.GetDurationSeconds(Sequence.CurrentFrameIdx) ?? ThrottleTime;
93
94 internal long CurrentFrameThrottle
95 {
96 get
97 {
98 var ticks = CurrentFrameDurationSeconds * HighResTimer.TicksPerSecond;
99 return !double.IsFinite(ticks) || ticks < 1 || ticks >= long.MaxValue ? 0 : (long)ticks;
100 }
101 }
102
106 [JsonProperty]
107 public double ThrottleTime
108 {
109 get { return _throttleTime; }
110 set
111 {
112 _throttle = (long)(value * (double)HighResTimer.TicksPerSecond);
113 _throttleTime = value;
114 }
115 }
116
120 [JsonIgnore]
121 public double TotalCycleTime
122 {
123 get
124 {
125 if (Enumerable.Range(0, Sequence.FrameCount).Any(i => Sequence.GetDurationSeconds(i).HasValue))
126 {
127 double total = 0;
128 for (int i = 0; i < Sequence.FrameCount; i++)
129 {
130 double duration = Sequence.GetDurationSeconds(i) ?? ThrottleTime;
131 total += duration;
132 if (Sequence.SequenceCycleType == CycleType.PingPong && i > 0 && i < Sequence.FrameCount - 1)
133 total += duration;
134 }
135 return total;
136 }
137 switch (Sequence.SequenceCycleType)
138 {
139 case CycleType.Simple:
140 // -1 since first frame is played right away
141 return ThrottleTime * (double)(Sequence.FrameCount - 1);
142
143 case CycleType.Repeating:
144 return ThrottleTime * (double)Sequence.FrameCount;
145
146 case CycleType.PingPong:
147 // -2, since:
148 // "C" is only shown once, and...
149 // second "A" is actually part of next cycle repetition
150 return ThrottleTime * (double)((Sequence.FrameCount * 2) - 2);
151
152 default:
153 return 0;
154 }
155 }
156 }
157
161 [JsonProperty]
162 public Cycle NextCycle { get; set; }
163
164 #endregion public properties
165
166 #region ICloneable Members
167
172 public object Clone()
173 {
174 return new Cycle(this);
175 }
176
177 #endregion ICloneable Members
178
179 #region IDisposable Members
180
184 public void Dispose()
185 {
186 GC.SuppressFinalize(this);
187
188 foreach (Sprite sprite in SpriteManager.Instance._spriteList)
189 {
190 if (sprite.TileAnimator.CurrentCycle == this)
191 sprite.TileAnimator.CurrentCycle = null;
192 }
193
194 Cycle._cycles.Remove(CycleKey);
195 }
196
197 #endregion IDisposable Members
198
199 #region static members
200
201 internal static readonly Dictionary<string, Cycle> _cycles = new();
202
206 public static int Count => _cycles.Count;
207
212 public static List<string> GetAnimationCycleKeys() => new List<string>(_cycles.Keys);
213
218 public static List<Cycle> GetAnimationCycles() => new List<Cycle>(_cycles.Values);
219
225 public static Cycle GetAnimationCycle(string cycleKey)
226 {
227 if (_cycles.ContainsKey(cycleKey))
228 return (Cycle)_cycles[cycleKey].Clone();
229 else
230 return null;
231 }
232
237 public static void ClearAnimationCycle(string cycleKey)
238 {
239 if (_cycles.ContainsKey(cycleKey))
240 _cycles[cycleKey].Dispose();
241 }
242
246 public static void ClearAllAnimationCycles()
247 {
248 var tempCycles = new List<Cycle>(_cycles.Values);
249 foreach (Cycle cyc in tempCycles)
250 cyc.Dispose();
251 }
252
253 #endregion static members
254}
References a FrameSequence object, along with a particular Throttle value for animating through Frame...
Definition Cycle.cs:14
static void ClearAllAnimationCycles()
Removes and disposes all registered animation cycles.
Definition Cycle.cs:246
double TotalCycleTime
Returns the total time in seconds for the Cycle.
Definition Cycle.cs:122
static int Count
Gets the total number of animation cycles currently registered.
Definition Cycle.cs:206
double ThrottleTime
Gets or sets the time in seconds between frame transitions in the animation cycle.
Definition Cycle.cs:108
object Clone()
Creates a shallow copy of the current Cycle instance.
Definition Cycle.cs:172
double CurrentFrameDurationSeconds
Effective duration of the currently displayed frame.
Definition Cycle.cs:91
Cycle NextCycle
Gets or sets the next cycle to transition to when this cycle completes.
Definition Cycle.cs:162
static void ClearAnimationCycle(string cycleKey)
Removes and disposes the animation cycle with the specified key.
Definition Cycle.cs:237
Cycle(FrameSequence sequence, double throttleTime, string cycleKey, bool hideTileOnCycleEnd=false)
Initializes a new instance of the Cycle class.
Definition Cycle.cs:51
static Cycle GetAnimationCycle(string cycleKey)
Retrieves a clone of the animation cycle with the specified key.
Definition Cycle.cs:225
void Dispose()
Releases all resources used by the Cycle and removes it from the static cycle collection.
Definition Cycle.cs:184
readonly bool HideTileOnCycleEnd
Indicates whether the tile should be hidden when the cycle completes.
Definition Cycle.cs:36
FrameSequence Sequence
The frame sequence used by this animation cycle.
Definition Cycle.cs:24
static List< string > GetAnimationCycleKeys()
Retrieves a list of all registered animation cycle keys.
static List< Cycle > GetAnimationCycles()
Retrieves a list of all registered animation cycles.
readonly string CycleKey
The unique identifier key for this cycle.
Definition Cycle.cs:30
Manages the lifecycle, lookup, and per-frame updates of all active Sprite instances.
static SpriteManager Instance
Gets the singleton instance of the sprite manager.
Represents a movable visual element on a scene layer.
Definition Sprite.cs:20
virtual Animator TileAnimator
Gets the animator associated with this tile.
Definition Tile.cs:217
CycleType
Simple is self-terminating; the other two are repeating.
Definition CycleType.cs:11
Represents a sequence of animation frames that can be cycled through using different animation patter...