PageRenderTime 49ms CodeModel.GetById 26ms RepoModel.GetById 0ms app.codeStats 0ms

/TamagotchiGame/TamagotchiGame/Common/SpriteAnimation.cs

https://bitbucket.org/hasyimi/tamagotchi
C# | 212 lines | 127 code | 37 blank | 48 comment | 22 complexity | e7d81a2dcedeee83e665e41e23aa7abc MD5 | raw file
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using Microsoft.Xna.Framework;
  6. using Microsoft.Xna.Framework.Graphics;
  7. namespace TamagotchiGame.Common
  8. {
  9. public enum AnimationType
  10. {
  11. Looping,
  12. NonLooping,
  13. Pendulum
  14. }
  15. /// <summary>
  16. /// A concrete class of a SpriteAnimation, implementing the ISpriteAnimation interface.
  17. /// SpriteAnimation is a wrapper class for a single-row sprite sheet animation.
  18. /// It has supports for non-looping and looping animations.
  19. /// </summary>
  20. public class SpriteAnimation
  21. {
  22. #region Fields
  23. private int framesCount, currentFrame;
  24. private float elapsedTime;
  25. private Texture2D texture;
  26. private bool isReversed;
  27. #endregion
  28. #region Properties
  29. /// <summary>
  30. /// The number of frames for this animation.
  31. /// </summary>
  32. public int FramesCount { get { return framesCount; } }
  33. /// <summary>
  34. /// The current frame of this animation.
  35. /// </summary>
  36. public int CurrentFrame { get { return currentFrame; } }
  37. /// <summary>
  38. /// The width of a single frame.
  39. /// </summary>
  40. public int FrameWidth { get; set; }
  41. /// <summary>
  42. /// The height of the sprite sheet.
  43. /// </summary>
  44. public int FrameHeight { get; set; }
  45. /// <summary>
  46. /// The time delay for displaying each frame.
  47. /// </summary>
  48. public TimeSpan FrameDelay { get; set; }
  49. public TimeSpan TotalDelay
  50. {
  51. get { return TimeSpan.FromSeconds( FrameDelay.TotalSeconds * FramesCount ); }
  52. set
  53. {
  54. FrameDelay = TimeSpan.FromSeconds(value.TotalSeconds / FramesCount);
  55. }
  56. }
  57. /// <summary>
  58. /// The sprite sheet.
  59. /// </summary>
  60. public Texture2D Texture { get { return texture; } }
  61. /// <summary>
  62. /// The animation type. Either looping or non-looping.
  63. /// </summary>
  64. public AnimationType CurrentAnimationType { get; set; }
  65. /// <summary>
  66. /// Is the animation paused?
  67. /// </summary>
  68. public bool IsPaused { get; set; }
  69. #endregion
  70. /// <summary>
  71. /// The SpriteAnimation constructor.
  72. /// </summary>
  73. /// <param name="texture">The sprite sheet.</param>
  74. /// <param name="frameWidth">The width of a single frame.</param>
  75. /// <param name="frameDelay">The delay for playing each frame.</param>
  76. public SpriteAnimation(Texture2D texture, int frameWidth, TimeSpan frameDelay)
  77. {
  78. framesCount = texture.Width / frameWidth;
  79. FrameWidth = frameWidth;
  80. FrameHeight = texture.Height;
  81. FrameDelay = frameDelay;
  82. this.texture = texture;
  83. isReversed = false;
  84. }
  85. /// <summary>
  86. /// Updates the SpriteAnimation. In other words, this function is for playing the animation.
  87. /// </summary>
  88. /// <param name="gameTime">Provides a snapshot of timing information for the current frame.</param>
  89. public void Update(GameTime gameTime)
  90. {
  91. if (!IsPaused)
  92. {
  93. elapsedTime += (float)gameTime.ElapsedGameTime.TotalSeconds;
  94. if (elapsedTime >= FrameDelay.TotalSeconds)
  95. {
  96. if (CurrentAnimationType == AnimationType.NonLooping)
  97. {
  98. if (currentFrame + 1 < framesCount)
  99. currentFrame++;
  100. }
  101. else if (CurrentAnimationType == AnimationType.Looping)
  102. {
  103. currentFrame = (currentFrame + 1) % framesCount;
  104. }
  105. else if (CurrentAnimationType == AnimationType.Pendulum)
  106. {
  107. if (isReversed)
  108. {
  109. currentFrame = (currentFrame - 1) % framesCount;
  110. if (currentFrame == 0)
  111. {
  112. IsPaused = true;
  113. isReversed = false;
  114. }
  115. }
  116. else
  117. {
  118. currentFrame = (currentFrame + 1) % framesCount;
  119. if (currentFrame + 1 == framesCount)
  120. {
  121. IsPaused = true;
  122. isReversed = true;
  123. }
  124. }
  125. }
  126. elapsedTime = 0.0f;
  127. }
  128. }
  129. }
  130. /// <summary>
  131. /// Displays the animation on the screen.
  132. /// </summary>
  133. /// <param name="spriteBatch">An instance of SpriteBatch.</param>
  134. /// <param name="position">The position where the frame will be displayed.</param>
  135. /// <param name="isFlipped">Is the sprite flipped?</param>
  136. public void Draw(SpriteBatch spriteBatch, Vector2 position, bool isFlipped, float depth)
  137. {
  138. SpriteEffects spriteEffects;
  139. if (isFlipped)
  140. spriteEffects = SpriteEffects.FlipHorizontally;
  141. else spriteEffects = SpriteEffects.None;
  142. spriteBatch.Draw(texture, position,
  143. new Rectangle(currentFrame * FrameWidth, 0, FrameWidth, FrameHeight),
  144. Color.White, 0.0f, Vector2.Zero, 1.0f, spriteEffects, depth);
  145. }
  146. public void Draw(SpriteBatch spriteBatch, Vector2 position, bool isFlipped, float depth, Color color)
  147. {
  148. SpriteEffects spriteEffects;
  149. if (isFlipped)
  150. spriteEffects = SpriteEffects.FlipHorizontally;
  151. else spriteEffects = SpriteEffects.None;
  152. spriteBatch.Draw(texture, position,
  153. new Rectangle(currentFrame * FrameWidth, 0, FrameWidth, FrameHeight),
  154. color, 0.0f, Vector2.Zero, 1.0f, spriteEffects, depth);
  155. }
  156. public void Draw(SpriteBatch spriteBatch, Vector2 position, bool isFlipped, float depth, byte alpha)
  157. {
  158. SpriteEffects spriteEffects;
  159. if (isFlipped)
  160. spriteEffects = SpriteEffects.FlipHorizontally;
  161. else spriteEffects = SpriteEffects.None;
  162. spriteBatch.Draw(texture, position,
  163. new Rectangle(currentFrame * FrameWidth, 0, FrameWidth, FrameHeight),
  164. new Color(255, 255, 255, alpha), 0.0f, Vector2.Zero, 1.0f, spriteEffects, depth);
  165. }
  166. /// <summary>
  167. /// Resets the animation, setting its current frame to 0.
  168. /// </summary>
  169. public void Reset()
  170. {
  171. currentFrame = 0;
  172. elapsedTime = 0.0f;
  173. }
  174. }
  175. }