Animation.cs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. #region File Information
  2. //-----------------------------------------------------------------------------
  3. // Animation.cs
  4. //
  5. // Microsoft XNA Community Game Platform
  6. // Copyright (C) Microsoft Corporation. All rights reserved.
  7. //-----------------------------------------------------------------------------
  8. #endregion
  9. #region Using Statements
  10. using System;
  11. using System.Collections.Generic;
  12. using System.Linq;
  13. using System.Text;
  14. using Microsoft.Xna.Framework;
  15. using Microsoft.Xna.Framework.Graphics;
  16. #endregion
  17. namespace Graphics3DSample
  18. {
  19. class Animation
  20. {
  21. #region Fields
  22. // The texture with animation frames
  23. Texture2D animationTexture;
  24. // The size and structure of whole frames sheet in animationTexture. The animationTexture could
  25. // hold animaton sequence organized in multiple rows and multiple columns, that's why animation
  26. // engine should know how the frames are organized inside a frames sheet
  27. Point sheetSize;
  28. // Amount of time between frames
  29. TimeSpan frameInterval;
  30. // Time passed since last frame
  31. TimeSpan nextFrame;
  32. // Current frame in the animation sequence
  33. public Point currentFrame;
  34. // The size of single frame inside the animationTexture
  35. public Point frameSize;
  36. #endregion
  37. #region Initialization
  38. /// <summary>
  39. /// Constructor of an animation class
  40. /// </summary>
  41. /// <param name="frameSheet">Texture with animation frames sheet</param>
  42. /// <param name="size">Single frame size</param>
  43. /// <param name="frameSheetSize">The whole frame sheet size</param>
  44. /// <param name="interval">Interval between progressing to the next frame</param>
  45. public Animation(Texture2D frameSheet, Point size, Point frameSheetSize, TimeSpan interval)
  46. {
  47. animationTexture = frameSheet;
  48. frameSize = size;
  49. sheetSize = frameSheetSize;
  50. frameInterval = interval;
  51. }
  52. #endregion
  53. #region Update and Render
  54. /// <summary>
  55. /// Updates the animaton progress
  56. /// </summary>
  57. /// <param name="gameTime"></param>
  58. /// <param name="progressed">Returns true if animation were progressed; in such case
  59. /// caller could updated the position of the animated character</param>
  60. public bool Update(GameTime gameTime)
  61. {
  62. bool progressed;
  63. // Check is it is a time to progress to the next frame
  64. if (nextFrame >= frameInterval)
  65. {
  66. // Progress to the next frame in the row
  67. currentFrame.X++;
  68. // If reached end of the row advance to the next row
  69. // and start form the first frame there
  70. if (currentFrame.X >= sheetSize.X)
  71. {
  72. currentFrame.X = 0;
  73. currentFrame.Y++;
  74. }
  75. // If reached last row in the frame sheet jump to the first row again - produce endless loop
  76. if (currentFrame.Y >= sheetSize.Y)
  77. currentFrame.Y = 0;
  78. // Reset interval for next frame
  79. progressed = true;
  80. nextFrame = TimeSpan.Zero;
  81. }
  82. else
  83. {
  84. // Wait for the next frame
  85. nextFrame += gameTime.ElapsedGameTime;
  86. progressed = false;
  87. }
  88. return progressed;
  89. }
  90. /// <summary>
  91. /// Rendering of the animation
  92. /// </summary>
  93. /// <param name="spriteBatch">SpriteBatch in which current frame will be rendered</param>
  94. /// <param name="position">The position of current frame</param>
  95. /// <param name="spriteEffect">SpriteEffect to apply on current frame</param>
  96. public void Draw(SpriteBatch spriteBatch, Vector2 position, SpriteEffects spriteEffect)
  97. {
  98. Draw(spriteBatch, position, 1.0f, spriteEffect);
  99. }
  100. /// <summary>
  101. /// Rendering of the animation
  102. /// </summary>
  103. /// <param name="spriteBatch">SpriteBatch in which current frame will be rendered</param>
  104. /// <param name="position">The position of the current frame</param>
  105. /// <param name="scale">Scale factor to apply on the current frame</param>
  106. /// <param name="spriteEffect">SpriteEffect to apply on the current frame</param>
  107. public void Draw(SpriteBatch spriteBatch, Vector2 position, float scale, SpriteEffects spriteEffect)
  108. {
  109. spriteBatch.Draw(animationTexture, position, new Rectangle(
  110. frameSize.X * currentFrame.X,
  111. frameSize.Y * currentFrame.Y,
  112. frameSize.X,
  113. frameSize.Y),
  114. Color.White, 0f, Vector2.Zero, scale, spriteEffect, 0);
  115. }
  116. #endregion
  117. }
  118. }