AnimatedGameComponentAnimation.cs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. #region File Description
  2. //-----------------------------------------------------------------------------
  3. // AnimatedGameComponentAnimation.cs
  4. //
  5. // Microsoft XNA Community Game Platform
  6. // Copyright (C) Microsoft Corporation. All rights reserved.
  7. //-----------------------------------------------------------------------------
  8. #endregion
  9. #region Using Statements
  10. using System;
  11. using System.Collections.Generic;
  12. using System.Text;
  13. using Microsoft.Xna.Framework;
  14. #endregion
  15. namespace CardsFramework
  16. {
  17. /// <summary>
  18. /// Represents an animation that can alter an animated component.
  19. /// </summary>
  20. public class AnimatedGameComponentAnimation
  21. {
  22. #region Fields and Properties
  23. protected TimeSpan Elapsed { get; set; }
  24. public AnimatedGameComponent Component { get; internal set; }
  25. /// <summary>
  26. /// An action to perform before the animation begins.
  27. /// </summary>
  28. public Action<object> PerformBeforeStart;
  29. public object PerformBeforSartArgs { get; set; }
  30. /// <summary>
  31. /// An action to perform once the animation is complete.
  32. /// </summary>
  33. public Action<object> PerformWhenDone;
  34. public object PerformWhenDoneArgs { get; set; }
  35. uint animationCycles = 1;
  36. /// <summary>
  37. /// Sets the amount of cycles to perform for the animation.
  38. /// </summary>
  39. public uint AnimationCycles
  40. {
  41. get
  42. {
  43. return animationCycles;
  44. }
  45. set
  46. {
  47. if (value > 0)
  48. {
  49. animationCycles = value;
  50. }
  51. }
  52. }
  53. public DateTime StartTime { get; set; }
  54. public TimeSpan Duration { get; set; }
  55. /// <summary>
  56. /// Returns the time at which the animation is estimated to end.
  57. /// </summary>
  58. public TimeSpan EstimatedTimeForAnimationCompletion
  59. {
  60. get
  61. {
  62. if (isStarted)
  63. {
  64. return (Duration - Elapsed);
  65. }
  66. else
  67. {
  68. return StartTime - DateTime.Now + Duration;
  69. }
  70. }
  71. }
  72. public bool IsLooped { get; set; }
  73. private bool isDone = false;
  74. private bool isStarted = false;
  75. #endregion
  76. #region Initiaizations
  77. /// <summary>
  78. /// Initializes a new instance of the class. Be default, an animation starts
  79. /// immediately and has a duration of 150 milliseconds.
  80. /// </summary>
  81. public AnimatedGameComponentAnimation()
  82. {
  83. StartTime = DateTime.Now;
  84. Duration = TimeSpan.FromMilliseconds(150);
  85. }
  86. #endregion
  87. /// <summary>
  88. /// Check whether or not the animation is done playing. Looped animations
  89. /// never finish playing.
  90. /// </summary>
  91. /// <returns>Whether or not the animation is done playing</returns>
  92. public bool IsDone()
  93. {
  94. if (!isDone)
  95. {
  96. isDone = !IsLooped && (Elapsed >= Duration);
  97. if (isDone && PerformWhenDone != null)
  98. {
  99. PerformWhenDone(PerformWhenDoneArgs);
  100. PerformWhenDone = null;
  101. }
  102. }
  103. return isDone;
  104. }
  105. /// <summary>
  106. /// Returns whether or not the animation is started. As a side-effect, starts
  107. /// the animation if it is not started and it is time for it to start.
  108. /// </summary>
  109. /// <returns>Whether or not the animation is started</returns>
  110. public bool IsStarted()
  111. {
  112. if (!isStarted)
  113. {
  114. if (StartTime <= DateTime.Now)
  115. {
  116. if (PerformBeforeStart != null)
  117. {
  118. PerformBeforeStart(PerformBeforSartArgs);
  119. PerformBeforeStart = null;
  120. }
  121. StartTime = DateTime.Now;
  122. isStarted = true;
  123. }
  124. }
  125. return isStarted;
  126. }
  127. /// <summary>
  128. /// Increases the amount of elapsed time as seen by the animation, but only
  129. /// if the animation is started.
  130. /// </summary>
  131. /// <param name="elapsedTime">The timespan by which to incerase the animation's
  132. /// elapsed time.</param>
  133. internal void AccumulateElapsedTime(TimeSpan elapsedTime)
  134. {
  135. if (isStarted)
  136. {
  137. Elapsed += elapsedTime;
  138. }
  139. }
  140. /// <summary>
  141. /// Runs the animation.
  142. /// </summary>
  143. /// <param name="gameTime">Game time information.</param>
  144. public virtual void Run(GameTime gameTime)
  145. {
  146. bool isStarted = IsStarted();
  147. }
  148. }
  149. }