TransitionGameComponentAnimation.cs 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. #region File Description
  2. //-----------------------------------------------------------------------------
  3. // TransitionGameComponentAnimation.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. /// An animation which moves a component from one point to the other.
  19. /// </summary>
  20. public class TransitionGameComponentAnimation : AnimatedGameComponentAnimation
  21. {
  22. #region Fields
  23. Vector2 sourcePosition;
  24. Vector2 positionDelta;
  25. float percent = 0;
  26. Vector2 destinationPosition;
  27. #endregion
  28. #region Initializations
  29. /// <summary>
  30. /// Initializes a new instance of the class.
  31. /// </summary>
  32. /// <param name="sourcePosition">The source position.</param>
  33. /// <param name="destinationPosition">The destination position.</param>
  34. public TransitionGameComponentAnimation(Vector2 sourcePosition,
  35. Vector2 destinationPosition)
  36. {
  37. this.destinationPosition = destinationPosition;
  38. this.sourcePosition = sourcePosition;
  39. positionDelta = destinationPosition - sourcePosition;
  40. }
  41. #endregion
  42. /// <summary>
  43. /// Runs the transition animation.
  44. /// </summary>
  45. /// <param name="gameTime">Game time information.</param>
  46. public override void Run(GameTime gameTime)
  47. {
  48. if (IsStarted())
  49. {
  50. // Calculate the animation's completion percentage.
  51. percent += (float)(gameTime.ElapsedGameTime.TotalSeconds /
  52. Duration.TotalSeconds);
  53. // Move the component towards the destination as the animation
  54. // progresses
  55. Component.CurrentPosition = sourcePosition + positionDelta * percent;
  56. if (IsDone())
  57. {
  58. Component.CurrentPosition = destinationPosition;
  59. }
  60. }
  61. }
  62. }
  63. }