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