Sprite.cs 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using Microsoft.Xna.Framework.Graphics;
  6. using Microsoft.Xna.Framework;
  7. namespace Robot_Rampage
  8. {
  9. class Sprite
  10. {
  11. #region Declarations
  12. public Texture2D Texture;
  13. private Vector2 worldLocation = Vector2.Zero;
  14. private Vector2 velocity = Vector2.Zero;
  15. private List<Rectangle> frames = new List<Rectangle>();
  16. private int currentFrame;
  17. private float frameTime = 0.1f;
  18. private float timeForCurrentFrame = 0.0f;
  19. private Color tintColor = Color.White;
  20. private float rotation = 0.0f;
  21. public bool Expired = false;
  22. public bool Animate = true;
  23. public bool AnimateWhenStopped = true;
  24. public bool Collidable = true;
  25. public int CollisionRadius = 0;
  26. public int BoundingXPadding = 0;
  27. public int BoundingYPadding = 0;
  28. #endregion
  29. #region Constructors
  30. public Sprite(
  31. Vector2 worldLocation,
  32. Texture2D texture,
  33. Rectangle initialFrame,
  34. Vector2 velocity)
  35. {
  36. this.worldLocation = worldLocation;
  37. Texture = texture;
  38. this.velocity = velocity;
  39. frames.Add(initialFrame);
  40. }
  41. #endregion
  42. #region Drawing and Animation Properties
  43. public int FrameWidth
  44. {
  45. get { return frames[0].Width; }
  46. }
  47. public int FrameHeight
  48. {
  49. get { return frames[0].Height; }
  50. }
  51. public Color TintColor
  52. {
  53. get { return tintColor; }
  54. set { tintColor = value; }
  55. }
  56. public float Rotation
  57. {
  58. get { return rotation; }
  59. set { rotation = value % MathHelper.TwoPi; }
  60. }
  61. public int Frame
  62. {
  63. get { return currentFrame; }
  64. set
  65. {
  66. currentFrame = (int)MathHelper.Clamp(value, 0,
  67. frames.Count - 1);
  68. }
  69. }
  70. public float FrameTime
  71. {
  72. get { return frameTime; }
  73. set { frameTime = MathHelper.Max(0, value); }
  74. }
  75. public Rectangle Source
  76. {
  77. get { return frames[currentFrame]; }
  78. }
  79. #endregion
  80. #region Positional Properties
  81. public Vector2 WorldLocation
  82. {
  83. get { return worldLocation; }
  84. set { worldLocation = value; }
  85. }
  86. public Vector2 ScreenLocation
  87. {
  88. get
  89. {
  90. return Camera.Transform(worldLocation);
  91. }
  92. }
  93. public Vector2 Velocity
  94. {
  95. get { return velocity; }
  96. set { velocity = value; }
  97. }
  98. public Rectangle WorldRectangle
  99. {
  100. get
  101. {
  102. return new Rectangle(
  103. (int)worldLocation.X,
  104. (int)worldLocation.Y,
  105. FrameWidth,
  106. FrameHeight);
  107. }
  108. }
  109. public Rectangle ScreenRectangle
  110. {
  111. get
  112. {
  113. return Camera.Transform(WorldRectangle);
  114. }
  115. }
  116. public Vector2 RelativeCenter
  117. {
  118. get { return new Vector2(FrameWidth / 2, FrameHeight / 2); }
  119. }
  120. public Vector2 WorldCenter
  121. {
  122. get { return worldLocation + RelativeCenter; }
  123. }
  124. public Vector2 ScreenCenter
  125. {
  126. get
  127. {
  128. return Camera.Transform(worldLocation + RelativeCenter);
  129. }
  130. }
  131. #endregion
  132. #region Collision Related Properties
  133. public Rectangle BoundingBoxRect
  134. {
  135. get
  136. {
  137. return new Rectangle(
  138. (int)worldLocation.X + BoundingXPadding,
  139. (int)worldLocation.Y + BoundingYPadding,
  140. FrameWidth - (BoundingXPadding * 2),
  141. FrameHeight - (BoundingYPadding * 2));
  142. }
  143. }
  144. #endregion
  145. #region Collision Detection Methods
  146. public bool IsBoxColliding(Rectangle OtherBox)
  147. {
  148. if ((Collidable) && (!Expired))
  149. {
  150. return BoundingBoxRect.Intersects(OtherBox);
  151. }
  152. else
  153. {
  154. return false;
  155. }
  156. }
  157. public bool IsCircleColliding(
  158. Vector2 otherCenter,
  159. float otherRadius)
  160. {
  161. if ((Collidable) && (!Expired))
  162. {
  163. if (Vector2.Distance(WorldCenter, otherCenter) <
  164. (CollisionRadius + otherRadius))
  165. return true;
  166. else
  167. return false;
  168. }
  169. else
  170. {
  171. return false;
  172. }
  173. }
  174. #endregion
  175. #region Animation-Related Methods
  176. public void AddFrame(Rectangle frameRectangle)
  177. {
  178. frames.Add(frameRectangle);
  179. }
  180. public void RotateTo(Vector2 direction)
  181. {
  182. Rotation = (float)Math.Atan2(direction.Y, direction.X);
  183. }
  184. #endregion
  185. #region Update and Draw Methods
  186. public virtual void Update(GameTime gameTime)
  187. {
  188. if (!Expired)
  189. {
  190. float elapsed = (float)gameTime.ElapsedGameTime.TotalSeconds;
  191. timeForCurrentFrame += elapsed;
  192. if (Animate)
  193. {
  194. if (timeForCurrentFrame >= FrameTime)
  195. {
  196. if ((AnimateWhenStopped) ||
  197. (velocity != Vector2.Zero))
  198. {
  199. currentFrame = (currentFrame + 1) %
  200. (frames.Count);
  201. timeForCurrentFrame = 0.0f;
  202. }
  203. }
  204. }
  205. worldLocation += (velocity * elapsed);
  206. }
  207. }
  208. public virtual void Draw(SpriteBatch spriteBatch)
  209. {
  210. if (!Expired)
  211. {
  212. if (Camera.ObjectIsVisible(WorldRectangle))
  213. {
  214. spriteBatch.Draw(
  215. Texture,
  216. ScreenCenter,
  217. Source,
  218. tintColor,
  219. rotation,
  220. RelativeCenter,
  221. 1.0f,
  222. SpriteEffects.None,
  223. 0.0f);
  224. }
  225. }
  226. }
  227. #endregion
  228. }
  229. }