PageRenderTime 48ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/MangoLander/MangoLander/Graphics/AnimatedSprite.cs

https://bitbucket.org/loveduckie/mangolander
C# | 121 lines | 96 code | 20 blank | 5 comment | 12 complexity | 05a9fddae4fca5da3466b18235cd4c72 MD5 | raw file
  1. using Microsoft.Xna.Framework;
  2. using Microsoft.Xna.Framework.Graphics;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Linq;
  6. using System.Text;
  7. namespace MangoLander.Graphics
  8. {
  9. class AnimatedSprite : IUpdateable
  10. {
  11. public Texture2D Texture { get; private set; }
  12. public int FrameWidth { get; set; }
  13. public int FrameHeight { get; set; }
  14. public int CurrentFrame { get; private set; }
  15. public int TotalFrames { get; private set; }
  16. public double FrameDelay { get; set; }
  17. public bool LoopAnimation { get; set; }
  18. private bool _initialized = false;
  19. private double _elapsedSinceLastFrame = 0;
  20. private bool _playing = false;
  21. public AnimatedSprite()
  22. {
  23. }
  24. public AnimatedSprite(Texture2D texture, int frameWidth, int frameHeight, double frameDelay, bool loopAnimation)
  25. {
  26. Initialize(texture, frameWidth, frameHeight, frameDelay, loopAnimation);
  27. }
  28. public void Initialize(Texture2D texture, int frameWidth, int frameHeight, double frameDelay, bool loopAnimation)
  29. {
  30. this.Texture = texture;
  31. this.FrameWidth = frameWidth;
  32. this.FrameHeight = frameHeight;
  33. this.FrameDelay = frameDelay;
  34. this.LoopAnimation = loopAnimation;
  35. this.CurrentFrame = 0;
  36. if (this.FrameWidth > this.Texture.Width || this.FrameHeight > this.Texture.Height)
  37. {
  38. // Illegal texture and frame height/width specification
  39. _initialized = false;
  40. return;
  41. }
  42. this.TotalFrames = this.Texture.Width / this.FrameWidth;
  43. _initialized = true;
  44. }
  45. private Rectangle GetCurrentFrameRectangle()
  46. {
  47. return new Rectangle(this.FrameWidth * this.CurrentFrame, 0, this.FrameWidth, this.FrameHeight);
  48. }
  49. // Control methods
  50. public void Play()
  51. {
  52. this.CurrentFrame = 0;
  53. _playing = true;
  54. _elapsedSinceLastFrame = 0;
  55. }
  56. public void Stop()
  57. {
  58. _playing = false;
  59. _elapsedSinceLastFrame = 0;
  60. }
  61. public void Reset()
  62. {
  63. this.CurrentFrame = 0;
  64. _elapsedSinceLastFrame = 0;
  65. }
  66. public void Draw(SpriteBatch spriteBatch, Rectangle destinationRectangle, Color color, float rotation, Vector2 origin, SpriteEffects effects, float layerDepth)
  67. {
  68. if (!_initialized)
  69. return;
  70. spriteBatch.Draw(this.Texture, destinationRectangle, this.GetCurrentFrameRectangle(), color, rotation, origin, effects, layerDepth);
  71. }
  72. public void Update(GameTime gameTime)
  73. {
  74. if (!_initialized || !_playing || this.FrameDelay <= 0 || this.TotalFrames == 0)
  75. return;
  76. double currentFrameTime = gameTime.ElapsedGameTime.TotalSeconds;
  77. _elapsedSinceLastFrame += currentFrameTime;
  78. while (_elapsedSinceLastFrame > this.FrameDelay)
  79. {
  80. _elapsedSinceLastFrame -= this.FrameDelay;
  81. this.CurrentFrame++;
  82. // Are we at the end?
  83. if (this.CurrentFrame == this.TotalFrames)
  84. {
  85. if (this.LoopAnimation)
  86. {
  87. // Loop back to the start
  88. this.CurrentFrame = 0;
  89. }
  90. else
  91. {
  92. // Stop playing
  93. _playing = false;
  94. _elapsedSinceLastFrame = 0;
  95. break;
  96. }
  97. }
  98. }
  99. }
  100. }
  101. }