Sprite.cs 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  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 Tutorial024.Sprites
  9. {
  10. public class Sprite : Component, ICloneable
  11. {
  12. protected Texture2D _texture;
  13. public float Opacity { get; set; }
  14. public Vector2 Origin { get; set; }
  15. public float Rotation { get; set; }
  16. public float Scale { get; set; }
  17. public Vector2 Position;
  18. public Vector2 Velocity;
  19. public Rectangle Rectangle
  20. {
  21. get
  22. {
  23. return new Rectangle((int)(Position.X - Origin.X), (int)(Position.Y - Origin.Y), (int)(_texture.Width * Scale), (int)(_texture.Height * Scale));
  24. }
  25. }
  26. public bool IsRemoved { get; set; }
  27. public Sprite(Texture2D texture)
  28. {
  29. _texture = texture;
  30. Opacity = 1f;
  31. Origin = new Vector2(_texture.Width / 2, _texture.Height / 2);
  32. }
  33. public override void Update(GameTime gameTime)
  34. {
  35. Position += Velocity;
  36. if (Rectangle.Top > Game1.ScreenHeight)
  37. IsRemoved = true;
  38. }
  39. public override void Draw(GameTime gameTime, SpriteBatch spriteBatch)
  40. {
  41. spriteBatch.Draw(_texture, Position, null, Color.White * Opacity, Rotation, Origin, Scale, SpriteEffects.None, 0);
  42. }
  43. public object Clone()
  44. {
  45. return this.MemberwiseClone();
  46. }
  47. }
  48. }