ScaleGameComponentAnimation.cs 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. //-----------------------------------------------------------------------------
  2. // ScaleGameComponentAnimation.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. using System.Diagnostics;
  13. namespace CardsFramework
  14. {
  15. /// <summary>
  16. /// An animation which scales a component.
  17. /// </summary>
  18. public class ScaleGameComponentAnimation : AnimatedGameComponentAnimation
  19. {
  20. float percent = 0;
  21. float beginFactor;
  22. float factorDelta;
  23. /// <summary>
  24. /// Initializes a new instance of the class.
  25. /// </summary>
  26. /// <param name="beginFactor">The initial scale factor.</param>
  27. /// <param name="endFactor">The eventual scale factor.</param>
  28. public ScaleGameComponentAnimation(float beginFactor, float endFactor)
  29. {
  30. this.beginFactor = beginFactor;
  31. factorDelta = endFactor - beginFactor;
  32. }
  33. /// <summary>
  34. /// Runs the scaling animation.
  35. /// </summary>
  36. /// <param name="gameTime">Game time information.</param>
  37. public override void Run(GameTime gameTime)
  38. {
  39. Texture2D texture;
  40. if (IsStarted())
  41. {
  42. texture = Component.CurrentFrame;
  43. if (texture != null)
  44. {
  45. // Calculate the completion percent of animation
  46. percent += (float)(gameTime.ElapsedGameTime.TotalSeconds /
  47. Duration.TotalSeconds);
  48. // Inflate the component with an increasing delta. The eventual
  49. // delta will have the componenet scale to the specified target
  50. // scaling factor.
  51. Rectangle bounds = texture.Bounds;
  52. bounds.X = (int)Component.CurrentPosition.X;
  53. bounds.Y = (int)Component.CurrentPosition.Y;
  54. float currentFactor = beginFactor + factorDelta * percent - 1;
  55. bounds.Inflate((int)(bounds.Width * currentFactor),
  56. (int)(bounds.Height * currentFactor));
  57. Component.CurrentDestination = bounds;
  58. }
  59. }
  60. }
  61. }
  62. }