AnimatedGameComponentAnimation.cs 4.9 KB

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