PageRenderTime 62ms CodeModel.GetById 29ms RepoModel.GetById 0ms app.codeStats 0ms

/GameEngine/Drawing/Animation.cs

https://bitbucket.org/begedin/tanked
C# | 90 lines | 39 code | 9 blank | 42 comment | 1 complexity | 7849cebc96447a30c64165dc0ef4f957 MD5 | raw file
  1. using System;
  2. using Microsoft.Xna.Framework.Graphics;
  3. using Microsoft.Xna.Framework;
  4. namespace GameEngine.Drawing
  5. {
  6. /// <summary>
  7. /// Animation class that allows the user to specify metrics about animation frames from a spritesheet. Also allows
  8. /// specification of other meta properties such as the Delay between frames, whether the animation should loop
  9. /// and what methods to provide information about current frame information based on the Game Time.
  10. /// </summary>
  11. public class Animation
  12. {
  13. internal const int FRAME_DELAY_DEFAULT = 100;
  14. public Texture2D SpriteSheet { get; set; }
  15. public Rectangle[] Frames { get; set; }
  16. public int FrameDelay { get; set; }
  17. public bool Loop { get; set; }
  18. private double _startTime = 0;
  19. /// <summary>
  20. /// Initialises an Animation object. Specifies a SpriteSheet to use and the individual frame locations
  21. /// within the sheet to use. Optionally, the Delay between Frame changes and whether the animation
  22. /// should loop when complete can be passed as constructor parameters.
  23. /// </summary>
  24. /// <param name="SpriteSheet">Texture2D object that represents the SpriteSheet to use for this animation.</param>
  25. /// <param name="Frames">Array of Rectangle objects that specify the locations in the spritesheet to use as frames.</param>
  26. /// <param name="FrameChange">integer value specifying the amount of time in ms to delay between each frame change. Set to 100 by Default.</param>
  27. /// <param name="Loop">bool value specifying wheter the animation should re-start at the end of the animation frames.</param>
  28. public Animation(Texture2D SpriteSheet, Rectangle[] Frames, int FrameDelay = FRAME_DELAY_DEFAULT, bool Loop = false)
  29. {
  30. this.SpriteSheet = SpriteSheet;
  31. this.Frames = Frames;
  32. this.FrameDelay = FrameDelay;
  33. this.Loop = Loop;
  34. }
  35. /// <summary>
  36. /// Resets the Animation to the first frame. Requires the GameTime as an input
  37. /// parameters so that the animation may know at point in time the game is at.
  38. /// This shouldnt be a problem since most of the logic involved with animations
  39. /// will occur in Draw and Update methods - both of which are passed GameTime
  40. /// parameters.
  41. /// </summary>
  42. /// <param name="GameTime">Current GameTime that the game is at.</param>
  43. public void ResetAnimation(GameTime GameTime)
  44. {
  45. _startTime = GameTime.TotalGameTime.TotalMilliseconds;
  46. }
  47. /// <summary>
  48. /// Returns the current Frame Index as an integer value based on the GameTime
  49. /// parameters passed into this method.
  50. /// </summary>
  51. /// <param name="GameTime">GameTime object representing the current GameTime in the application.</param>
  52. /// <returns>Int index of the current frame in the Frames property.</returns>
  53. public int GetCurrentFrameIndex(GameTime GameTime)
  54. {
  55. int index = (int)((GameTime.TotalGameTime.TotalMilliseconds - _startTime) / FrameDelay);
  56. return (Loop) ? index % Frames.Length : index; //If looping, start from the beginning
  57. }
  58. /// <summary>
  59. /// Specifies whether the Animation has completed. If the Animation is of Looping type, then this
  60. /// method will always return a true. For non-looping animations, this method should return a true
  61. /// once it has passed its last frame. The GameTime parameter is required to determine its current
  62. /// position based on the current GameTime.
  63. /// </summary>
  64. /// <param name="GameTime">GameTime object specifying the current Game Time.</param>
  65. /// <returns>bool value specifying whether the animation has finished.</returns>
  66. public bool IsFinished(GameTime GameTime)
  67. {
  68. return Loop || GetCurrentFrameIndex(GameTime) >= Frames.Length;
  69. }
  70. /// <summary>
  71. /// Returns the Current Frame to show in the sprite sheet based on the current
  72. /// games running time.
  73. /// </summary>
  74. /// <param name="GameTime">GameTime object representing the current state in time of the game.</param>
  75. /// <returns>Rectangle object representing the Frame in the spritesheet to show.</returns>
  76. public Rectangle GetCurrentFrame(GameTime GameTime)
  77. {
  78. return Frames[Math.Min(GetCurrentFrameIndex(GameTime), Frames.Length - 1)];
  79. }
  80. }
  81. }