FramesetGameComponentAnimation.cs 3.1 KB

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