1+ using System . Diagnostics ;
12using System . Linq ;
23
34namespace Orbit . Engine ;
@@ -11,19 +12,25 @@ public class Sprite : GameObject
1112 private readonly double imageDisplayDuration ;
1213 private int imageIndex ;
1314 private double elapsedMilliseconds ;
14- private bool isRunning ;
15+
16+ /// <summary>
17+ /// Gets whether the sprite is running.
18+ /// </summary>
19+ public bool IsRunning { get ; private set ; }
1520
1621 /// <summary>
1722 /// Creates a new instance of <see cref="Sprite"/> which accepts the names of images to load.
1823 /// </summary>
1924 /// <param name="imageNames">The names of the images to display, in order of sequence to be displayed.</param>
2025 /// <param name="imageDisplayDuration">How long each image should be displayed for before transitioning to the next image in the sequence.</param>
2126 /// <param name="autoStart">Whether the sprite animation should start automatically.</param>
22- public Sprite ( IReadOnlyList < string > imageNames , double imageDisplayDuration , bool autoStart = true )
27+ /// <param name="playMode">How the sprite will animate.</param>
28+ public Sprite ( IReadOnlyList < string > imageNames , double imageDisplayDuration , bool autoStart = true , PlayModeType playMode = PlayModeType . Loop )
2329 {
2430 this . images = imageNames . Select ( LoadImage ) . ToList ( ) ;
2531 this . imageDisplayDuration = imageDisplayDuration ;
26- this . isRunning = autoStart ;
32+ this . IsRunning = autoStart ;
33+ this . PlayMode = playMode ;
2734 }
2835
2936 /// <summary>
@@ -32,11 +39,13 @@ public Sprite(IReadOnlyList<string> imageNames, double imageDisplayDuration, boo
3239 /// <param name="images">The images to display, in order of sequence to be displayed.</param>
3340 /// <param name="imageDisplayDuration">How long each image should be displayed for before transitioning to the next image in the sequence.</param>
3441 /// <param name="autoStart">Whether the sprite animation should start automatically.</param>
35- public Sprite ( IReadOnlyList < Microsoft . Maui . Graphics . IImage > images , double imageDisplayDuration , bool autoStart = true )
42+ /// <param name="playMode">How the sprite will animate.</param>
43+ public Sprite ( IReadOnlyList < Microsoft . Maui . Graphics . IImage > images , double imageDisplayDuration , bool autoStart = true , PlayModeType playMode = PlayModeType . Loop )
3644 {
3745 this . images = images ;
3846 this . imageDisplayDuration = imageDisplayDuration ;
39- this . isRunning = autoStart ;
47+ this . IsRunning = autoStart ;
48+ this . PlayMode = playMode ;
4049 }
4150
4251 /// <inheritdoc />
@@ -52,7 +61,7 @@ public override void Update(double millisecondsSinceLastUpdate)
5261 {
5362 base . Update ( millisecondsSinceLastUpdate ) ;
5463
55- if ( this . isRunning is false )
64+ if ( this . IsRunning is false )
5665 {
5766 return ;
5867 }
@@ -67,6 +76,16 @@ public override void Update(double millisecondsSinceLastUpdate)
6776 // - Loop
6877 // - Reverse
6978 imageIndex = Math . Clamp ( imageIndex + 1 , 0 , images . Count ) ;
79+
80+ if ( imageIndex == images . Count )
81+ {
82+ imageIndex = 0 ;
83+
84+ if ( PlayMode == PlayModeType . Single )
85+ {
86+ Stop ( ) ;
87+ }
88+ }
7089 }
7190 }
7291
@@ -77,22 +96,48 @@ public void Stop()
7796 {
7897 elapsedMilliseconds = 0 ;
7998 imageIndex = 0 ;
80- this . isRunning = false ;
99+ this . IsRunning = false ;
81100 }
82101
83102 /// <summary>
84103 /// Pauses the sprite animation.
85104 /// </summary>
86105 public void Pause ( )
87106 {
88- this . isRunning = false ;
107+ this . IsRunning = false ;
89108 }
90109
91110 /// <summary>
92111 /// Starts the sprite animation.
93112 /// </summary>
94113 public void Start ( )
95114 {
96- this . isRunning = true ;
115+ this . IsRunning = true ;
116+ }
117+
118+ /// <summary>
119+ /// Gets the <see cref="PlayMode"/> determining how the sprite will animate.
120+ /// </summary>
121+ public PlayModeType PlayMode { get ; }
122+
123+ /// <summary>
124+ /// Gets the mode in which a sprite will animate.
125+ /// </summary>
126+ public enum PlayModeType
127+ {
128+ /// <summary>
129+ /// The sprite will animate through the full sequence once and then stop.
130+ /// </summary>
131+ Single ,
132+
133+ /// <summary>
134+ /// The sprite will animate indefinitely.
135+ /// </summary>
136+ Loop ,
137+
138+ /// <summary>
139+ /// The sprite will animate forwards and then backwards through the sequence.
140+ /// </summary>
141+ Reverse
97142 }
98143}
0 commit comments