AnimationPlayer.cs 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. #region File Description
  2. //-----------------------------------------------------------------------------
  3. // AnimationPlayer.cs
  4. //
  5. // Microsoft XNA Community Game Platform
  6. // Copyright (C) Microsoft Corporation. All rights reserved.
  7. //-----------------------------------------------------------------------------
  8. #endregion
  9. using System;
  10. using Microsoft.Xna.Framework;
  11. using Microsoft.Xna.Framework.Graphics;
  12. namespace Platformer
  13. {
  14. /// <summary>
  15. /// Controls playback of an Animation.
  16. /// </summary>
  17. struct AnimationPlayer
  18. {
  19. /// <summary>
  20. /// Gets the animation which is currently playing.
  21. /// </summary>
  22. public Animation Animation
  23. {
  24. get { return animation; }
  25. }
  26. Animation animation;
  27. /// <summary>
  28. /// Gets the index of the current frame in the animation.
  29. /// </summary>
  30. public int FrameIndex
  31. {
  32. get { return frameIndex; }
  33. }
  34. int frameIndex;
  35. /// <summary>
  36. /// The amount of time in seconds that the current frame has been shown for.
  37. /// </summary>
  38. private float time;
  39. /// <summary>
  40. /// Gets a texture origin at the bottom center of each frame.
  41. /// </summary>
  42. public Vector2 Origin
  43. {
  44. get { return new Vector2(Animation.FrameWidth / 2.0f, Animation.FrameHeight); }
  45. }
  46. /// <summary>
  47. /// Begins or continues playback of an animation.
  48. /// </summary>
  49. public void PlayAnimation(Animation animation)
  50. {
  51. // If this animation is already running, do not restart it.
  52. if (Animation == animation)
  53. return;
  54. // Start the new animation.
  55. this.animation = animation;
  56. this.frameIndex = 0;
  57. this.time = 0.0f;
  58. }
  59. /// <summary>
  60. /// Advances the time position and draws the current frame of the animation.
  61. /// </summary>
  62. public void Draw(GameTime gameTime, SpriteBatch spriteBatch, Vector2 position, SpriteEffects spriteEffects)
  63. {
  64. if (Animation == null)
  65. throw new NotSupportedException("No animation is currently playing.");
  66. // Process passing time.
  67. time += (float)gameTime.ElapsedGameTime.TotalSeconds;
  68. while (time > Animation.FrameTime)
  69. {
  70. time -= Animation.FrameTime;
  71. // Advance the frame index; looping or clamping as appropriate.
  72. if (Animation.IsLooping)
  73. {
  74. frameIndex = (frameIndex + 1) % Animation.FrameCount;
  75. }
  76. else
  77. {
  78. frameIndex = Math.Min(frameIndex + 1, Animation.FrameCount - 1);
  79. }
  80. }
  81. // Calculate the source rectangle of the current frame.
  82. Rectangle source = new Rectangle(FrameIndex * Animation.Texture.Height, 0, Animation.Texture.Height, Animation.Texture.Height);
  83. // Draw the current frame.
  84. spriteBatch.Draw(Animation.Texture, position, source, Color.White, 0.0f, Origin, 1.0f, spriteEffects, 0.0f);
  85. }
  86. }
  87. }