Sprite.cs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. using Microsoft.Xna.Framework;
  2. using Microsoft.Xna.Framework.Graphics;
  3. using Microsoft.Xna.Framework.Input;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. namespace Tutorial019.Sprites
  10. {
  11. public class Sprite : Component, ICloneable
  12. {
  13. protected Texture2D _texture;
  14. protected float _rotation;
  15. protected KeyboardState _currentKey;
  16. protected KeyboardState _previousKey;
  17. public Vector2 Origin;
  18. public Vector2 Position;
  19. public Rectangle Rectangle
  20. {
  21. get
  22. {
  23. return new Rectangle((int)Position.X - (int)Origin.X, (int)Position.Y - (int)Origin.Y, _texture.Width, _texture.Height);
  24. }
  25. }
  26. public List<Sprite> Children { get; set; }
  27. public Color Colour { get; set; }
  28. public Vector2 Direction;
  29. public float RotationVelocity = 3f;
  30. public float LinearVelocity = 4f;
  31. public Sprite Parent;
  32. public float LifeSpan = 0f;
  33. public bool IsRemoved = false;
  34. public readonly Color[] TextureData;
  35. public Matrix Transform
  36. {
  37. get
  38. {
  39. return Matrix.CreateTranslation(new Vector3(-Origin, 0)) *
  40. Matrix.CreateRotationZ(_rotation) *
  41. Matrix.CreateTranslation(new Vector3(Position, 0));
  42. }
  43. }
  44. public Sprite(Texture2D texture)
  45. {
  46. _texture = texture;
  47. // The default origin in the centre of the sprite
  48. Origin = new Vector2(_texture.Width / 2, _texture.Height / 2);
  49. Children = new List<Sprite>();
  50. Colour = Color.White;
  51. TextureData = new Color[_texture.Width * _texture.Height];
  52. _texture.GetData(TextureData);
  53. }
  54. public override void Update(GameTime gameTime)
  55. {
  56. }
  57. public override void Draw(GameTime gameTime, SpriteBatch spriteBatch)
  58. {
  59. spriteBatch.Draw(_texture, Position, null, Colour, _rotation, Origin, 1, SpriteEffects.None, 0);
  60. }
  61. public bool Intersects(Sprite sprite)
  62. {
  63. // Calculate a matrix which transforms from A's local space into
  64. // world space and then into B's local space
  65. var transformAToB = this.Transform * Matrix.Invert(sprite.Transform);
  66. // When a point moves in A's local space, it moves in B's local space with a
  67. // fixed direction and distance proportional to the movement in A.
  68. // This algorithm steps through A one pixel at a time along A's X and Y axes
  69. // Calculate the analogous steps in B:
  70. var stepX = Vector2.TransformNormal(Vector2.UnitX, transformAToB);
  71. var stepY = Vector2.TransformNormal(Vector2.UnitY, transformAToB);
  72. // Calculate the top left corner of A in B's local space
  73. // This variable will be reused to keep track of the start of each row
  74. var yPosInB = Vector2.Transform(Vector2.Zero, transformAToB);
  75. for (int yA = 0; yA < this.Rectangle.Height; yA++)
  76. {
  77. // Start at the beginning of the row
  78. var posInB = yPosInB;
  79. for (int xA = 0; xA < this.Rectangle.Width; xA++)
  80. {
  81. // Round to the nearest pixel
  82. var xB = (int)Math.Round(posInB.X);
  83. var yB = (int)Math.Round(posInB.Y);
  84. if (0 <= xB && xB < sprite.Rectangle.Width &&
  85. 0 <= yB && yB < sprite.Rectangle.Height)
  86. {
  87. // Get the colors of the overlapping pixels
  88. var colourA = this.TextureData[xA + yA * this.Rectangle.Width];
  89. var colourB = sprite.TextureData[xB + yB * sprite.Rectangle.Width];
  90. // If both pixel are not completely transparent
  91. if (colourA.A != 0 && colourB.A != 0)
  92. {
  93. return true;
  94. }
  95. }
  96. // Move to the next pixel in the row
  97. posInB += stepX;
  98. }
  99. // Move to the next row
  100. yPosInB += stepY;
  101. }
  102. // No intersection found
  103. return false;
  104. }
  105. public virtual void OnCollide(Sprite sprite)
  106. {
  107. }
  108. public object Clone()
  109. {
  110. return this.MemberwiseClone();
  111. }
  112. }
  113. }