Sprite.cs 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  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 Tutorial030.Managers;
  9. using Tutorial030.Models;
  10. namespace Tutorial030.Sprites
  11. {
  12. public class Sprite : Component
  13. {
  14. protected AnimationManager _animationManager;
  15. protected Dictionary<string, Animation> _animations;
  16. protected Texture2D _texture;
  17. protected Vector2 _position;
  18. protected float _layer { get; set; }
  19. public Color Colour { get; set; }
  20. public float Opacity { get; set; }
  21. public Vector2 Origin { get; set; }
  22. public float Rotation { get; set; }
  23. public float Scale { get; set; }
  24. public Vector2 Position
  25. {
  26. get { return _position; }
  27. set
  28. {
  29. _position = value;
  30. if (_animationManager != null)
  31. _animationManager.Position = _position;
  32. }
  33. }
  34. public float X
  35. {
  36. get { return Position.X; }
  37. set
  38. {
  39. Position = new Vector2(value, Position.Y);
  40. }
  41. }
  42. public float Y
  43. {
  44. get { return Position.Y; }
  45. set
  46. {
  47. Position = new Vector2(Position.X, value);
  48. }
  49. }
  50. public float Layer
  51. {
  52. get { return _layer; }
  53. set
  54. {
  55. _layer = value;
  56. if (_animationManager != null)
  57. _animationManager.Layer = _layer;
  58. }
  59. }
  60. public Rectangle Rectangle
  61. {
  62. get
  63. {
  64. int x = 0;
  65. int y = 0;
  66. int width = 0;
  67. int height = 0;
  68. if (_texture != null)
  69. {
  70. width = _texture.Width;
  71. height = _texture.Height;
  72. }
  73. else if (_animationManager != null)
  74. {
  75. width = _animationManager.FrameWidth;
  76. height = _animationManager.FrameHeight;
  77. }
  78. return new Rectangle((int)(Position.X - Origin.X), (int)(Position.Y - Origin.Y), (int)(width * Scale), (int)(height * Scale));
  79. }
  80. }
  81. #region
  82. public Vector2 TopLeft
  83. {
  84. get
  85. {
  86. return new Vector2(Rectangle.X, Rectangle.Y);
  87. }
  88. }
  89. public Vector2 TopRight
  90. {
  91. get
  92. {
  93. return new Vector2(Rectangle.X + Rectangle.Width, Rectangle.Y);
  94. }
  95. }
  96. public Vector2 BottomLeft
  97. {
  98. get
  99. {
  100. return new Vector2(Rectangle.X, Rectangle.Y + Rectangle.Height);
  101. }
  102. }
  103. public Vector2 BottomRight
  104. {
  105. get
  106. {
  107. return new Vector2(Rectangle.X + Rectangle.Width, Rectangle.Y + Rectangle.Height);
  108. }
  109. }
  110. public Vector2 Centre
  111. {
  112. get
  113. {
  114. return new Vector2(Rectangle.X + (Rectangle.Width / 2), Rectangle.Y + (Rectangle.Height / 2));
  115. }
  116. }
  117. public List<Vector2> Dots
  118. {
  119. get
  120. {
  121. return new List<Vector2>()
  122. {
  123. Centre,
  124. TopRight,
  125. BottomRight,
  126. BottomLeft,
  127. TopLeft,
  128. };
  129. }
  130. }
  131. public List<Vector2> GetNormals()
  132. {
  133. var normals = new List<Vector2>();
  134. var dots = this.Dots;
  135. for (int i = 1; i < dots.Count - 1; i++)
  136. {
  137. normals.Add(Vector2.Normalize(new Vector2(dots[i + 1].X - dots[i].X, dots[i + 1].Y - dots[i].Y)));
  138. }
  139. normals.Add(Vector2.Normalize(new Vector2(dots[1].X - dots[dots.Count - 1].X, dots[1].Y - dots[dots.Count - 1].Y)));
  140. return normals;
  141. }
  142. #endregion
  143. public bool IsRemoved { get; set; }
  144. public Sprite(Texture2D texture)
  145. {
  146. _texture = texture;
  147. Opacity = 1f;
  148. Scale = 1f;
  149. Origin = new Vector2(0, 0);
  150. Colour = Color.White;
  151. }
  152. public Sprite(Dictionary<string, Animation> animations)
  153. {
  154. _animations = animations;
  155. _animationManager = new AnimationManager(_animations.First().Value);
  156. Opacity = 1f;
  157. Scale = 1f;
  158. Colour = Color.White;
  159. }
  160. public override void Update(GameTime gameTime)
  161. {
  162. }
  163. public override void Draw(GameTime gameTime, SpriteBatch spriteBatch)
  164. {
  165. if (_texture != null)
  166. spriteBatch.Draw(_texture, Position, null, Colour * Opacity, Rotation, Origin, Scale, SpriteEffects.None, Layer);
  167. if (_animationManager != null)
  168. _animationManager.Draw(spriteBatch);
  169. }
  170. public virtual void OnCollide(Sprite sprite)
  171. {
  172. }
  173. public bool IsTouching(Sprite sprite)
  174. {
  175. return this.Rectangle.Right >= sprite.Rectangle.Left &&
  176. this.Rectangle.Left <= sprite.Rectangle.Right &&
  177. this.Rectangle.Bottom >= sprite.Rectangle.Top &&
  178. this.Rectangle.Top <= sprite.Rectangle.Bottom;
  179. }
  180. public bool IsTouchingTopOf(Sprite sprite)
  181. {
  182. return this.Rectangle.Right >= sprite.Rectangle.Left &&
  183. this.Rectangle.Left <= sprite.Rectangle.Right &&
  184. this.Rectangle.Bottom >= sprite.Rectangle.Top &&
  185. this.Rectangle.Top < sprite.Rectangle.Top;
  186. }
  187. public bool IsTouchingLeftOf(Sprite sprite)
  188. {
  189. return this.Rectangle.Bottom >= sprite.Rectangle.Top &&
  190. this.Rectangle.Top <= sprite.Rectangle.Bottom &&
  191. this.Rectangle.Right >= sprite.Rectangle.Left &&
  192. this.Rectangle.Left < sprite.Rectangle.Left;
  193. }
  194. public object Clone()
  195. {
  196. return this.MemberwiseClone();
  197. }
  198. }
  199. }