Sprite.cs 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  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 Tutorial005
  10. {
  11. public class Sprite
  12. {
  13. private Texture2D _texture;
  14. private float _rotation;
  15. public Vector2 Position;
  16. /// <summary>
  17. /// The point we rotate on
  18. /// </summary>
  19. public Vector2 Origin;
  20. /// <summary>
  21. /// The speed of the rotation
  22. /// </summary>
  23. public float RotationVelocity = 3f;
  24. /// <summary>
  25. /// The speed of moving forward
  26. /// </summary>
  27. public float LinearVelocity = 4f;
  28. public Sprite(Texture2D texture)
  29. {
  30. _texture = texture;
  31. }
  32. public void Update()
  33. {
  34. if (Keyboard.GetState().IsKeyDown(Keys.A))
  35. _rotation -= MathHelper.ToRadians(RotationVelocity);
  36. else if (Keyboard.GetState().IsKeyDown(Keys.D))
  37. _rotation += MathHelper.ToRadians(RotationVelocity);
  38. var direction = new Vector2((float)Math.Cos(MathHelper.ToRadians(90) - _rotation), -(float)Math.Sin(MathHelper.ToRadians(90) - _rotation));
  39. if (Keyboard.GetState().IsKeyDown(Keys.W))
  40. Position += direction * LinearVelocity;
  41. }
  42. public void Draw(SpriteBatch spriteBatch)
  43. {
  44. spriteBatch.Draw(_texture, Position, null, Color.White, _rotation, Origin, 1, SpriteEffects.None, 0f);
  45. }
  46. }
  47. }