RotationGameComponentAnimation.cs 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. //-----------------------------------------------------------------------------
  2. // RotationGameComponentAnimation.cs
  3. //
  4. // Animation which rotates a component over time
  5. //-----------------------------------------------------------------------------
  6. using System;
  7. using Microsoft.Xna.Framework;
  8. namespace CardsFramework
  9. {
  10. /// <summary>
  11. /// An animation which rotates a component from one angle to another.
  12. /// </summary>
  13. public class RotationGameComponentAnimation : AnimatedGameComponentAnimation
  14. {
  15. private float percent = 0;
  16. private float beginRotation;
  17. private float rotationDelta;
  18. /// <summary>
  19. /// Initializes a new instance of the class.
  20. /// </summary>
  21. /// <param name="beginRotation">The initial rotation angle in radians.</param>
  22. /// <param name="endRotation">The eventual rotation angle in radians.</param>
  23. public RotationGameComponentAnimation(float beginRotation, float endRotation)
  24. {
  25. this.beginRotation = beginRotation;
  26. rotationDelta = endRotation - beginRotation;
  27. }
  28. /// <summary>
  29. /// Runs the rotation animation.
  30. /// </summary>
  31. /// <param name="gameTime">Game time information.</param>
  32. public override void Run(GameTime gameTime)
  33. {
  34. if (IsStarted())
  35. {
  36. // Calculate the completion percent of animation
  37. percent += (float)(gameTime.ElapsedGameTime.TotalSeconds / Duration.TotalSeconds);
  38. percent = MathHelper.Clamp(percent, 0f, 1f);
  39. // Smoothly interpolate rotation
  40. Component.CurrentRotation = beginRotation + rotationDelta * percent;
  41. }
  42. }
  43. }
  44. }