AnimationStrip.cs 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using Microsoft.Xna.Framework;
  6. using Microsoft.Xna.Framework.Graphics;
  7. namespace Gemstone_Hunter
  8. {
  9. public class AnimationStrip
  10. {
  11. #region Declarations
  12. private Texture2D texture;
  13. private int frameWidth;
  14. private int frameHeight;
  15. private float frameTimer = 0f;
  16. private float frameDelay = 0.05f;
  17. private int currentFrame;
  18. private bool loopAnimation = true;
  19. private bool finishedPlaying = false;
  20. private string name;
  21. private string nextAnimation;
  22. #endregion
  23. #region Properties
  24. public int FrameWidth
  25. {
  26. get { return frameWidth; }
  27. set { frameWidth = value; }
  28. }
  29. public int FrameHeight
  30. {
  31. get { return frameHeight; }
  32. set { frameHeight = value; }
  33. }
  34. public Texture2D Texture
  35. {
  36. get { return texture; }
  37. set { texture = value; }
  38. }
  39. public string Name
  40. {
  41. get { return name; }
  42. set { name = value; }
  43. }
  44. public string NextAnimation
  45. {
  46. get { return nextAnimation; }
  47. set { nextAnimation = value; }
  48. }
  49. public bool LoopAnimation
  50. {
  51. get { return loopAnimation; }
  52. set { loopAnimation = value; }
  53. }
  54. public bool FinishedPlaying
  55. {
  56. get { return finishedPlaying; }
  57. }
  58. public int FrameCount
  59. {
  60. get { return texture.Width / frameWidth; }
  61. }
  62. public float FrameLength
  63. {
  64. get { return frameDelay; }
  65. set { frameDelay = value; }
  66. }
  67. public Rectangle FrameRectangle
  68. {
  69. get
  70. {
  71. return new Rectangle(
  72. currentFrame * frameWidth,
  73. 0,
  74. frameWidth,
  75. frameHeight);
  76. }
  77. }
  78. #endregion
  79. #region Constructor
  80. public AnimationStrip(Texture2D texture, int frameWidth, string name)
  81. {
  82. this.texture = texture;
  83. this.frameWidth = frameWidth;
  84. this.frameHeight = texture.Height;
  85. this.name = name;
  86. }
  87. #endregion
  88. #region Public Methods
  89. public void Play()
  90. {
  91. currentFrame = 0;
  92. finishedPlaying = false;
  93. }
  94. public void Update(GameTime gameTime)
  95. {
  96. float elapsed = (float)gameTime.ElapsedGameTime.TotalSeconds;
  97. frameTimer += elapsed;
  98. if (frameTimer >= frameDelay)
  99. {
  100. currentFrame++;
  101. if (currentFrame >= FrameCount)
  102. {
  103. if (loopAnimation)
  104. {
  105. currentFrame = 0;
  106. }
  107. else
  108. {
  109. currentFrame = FrameCount - 1;
  110. finishedPlaying = true;
  111. }
  112. }
  113. frameTimer = 0f;
  114. }
  115. }
  116. #endregion
  117. }
  118. }