AnimatedSprite.cs 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. // Copyright (c) Craftwork Games. All rights reserved.
  2. // Licensed under the MIT license.
  3. // See LICENSE file in the project root for full license information.
  4. using System;
  5. using System.Collections.Generic;
  6. using Microsoft.Xna.Framework;
  7. using MonoGame.Extended.Animations;
  8. namespace MonoGame.Extended.Graphics;
  9. /// <summary>
  10. /// Represents an animated sprite that can play, pause, and update animations.
  11. /// </summary>
  12. public class AnimatedSprite : Sprite
  13. {
  14. private readonly SpriteSheet _spriteSheet;
  15. private IAnimation _animation;
  16. private readonly Texture2DRegion[] _regions;
  17. /// <summary>
  18. /// Gets the animation controller used to control the current animation of this animated sprite.
  19. /// </summary>
  20. public IAnimationController Controller { get; private set; }
  21. /// <summary>
  22. /// Gets the name of the current animation playing.
  23. /// </summary>
  24. public string CurrentAnimation => _animation.Name;
  25. /// <summary>
  26. /// Initializes a new instance of the <see cref="AnimatedSprite"/> class with the specified
  27. /// <see cref="SpriteSheet"/>.
  28. /// </summary>
  29. /// <param name="spriteSheet">The <see cref="SpriteSheet"/> that contains the animations.</param>
  30. public AnimatedSprite(SpriteSheet spriteSheet)
  31. : base(spriteSheet.TextureAtlas[0])
  32. {
  33. ArgumentNullException.ThrowIfNull(spriteSheet);
  34. _spriteSheet = spriteSheet;
  35. }
  36. /// <summary>
  37. /// Initializes a new instance of the <see cref="AnimatedSprite"/> class with the specified
  38. /// <see cref="SpriteSheet"/>.
  39. /// </summary>
  40. /// <param name="spriteSheet">The <see cref="SpriteSheet"/> that contains the animations.</param>
  41. /// <param name="initialAnimation">The initial animation to play</param>
  42. public AnimatedSprite(SpriteSheet spriteSheet, string initialAnimation) :this(spriteSheet)
  43. {
  44. _animation = spriteSheet.GetAnimation(initialAnimation);
  45. Controller = new AnimationController(_animation);
  46. TextureRegion = _spriteSheet.TextureAtlas[Controller.CurrentFrame];
  47. }
  48. /// <summary>
  49. /// Sets the animation to use for this animated sprite.
  50. /// </summary>
  51. /// <param name="name">The name of the animation.</param>
  52. /// <returns>The <see cref="IAnimationController"/> of the animation.</returns>
  53. /// <exception cref="KeyNotFoundException">
  54. /// Thrown if the source spritesheet does not contain an animation a name that matches the <paramref name="name"/> parameter.
  55. /// </exception>
  56. public IAnimationController SetAnimation(string name)
  57. {
  58. _animation = _spriteSheet.GetAnimation(name);
  59. Controller = new AnimationController(_animation);
  60. TextureRegion = _spriteSheet.TextureAtlas[Controller.CurrentFrame];
  61. return Controller;
  62. }
  63. /// <inheritdoc />
  64. public void Update(GameTime gameTime)
  65. {
  66. Update(gameTime.ElapsedGameTime);
  67. }
  68. /// <inheritdoc />
  69. public void Update(TimeSpan elapsedTime)
  70. {
  71. int index = Controller.CurrentFrame;
  72. Controller.Update(elapsedTime);
  73. // If the current frame changed during the update, change the texture region
  74. if (index != Controller.CurrentFrame)
  75. {
  76. TextureRegion = _spriteSheet.TextureAtlas[Controller.CurrentFrame];
  77. }
  78. }
  79. }