AnimationManager.cs 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  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. using System.Threading.Tasks;
  8. using Tutorial011.Models;
  9. namespace Tutorial011.Managers
  10. {
  11. public class AnimationManager
  12. {
  13. private Animation _animation;
  14. private float _timer;
  15. public Vector2 Position { get; set; }
  16. public AnimationManager(Animation animation)
  17. {
  18. _animation = animation;
  19. }
  20. public void Draw(SpriteBatch spriteBatch)
  21. {
  22. spriteBatch.Draw(_animation.Texture,
  23. Position,
  24. new Rectangle(_animation.CurrentFrame * _animation.FrameWidth,
  25. 0,
  26. _animation.FrameWidth,
  27. _animation.FrameHeight),
  28. Color.White);
  29. }
  30. public void Play(Animation animation)
  31. {
  32. if (_animation == animation)
  33. return;
  34. _animation = animation;
  35. _animation.CurrentFrame = 0;
  36. _timer = 0;
  37. }
  38. public void Stop()
  39. {
  40. _timer = 0f;
  41. _animation.CurrentFrame = 0;
  42. }
  43. public void Update(GameTime gameTime)
  44. {
  45. _timer += (float)gameTime.ElapsedGameTime.TotalSeconds;
  46. if(_timer > _animation.FrameSpeed)
  47. {
  48. _timer = 0f;
  49. _animation.CurrentFrame++;
  50. if (_animation.CurrentFrame >= _animation.FrameCount)
  51. _animation.CurrentFrame = 0;
  52. }
  53. }
  54. }
  55. }