AnimatedSprite.cs 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. //-----------------------------------------------------------------------------
  2. // AnimatedSprite.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 Microsoft.Xna.Framework;
  10. using Microsoft.Xna.Framework.Audio;
  11. using Microsoft.Xna.Framework.Graphics;
  12. using Microsoft.Xna.Framework.Input;
  13. using Microsoft.Xna.Framework.Content;
  14. using Microsoft.Xna.Framework.Media;
  15. namespace TiledSprites
  16. {
  17. public class AnimatedSprite
  18. {
  19. private SpriteSheet sheet;
  20. private Vector2 positionValue;
  21. private Vector2 originValue;
  22. private Vector2 scaleValue;
  23. private float rotationValue;
  24. private int currentFrameValue;
  25. private int numFrames;
  26. public Vector2 Position
  27. {
  28. set
  29. {
  30. positionValue = value;
  31. }
  32. get
  33. {
  34. return positionValue;
  35. }
  36. }
  37. public Vector2 Origin
  38. {
  39. set
  40. {
  41. originValue = value;
  42. }
  43. get
  44. {
  45. return originValue;
  46. }
  47. }
  48. public float Rotation
  49. {
  50. set
  51. {
  52. rotationValue = value;
  53. }
  54. get
  55. {
  56. return rotationValue;
  57. }
  58. }
  59. public Vector2 ScaleValue
  60. {
  61. set
  62. {
  63. scaleValue = value;
  64. }
  65. get
  66. {
  67. return scaleValue;
  68. }
  69. }
  70. public int CurrentFrame
  71. {
  72. set
  73. {
  74. if (value > (numFrames - 1))
  75. {
  76. string message =
  77. string.Format("{0} is an invalid value for CurrentFrame. " +
  78. "Valid values are from 0 to numFrames - 1 ({1})",
  79. value, numFrames - 1);
  80. throw new ArgumentOutOfRangeException("value", message);
  81. }
  82. currentFrameValue = value;
  83. }
  84. get
  85. {
  86. return currentFrameValue;
  87. }
  88. }
  89. public AnimatedSprite(SpriteSheet spriteSheet, int frameWidth,
  90. int frameHeight, int padding, int rows, int columns,
  91. Point startFrame, int frames)
  92. {
  93. if (spriteSheet == null)
  94. {
  95. throw new ArgumentNullException("spriteSheet");
  96. }
  97. int spriteAreaHeight = (frameHeight + padding) * rows - padding;
  98. int spriteAreaWidth = (frameWidth + padding) * columns - padding;
  99. //first, make sure the sheet is possible
  100. if ((spriteAreaWidth > spriteSheet.Texture.Width) ||
  101. (spriteAreaHeight > spriteSheet.Texture.Height))
  102. {
  103. throw new ArgumentException(
  104. "The layout specified is too large for the SpriteSheet."
  105. );
  106. }
  107. sheet = spriteSheet;
  108. numFrames = frames;
  109. int startFrameIndex = startFrame.Y * columns + startFrame.X;
  110. //now auto-generate the animation data,
  111. //left to right, top to bottom.
  112. int frameIndex = 0;
  113. for (int i = startFrameIndex; i < (numFrames + startFrameIndex); i++)
  114. {
  115. int x = (i % columns);
  116. int y = (i / columns);
  117. int left = (x * (frameWidth + padding));
  118. int top = (y * (frameHeight + padding));
  119. top = top % spriteAreaHeight;
  120. sheet.AddSourceSprite(frameIndex,
  121. new Rectangle(left, top, frameWidth, frameHeight));
  122. frameIndex++;
  123. }
  124. }
  125. public void IncrementAnimationFrame()
  126. {
  127. currentFrameValue = (currentFrameValue + 1) % numFrames;
  128. }
  129. public void Draw(SpriteBatch batch, Color color, BlendState blendMode)
  130. {
  131. batch.Begin(SpriteSortMode.Immediate, blendMode);
  132. batch.Draw(sheet.Texture, positionValue, sheet[currentFrameValue],
  133. color, rotationValue, originValue, scaleValue,
  134. SpriteEffects.None, 0f);
  135. batch.End();
  136. }
  137. }
  138. }