Animation.cs 5.1 KB

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