Animation.cs 4.6 KB

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