PageRenderTime 64ms CodeModel.GetById 37ms RepoModel.GetById 0ms app.codeStats 0ms

/Pitch/Pitch/AnimationStrip.cs

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