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
Cycle.cs
1using System.Runtime.Serialization;
5using Newtonsoft.Json;
6
8
13[JsonObject(IsReference = true)]
14public class Cycle : ICloneable, IDisposable
15{
16 [Newtonsoft.Json.JsonIgnore] internal DefinitionProvenance? DefinitionProvenance { get; set; }
17
18 #region fields
19
23 [JsonProperty] public FrameSequence Sequence;
24
28 [JsonProperty] public readonly string CycleKey;
29
33 [JsonProperty] public readonly bool HideTileOnCycleEnd;
34
35 internal long _throttle = 0;
36
37 #endregion fields
38
39 #region constructors / destructor
40
48 public Cycle(
49 FrameSequence sequence,
50 double throttleTime,
51 string cycleKey,
52 bool hideTileOnCycleEnd = false)
53 {
54 Sequence = sequence;
55 ThrottleTime = throttleTime;
56 NextCycle = this;
57 CycleKey = cycleKey;
58 HideTileOnCycleEnd = hideTileOnCycleEnd;
59
60 if (Cycle._cycles.ContainsKey(cycleKey))
61 Cycle._cycles[cycleKey] = this;
62 else
63 Cycle._cycles.Add(cycleKey, this);
64 }
65
66 private Cycle(Cycle fromCycle)
67 {
68 Sequence = fromCycle.Sequence;
69 _throttle = fromCycle._throttle;
70 _throttleTime = fromCycle._throttleTime;
71 NextCycle = this;
72 CycleKey = fromCycle.CycleKey;
74 }
75
76 [OnDeserialized()]
77 private void OnDeserialized(StreamingContext context)
78 {
79 if (Cycle._cycles.ContainsKey(CycleKey))
80 Cycle._cycles[CycleKey] = this;
81 else
82 Cycle._cycles.Add(CycleKey, this);
83 }
84
85 #endregion constructors / destructor
86
87 #region public properties
88
89 private double _throttleTime;
90
92 [JsonIgnore]
94 Sequence.GetDurationSeconds(Sequence.CurrentFrameIdx) ?? ThrottleTime;
95
96 internal long CurrentFrameThrottle
97 {
98 get
99 {
100 var ticks = CurrentFrameDurationSeconds * HighResTimer.TicksPerSecond;
101 return !double.IsFinite(ticks) || ticks < 1 || ticks >= long.MaxValue ? 0 : (long)ticks;
102 }
103 }
104
108 [JsonProperty]
109 public double ThrottleTime
110 {
111 get { return _throttleTime; }
112 set
113 {
114 _throttle = (long)(value * (double)HighResTimer.TicksPerSecond);
115 _throttleTime = value;
116 }
117 }
118
122 [JsonIgnore]
123 public double TotalCycleTime
124 {
125 get
126 {
127 if (Enumerable.Range(0, Sequence.FrameCount).Any(i => Sequence.GetDurationSeconds(i).HasValue))
128 {
129 double total = 0;
130
131 for (int i = 0; i < Sequence.FrameCount; i++)
132 {
133 double duration = Sequence.GetDurationSeconds(i) ?? ThrottleTime;
134 total += duration;
135
136 if (Sequence.SequenceCycleType == CycleType.PingPong && i > 0 && i < Sequence.FrameCount - 1)
137 total += duration;
138 }
139
140 return total;
141 }
142
143 switch (Sequence.SequenceCycleType)
144 {
145 case CycleType.Simple:
146 // -1 since first frame is played right away
147 return ThrottleTime * (double)(Sequence.FrameCount - 1);
148
149 case CycleType.Repeating:
150 return ThrottleTime * (double)Sequence.FrameCount;
151
152 case CycleType.PingPong:
153 // -2, since:
154 // "C" is only shown once, and...
155 // second "A" is actually part of next cycle repetition
156 return ThrottleTime * (double)((Sequence.FrameCount * 2) - 2);
157
158 default:
159 return 0;
160 }
161 }
162 }
163
167 [JsonProperty]
168 public Cycle NextCycle { get; set; }
169
170 #endregion public properties
171
172 #region ICloneable Members
173
178 public object Clone()
179 {
180 return new Cycle(this);
181 }
182
183 #endregion ICloneable Members
184
185 #region IDisposable Members
186
190 public void Dispose()
191 {
192 GC.SuppressFinalize(this);
193
194 foreach (Sprite sprite in SpriteManager.Instance._spriteList)
195 {
196 if (sprite.TileAnimator.CurrentCycle == this)
197 sprite.TileAnimator.CurrentCycle = null;
198 }
199
200 Cycle._cycles.Remove(CycleKey);
201 }
202
203 #endregion IDisposable Members
204
205 #region static members
206
207 internal static readonly Dictionary<string, Cycle> _cycles = new(StringComparer.OrdinalIgnoreCase);
208
212 public static int Count => _cycles.Count;
213
218 public static List<string> GetAnimationCycleKeys() => new List<string>(_cycles.Keys);
219
224 public static List<Cycle> GetAnimationCycles() => new List<Cycle>(_cycles.Values);
225
231 public static Cycle GetAnimationCycle(string cycleKey)
232 {
233 if (_cycles.ContainsKey(cycleKey))
234 return (Cycle)_cycles[cycleKey].Clone();
235 else
236 return null;
237 }
238
243 public static void ClearAnimationCycle(string cycleKey)
244 {
245 if (_cycles.ContainsKey(cycleKey))
246 _cycles[cycleKey].Dispose();
247 }
248
252 public static void ClearAllAnimationCycles()
253 {
254 var tempCycles = new List<Cycle>(_cycles.Values);
255
256 foreach (Cycle cyc in tempCycles)
257 cyc.Dispose();
258 }
259
260 #endregion static members
261}
References a FrameSequence object, along with a particular Throttle value for animating through Frame...
Definition Cycle.cs:15
static void ClearAllAnimationCycles()
Removes and disposes all registered animation cycles.
Definition Cycle.cs:252
double TotalCycleTime
Returns the total time in seconds for the Cycle.
Definition Cycle.cs:124
static int Count
Gets the total number of animation cycles currently registered.
Definition Cycle.cs:212
double ThrottleTime
Gets or sets the time in seconds between frame transitions in the animation cycle.
Definition Cycle.cs:110
object Clone()
Creates a shallow copy of the current Cycle instance.
Definition Cycle.cs:178
double CurrentFrameDurationSeconds
Effective duration of the currently displayed frame.
Definition Cycle.cs:93
Cycle NextCycle
Gets or sets the next cycle to transition to when this cycle completes.
Definition Cycle.cs:168
static void ClearAnimationCycle(string cycleKey)
Removes and disposes the animation cycle with the specified key.
Definition Cycle.cs:243
Cycle(FrameSequence sequence, double throttleTime, string cycleKey, bool hideTileOnCycleEnd=false)
Initializes a new instance of the Cycle class.
Definition Cycle.cs:48
static Cycle GetAnimationCycle(string cycleKey)
Retrieves a clone of the animation cycle with the specified key.
Definition Cycle.cs:231
void Dispose()
Releases all resources used by the Cycle and removes it from the static cycle collection.
Definition Cycle.cs:190
readonly bool HideTileOnCycleEnd
Indicates whether the tile should be hidden when the cycle completes.
Definition Cycle.cs:33
FrameSequence Sequence
The frame sequence used by this animation cycle.
Definition Cycle.cs:23
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:28
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:21
virtual Animator TileAnimator
Gets the animator associated with this tile.
Definition Tile.cs:227
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...