FlipGameComponentAnimation.cs 3.1 KB

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