Animation.cs 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. #region File Description
  2. //-----------------------------------------------------------------------------
  3. // Animation.cs
  4. //
  5. // Microsoft XNA Community Game Platform
  6. // Copyright (C) Microsoft Corporation. All rights reserved.
  7. //-----------------------------------------------------------------------------
  8. #endregion
  9. #region File Information
  10. //-----------------------------------------------------------------------------
  11. // Animation.cs
  12. //
  13. // Microsoft XNA Community Game Platform
  14. // Copyright (C) Microsoft Corporation. All rights reserved.
  15. //-----------------------------------------------------------------------------
  16. #endregion
  17. #region Using Statements
  18. using System;
  19. using System.Collections.Generic;
  20. using System.Linq;
  21. using System.Text;
  22. using Microsoft.Xna.Framework;
  23. using Microsoft.Xna.Framework.Graphics;
  24. #endregion
  25. namespace CatapultGame
  26. {
  27. class Animation
  28. {
  29. #region Fields
  30. // Animation variables
  31. Texture2D animatedCharacter;
  32. Point sheetSize;
  33. Point currentFrame;
  34. public Point FrameSize { get; set; }
  35. public int FrameCount
  36. {
  37. get { return sheetSize.X * sheetSize.Y; }
  38. }
  39. public Vector2 Offset { get; set; }
  40. /// <summary>
  41. /// Returns or sets the current animation frame.
  42. /// </summary>
  43. public int FrameIndex
  44. {
  45. get
  46. {
  47. return sheetSize.X * currentFrame.Y + currentFrame.X;
  48. }
  49. set
  50. {
  51. if (value >= sheetSize.X * sheetSize.Y + 1)
  52. {
  53. throw new InvalidOperationException(
  54. "Specified frame index exeeds available frames");
  55. }
  56. currentFrame.Y = value / sheetSize.X;
  57. currentFrame.X = value % sheetSize.X;
  58. }
  59. }
  60. public bool IsActive { get; private set; }
  61. #endregion
  62. #region Initialization
  63. /// <summary>
  64. /// Constructor of an animation class
  65. /// </summary>
  66. /// <param name="frameSheet">Texture with animation frames sheet</param>
  67. /// <param name="size">Single frame size</param>
  68. /// <param name="frameSheetSize">The whole frame sheet size</param>
  69. /// <param name="interval">Interval between progressing to the next frame</param>
  70. public Animation(Texture2D frameSheet, Point size,
  71. Point frameSheetSize)
  72. {
  73. animatedCharacter = frameSheet;
  74. FrameSize = size;
  75. sheetSize = frameSheetSize;
  76. Offset = Vector2.Zero;
  77. }
  78. #endregion
  79. #region Update and Render
  80. /// <summary>
  81. /// Updates the animaton progress
  82. /// </summary>
  83. public void Update()
  84. {
  85. if (IsActive)
  86. {
  87. if (FrameIndex >= FrameCount - 1)
  88. {
  89. IsActive = false;
  90. FrameIndex = FrameCount - 1; // Stop at last frame
  91. }
  92. else
  93. {
  94. // Remember that updating "currentFrame" will also
  95. // update the FrameIndex property.
  96. currentFrame.X++;
  97. if (currentFrame.X >= sheetSize.X)
  98. {
  99. currentFrame.X = 0;
  100. currentFrame.Y++;
  101. }
  102. if (currentFrame.Y >= sheetSize.Y)
  103. currentFrame.Y = 0;
  104. }
  105. }
  106. }
  107. /// <summary>
  108. /// Rendering of the animation
  109. /// </summary>
  110. /// <param name="spriteBatch">SpriteBatch in which current
  111. /// frame will be rendered</param>
  112. /// <param name="position">The position of current frame</param>
  113. /// <param name="spriteEffect">SpriteEffect to apply on
  114. /// current frame</param>
  115. public void Draw(SpriteBatch spriteBatch, Vector2 position,
  116. SpriteEffects spriteEffect)
  117. {
  118. Draw(spriteBatch, position, 1.0f, spriteEffect);
  119. }
  120. /// <summary>
  121. /// Rendering of the animation
  122. /// </summary>
  123. /// <param name="spriteBatch">SpriteBatch in which current frame
  124. /// will be rendered</param>
  125. /// <param name="position">The position of the current frame</param>
  126. /// <param name="scale">Scale factor to apply on the current frame</param>
  127. /// <param name="spriteEffect">SpriteEffect to apply on the
  128. /// current frame</param>
  129. public void Draw(SpriteBatch spriteBatch, Vector2 position, float scale,
  130. SpriteEffects spriteEffect)
  131. {
  132. spriteBatch.Draw(animatedCharacter, position + Offset, new Rectangle(
  133. FrameSize.X * currentFrame.X,
  134. FrameSize.Y * currentFrame.Y,
  135. FrameSize.X,
  136. FrameSize.Y),
  137. Color.White, 0f, Vector2.Zero, scale, spriteEffect, 0);
  138. }
  139. /// <summary>
  140. /// Causes the animation to start playing from a specified frame index
  141. /// </summary>
  142. /// <param name="frameIndex"></param>
  143. public void PlayFromFrameIndex(int frameIndex)
  144. {
  145. FrameIndex = frameIndex;
  146. IsActive = true;
  147. }
  148. #endregion
  149. }
  150. }