FlipGameComponentAnimation.cs 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. #region File Description
  2. //-----------------------------------------------------------------------------
  3. // FlipGameComponentAnimation.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. using Microsoft.Xna.Framework.Graphics;
  15. #endregion
  16. namespace CardsFramework
  17. {
  18. public class FlipGameComponentAnimation : AnimatedGameComponentAnimation
  19. {
  20. #region Fields
  21. protected int percent = 0;
  22. public bool IsFromFaceDownToFaceUp = true;
  23. #endregion
  24. /// <summary>
  25. /// Runs the flip animation, which makes the component appear as if it has
  26. /// been flipped.
  27. /// </summary>
  28. /// <param name="gameTime">Game time information.</param>
  29. public override void Run(GameTime gameTime)
  30. {
  31. Texture2D texture;
  32. if (IsStarted())
  33. {
  34. if (IsDone())
  35. {
  36. // Finish tha animation
  37. Component.IsFaceDown = !IsFromFaceDownToFaceUp;
  38. Component.CurrentDestination = null;
  39. }
  40. else
  41. {
  42. texture = Component.CurrentFrame;
  43. if (texture != null)
  44. {
  45. // Calculate the completion percent of the animation
  46. percent += (int)(((gameTime.ElapsedGameTime.TotalMilliseconds /
  47. (Duration.TotalMilliseconds / AnimationCycles)) * 100));
  48. if (percent >= 100)
  49. {
  50. percent = 0;
  51. }
  52. int currentPercent;
  53. if (percent < 50)
  54. {
  55. // On the first half of the animation the component is
  56. // on its initial size
  57. currentPercent = percent;
  58. Component.IsFaceDown = IsFromFaceDownToFaceUp;
  59. }
  60. else
  61. {
  62. // On the second half of the animation the component
  63. // is flipped
  64. currentPercent = 100 - percent;
  65. Component.IsFaceDown = !IsFromFaceDownToFaceUp;
  66. }
  67. // Shrink and widen the component to look like it is flipping
  68. Component.CurrentDestination =
  69. new Rectangle((int)(Component.CurrentPosition.X +
  70. texture.Width * currentPercent / 100),
  71. (int)Component.CurrentPosition.Y,
  72. (int)(texture.Width - texture.Width *
  73. currentPercent / 100 * 2),
  74. texture.Height);
  75. }
  76. }
  77. }
  78. }
  79. }
  80. }