FramesetGameComponentAnimation.cs 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. //-----------------------------------------------------------------------------
  2. // FramesetGameComponentAnimation.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.Graphics;
  11. using Microsoft.Xna.Framework;
  12. namespace CardsFramework
  13. {
  14. /// <summary>
  15. /// A "typical" animation that consists of alternating between a set of frames.
  16. /// </summary>
  17. public class FramesetGameComponentAnimation : AnimatedGameComponentAnimation
  18. {
  19. Texture2D framesTexture;
  20. int numberOfFrames;
  21. int numberOfFramePerRow;
  22. Vector2 frameSize;
  23. private double percent = 0;
  24. /// <summary>
  25. /// Creates a new instance of the class.
  26. /// </summary>
  27. /// <param name="framesTexture">The frames texture (animation sheet).</param>
  28. /// <param name="numberOfFrames">The number of frames in the sheet.</param>
  29. /// <param name="numberOfFramePerRow">The number of frame per row.</param>
  30. /// <param name="frameSize">Size of the frame.</param>
  31. public FramesetGameComponentAnimation(Texture2D framesTexture, int numberOfFrames,
  32. int numberOfFramePerRow, Vector2 frameSize)
  33. {
  34. this.framesTexture = framesTexture;
  35. this.numberOfFrames = numberOfFrames;
  36. this.numberOfFramePerRow = numberOfFramePerRow;
  37. this.frameSize = frameSize;
  38. }
  39. /// <summary>
  40. /// Runs the frame set animation.
  41. /// </summary>
  42. /// <param name="gameTime">Game time information.</param>
  43. public override void Run(GameTime gameTime)
  44. {
  45. if (IsStarted())
  46. {
  47. // Calculate the completion percent of the animation
  48. percent += (((gameTime.ElapsedGameTime.TotalMilliseconds /
  49. (Duration.TotalMilliseconds / AnimationCycles)) * 100));
  50. if (percent >= 100)
  51. {
  52. percent = 0;
  53. }
  54. // Calculate the current frame index
  55. int animationIndex = (int)(numberOfFrames * percent / 100);
  56. Component.CurrentSegment =
  57. new Rectangle(
  58. (int)frameSize.X * (animationIndex % numberOfFramePerRow),
  59. (int)frameSize.Y * (animationIndex / numberOfFramePerRow),
  60. (int)frameSize.X, (int)frameSize.Y);
  61. Component.CurrentFrame = framesTexture;
  62. }
  63. else
  64. {
  65. Component.CurrentFrame = null;
  66. Component.CurrentSegment = null;
  67. }
  68. base.Run(gameTime);
  69. }
  70. }
  71. }