Sprite.cs 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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. namespace Tutorial022.Sprites
  9. {
  10. public class Sprite : Component
  11. {
  12. protected float _layer { get; set; }
  13. protected Vector2 _origin { get; set; }
  14. protected Vector2 _position { get; set; }
  15. protected float _rotation { get; set; }
  16. protected Texture2D _texture;
  17. public Color Colour { get; set; }
  18. /// <summary>
  19. /// The sprite that we want to follow
  20. /// </summary>
  21. public Sprite FollowTarget { get; set; }
  22. /// <summary>
  23. /// How close we want to be to our target
  24. /// </summary>
  25. public float FollowDistance { get; set; }
  26. public bool IsRemoved { get; set; }
  27. public Vector2 Direction;
  28. public float RotationVelocity = 3f;
  29. public float LinearVelocity = 4f;
  30. public float Layer
  31. {
  32. get { return _layer; }
  33. set
  34. {
  35. _layer = value;
  36. }
  37. }
  38. public Vector2 Origin
  39. {
  40. get { return _origin; }
  41. set
  42. {
  43. _origin = value;
  44. }
  45. }
  46. public Vector2 Position
  47. {
  48. get { return _position; }
  49. set
  50. {
  51. _position = value;
  52. }
  53. }
  54. public Rectangle Rectangle
  55. {
  56. get
  57. {
  58. return new Rectangle((int)Position.X - (int)Origin.X, (int)Position.Y - (int)Origin.Y, _texture.Width, _texture.Height);
  59. }
  60. }
  61. public float Rotation
  62. {
  63. get { return _rotation; }
  64. set
  65. {
  66. _rotation = value;
  67. }
  68. }
  69. public Sprite(Texture2D texture)
  70. {
  71. _texture = texture;
  72. Origin = new Vector2(_texture.Width / 2, _texture.Height / 2);
  73. Colour = Color.White;
  74. }
  75. public override void Update(GameTime gameTime)
  76. {
  77. Follow();
  78. }
  79. protected void Follow()
  80. {
  81. if (FollowTarget == null)
  82. return;
  83. var distance = FollowTarget.Position - this.Position;
  84. _rotation = (float)Math.Atan2(distance.Y, distance.X);
  85. Direction = new Vector2((float)Math.Cos(_rotation), (float)Math.Sin(_rotation));
  86. var currentDistance = Vector2.Distance(this.Position, FollowTarget.Position);
  87. if (currentDistance > FollowDistance)
  88. {
  89. var t = MathHelper.Min((float)Math.Abs(currentDistance - FollowDistance), LinearVelocity);
  90. var velocity = Direction * t;
  91. Position += velocity;
  92. }
  93. }
  94. public override void Draw(GameTime gameTime, SpriteBatch spriteBatch)
  95. {
  96. spriteBatch.Draw(_texture, Position, null, Colour, _rotation, Origin, 1f, SpriteEffects.None, Layer);
  97. }
  98. /// <summary>
  99. /// Once the follow target has been set, the sprite will follow the target
  100. /// </summary>
  101. /// <param name="followTarget">The sprite we want to follow</param>
  102. /// <param name="followDistance">how close we'll get to our target</param>
  103. /// <returns></returns>
  104. public Sprite SetFollowTarget(Sprite followTarget, float followDistance)
  105. {
  106. FollowTarget = followTarget;
  107. FollowDistance = followDistance;
  108. return this;
  109. }
  110. }
  111. }