Sprite.cs 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using Microsoft.Xna.Framework;
  7. using Microsoft.Xna.Framework.Graphics;
  8. using Tutorial020.Managers;
  9. using Tutorial020.Models;
  10. namespace Tutorial020.Sprites
  11. {
  12. public class Sprite : Component, ICloneable
  13. {
  14. protected Dictionary<string, Animation> _animations;
  15. protected AnimationManager _animationManager;
  16. protected float _layer { get; set; }
  17. protected Vector2 _origin { get; set; }
  18. protected Vector2 _position { get; set; }
  19. protected float _rotation { get; set; }
  20. protected float _scale { get; set; }
  21. protected Texture2D _texture;
  22. public List<Sprite> Children { get; set; }
  23. public Color Colour { get; set; }
  24. public bool IsRemoved { get; set; }
  25. public float Layer
  26. {
  27. get { return _layer; }
  28. set
  29. {
  30. _layer = value;
  31. if (_animationManager != null)
  32. _animationManager.Layer = _layer;
  33. }
  34. }
  35. public Vector2 Origin
  36. {
  37. get { return _origin; }
  38. set
  39. {
  40. _origin = value;
  41. if (_animationManager != null)
  42. _animationManager.Origin = _origin;
  43. }
  44. }
  45. public Vector2 Position
  46. {
  47. get
  48. {
  49. return _position;
  50. }
  51. set
  52. {
  53. _position = value;
  54. if (_animationManager != null)
  55. _animationManager.Position = _position;
  56. }
  57. }
  58. public Rectangle Rectangle
  59. {
  60. get
  61. {
  62. if (_texture != null)
  63. {
  64. return new Rectangle((int)Position.X - (int)Origin.X, (int)Position.Y - (int)Origin.Y, _texture.Width, _texture.Height);
  65. }
  66. if (_animationManager != null)
  67. {
  68. var animation = _animations.FirstOrDefault().Value;
  69. return new Rectangle((int)Position.X - (int)Origin.X, (int)Position.Y - (int)Origin.Y, animation.FrameWidth, animation.FrameHeight);
  70. }
  71. throw new Exception("Unknown sprite");
  72. }
  73. }
  74. public float Rotation
  75. {
  76. get { return _rotation; }
  77. set
  78. {
  79. _rotation = value;
  80. if (_animationManager != null)
  81. _animationManager.Rotation = value;
  82. }
  83. }
  84. public readonly Color[] TextureData;
  85. public Matrix Transform
  86. {
  87. get
  88. {
  89. return Matrix.CreateTranslation(new Vector3(-Origin, 0)) *
  90. Matrix.CreateRotationZ(_rotation) *
  91. Matrix.CreateTranslation(new Vector3(Position, 0));
  92. }
  93. }
  94. public Sprite Parent;
  95. /// <summary>
  96. /// The area of the sprite that could "potentially" be collided with
  97. /// </summary>
  98. public Rectangle CollisionArea
  99. {
  100. get
  101. {
  102. return new Rectangle(Rectangle.X, Rectangle.Y, MathHelper.Max(Rectangle.Width, Rectangle.Height), MathHelper.Max(Rectangle.Width, Rectangle.Height));
  103. }
  104. }
  105. public Sprite(Texture2D texture)
  106. {
  107. _texture = texture;
  108. Children = new List<Sprite>();
  109. Origin = new Vector2(_texture.Width / 2, _texture.Height / 2);
  110. Colour = Color.White;
  111. TextureData = new Color[_texture.Width * _texture.Height];
  112. _texture.GetData(TextureData);
  113. }
  114. public Sprite(Dictionary<string, Animation> animations)
  115. {
  116. _texture = null;
  117. Children = new List<Sprite>();
  118. Colour = Color.White;
  119. TextureData = null;
  120. _animations = animations;
  121. var animation = _animations.FirstOrDefault().Value;
  122. _animationManager = new AnimationManager(animation);
  123. Origin = new Vector2(animation.FrameWidth / 2, animation.FrameHeight / 2);
  124. }
  125. public override void Update(GameTime gameTime)
  126. {
  127. }
  128. public override void Draw(GameTime gameTime, SpriteBatch spriteBatch)
  129. {
  130. if (_texture != null)
  131. spriteBatch.Draw(_texture, Position, null, Colour, _rotation, Origin, 1f, SpriteEffects.None, Layer);
  132. else if (_animationManager != null)
  133. _animationManager.Draw(spriteBatch);
  134. }
  135. public bool Intersects(Sprite sprite)
  136. {
  137. if (this.TextureData == null)
  138. return false;
  139. if (sprite.TextureData == null)
  140. return false;
  141. // Calculate a matrix which transforms from A's local space into
  142. // world space and then into B's local space
  143. var transformAToB = this.Transform * Matrix.Invert(sprite.Transform);
  144. // When a point moves in A's local space, it moves in B's local space with a
  145. // fixed direction and distance proportional to the movement in A.
  146. // This algorithm steps through A one pixel at a time along A's X and Y axes
  147. // Calculate the analogous steps in B:
  148. var stepX = Vector2.TransformNormal(Vector2.UnitX, transformAToB);
  149. var stepY = Vector2.TransformNormal(Vector2.UnitY, transformAToB);
  150. // Calculate the top left corner of A in B's local space
  151. // This variable will be reused to keep track of the start of each row
  152. var yPosInB = Vector2.Transform(Vector2.Zero, transformAToB);
  153. for (int yA = 0; yA < this.Rectangle.Height; yA++)
  154. {
  155. // Start at the beginning of the row
  156. var posInB = yPosInB;
  157. for (int xA = 0; xA < this.Rectangle.Width; xA++)
  158. {
  159. // Round to the nearest pixel
  160. var xB = (int)Math.Round(posInB.X);
  161. var yB = (int)Math.Round(posInB.Y);
  162. if (0 <= xB && xB < sprite.Rectangle.Width &&
  163. 0 <= yB && yB < sprite.Rectangle.Height)
  164. {
  165. // Get the colors of the overlapping pixels
  166. var colourA = this.TextureData[xA + yA * this.Rectangle.Width];
  167. var colourB = sprite.TextureData[xB + yB * sprite.Rectangle.Width];
  168. // If both pixel are not completely transparent
  169. if (colourA.A != 0 && colourB.A != 0)
  170. {
  171. return true;
  172. }
  173. }
  174. // Move to the next pixel in the row
  175. posInB += stepX;
  176. }
  177. // Move to the next row
  178. yPosInB += stepY;
  179. }
  180. // No intersection found
  181. return false;
  182. }
  183. //public virtual void OnCollide(Sprite sprite)
  184. //{
  185. //}
  186. public object Clone()
  187. {
  188. var sprite = this.MemberwiseClone() as Sprite;
  189. if (_animations != null)
  190. {
  191. sprite._animations = this._animations.ToDictionary(c => c.Key, v => v.Value.Clone() as Animation);
  192. sprite._animationManager = sprite._animationManager.Clone() as AnimationManager;
  193. }
  194. return sprite;
  195. }
  196. }
  197. }