PowerUp.cs 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  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.Interfaces;
  9. namespace Tutorial030.Sprites
  10. {
  11. public class PowerUp : Sprite, IMoveable
  12. {
  13. private Vector2 _velocity;
  14. private bool _goingDown;
  15. private float _distanceTravelled;
  16. public float FloatingSpeed = 0.1f;
  17. public float FloatingDistance = 10f;
  18. public Vector2 Velocity
  19. {
  20. get { return _velocity; }
  21. set
  22. {
  23. _velocity = value;
  24. }
  25. }
  26. public PowerUp(Texture2D texture)
  27. : base(texture)
  28. {
  29. }
  30. public override void Update(GameTime gameTime)
  31. {
  32. if (_goingDown)
  33. _velocity.Y = FloatingSpeed;
  34. else _velocity.Y = -FloatingSpeed;
  35. _distanceTravelled += FloatingSpeed;
  36. if (_distanceTravelled >= FloatingDistance)
  37. {
  38. _distanceTravelled = 0;
  39. _goingDown = !_goingDown;
  40. }
  41. Position += _velocity;
  42. if (Rectangle.Right < 0)
  43. IsRemoved = false;
  44. }
  45. }
  46. }