Sprite.cs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using Microsoft.Xna.Framework;
  6. using Microsoft.Xna.Framework.Graphics;
  7. namespace Asteroid_Belt_Assault
  8. {
  9. class Sprite
  10. {
  11. public Texture2D Texture;
  12. protected List<Rectangle> frames = new List<Rectangle>();
  13. private int frameWidth = 0;
  14. private int frameHeight = 0;
  15. private int currentFrame;
  16. private float frameTime = 0.1f;
  17. private float timeForCurrentFrame = 0.0f;
  18. private Color tintColor = Color.White;
  19. private float rotation = 0.0f;
  20. public int CollisionRadius = 0;
  21. public int BoundingXPadding = 0;
  22. public int BoundingYPadding = 0;
  23. protected Vector2 location = Vector2.Zero;
  24. protected Vector2 velocity = Vector2.Zero;
  25. public Sprite(
  26. Vector2 location,
  27. Texture2D texture,
  28. Rectangle initialFrame,
  29. Vector2 velocity)
  30. {
  31. this.location = location;
  32. Texture = texture;
  33. this.velocity = velocity;
  34. frames.Add(initialFrame);
  35. frameWidth = initialFrame.Width;
  36. frameHeight = initialFrame.Height;
  37. }
  38. public Vector2 Location
  39. {
  40. get { return location; }
  41. set { location = value; }
  42. }
  43. public Vector2 Velocity
  44. {
  45. get { return velocity; }
  46. set { velocity = value; }
  47. }
  48. public Color TintColor
  49. {
  50. get { return tintColor; }
  51. set { tintColor = value; }
  52. }
  53. public float Rotation
  54. {
  55. get { return rotation; }
  56. set { rotation = value % MathHelper.TwoPi; }
  57. }
  58. public int Frame
  59. {
  60. get { return currentFrame; }
  61. set
  62. {
  63. currentFrame = (int)MathHelper.Clamp(value, 0,
  64. frames.Count - 1);
  65. }
  66. }
  67. public float FrameTime
  68. {
  69. get { return frameTime; }
  70. set { frameTime = MathHelper.Max(0, value); }
  71. }
  72. public Rectangle Source
  73. {
  74. get { return frames[currentFrame]; }
  75. }
  76. public Rectangle Destination
  77. {
  78. get
  79. {
  80. return new Rectangle(
  81. (int)location.X,
  82. (int)location.Y,
  83. frameWidth,
  84. frameHeight);
  85. }
  86. }
  87. public Vector2 Center
  88. {
  89. get
  90. {
  91. return location +
  92. new Vector2(frameWidth / 2, frameHeight / 2);
  93. }
  94. }
  95. public Rectangle BoundingBoxRect
  96. {
  97. get
  98. {
  99. return new Rectangle(
  100. (int)location.X + BoundingXPadding,
  101. (int)location.Y + BoundingYPadding,
  102. frameWidth - (BoundingXPadding * 2),
  103. frameHeight - (BoundingYPadding * 2));
  104. }
  105. }
  106. public bool IsBoxColliding(Rectangle OtherBox)
  107. {
  108. return BoundingBoxRect.Intersects(OtherBox);
  109. }
  110. public bool IsCircleColliding(Vector2 otherCenter, float otherRadius)
  111. {
  112. if (Vector2.Distance(Center, otherCenter) <
  113. (CollisionRadius + otherRadius))
  114. return true;
  115. else
  116. return false;
  117. }
  118. public void AddFrame(Rectangle frameRectangle)
  119. {
  120. frames.Add(frameRectangle);
  121. }
  122. public virtual void Update(GameTime gameTime)
  123. {
  124. float elapsed = (float)gameTime.ElapsedGameTime.TotalSeconds;
  125. timeForCurrentFrame += elapsed;
  126. if (timeForCurrentFrame >= FrameTime)
  127. {
  128. currentFrame = (currentFrame + 1) % (frames.Count);
  129. timeForCurrentFrame = 0.0f;
  130. }
  131. location += (velocity * elapsed);
  132. }
  133. public virtual void Draw(SpriteBatch spriteBatch)
  134. {
  135. spriteBatch.Draw(
  136. Texture,
  137. Center,
  138. Source,
  139. tintColor,
  140. rotation,
  141. new Vector2(frameWidth / 2, frameHeight / 2),
  142. 1.0f,
  143. SpriteEffects.None,
  144. 0.0f);
  145. }
  146. }
  147. }