AnimatedSprite.cs 4.0 KB

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