PowerUp.cs 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  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 Tutorial026.Interfaces;
  9. using Tutorial026.Models;
  10. namespace Tutorial026.Sprites
  11. {
  12. public class PowerUp : Sprite, IMoveable
  13. {
  14. private Vector2 _velocity;
  15. private bool _goingDown;
  16. private float _distanceTravelled;
  17. public float FloatingSpeed = 0.1f;
  18. public float FloatingDistance = 10f;
  19. /// <summary>
  20. /// What is gained if the power-up is collected
  21. /// </summary>
  22. public readonly Attributes Attributes;
  23. public Vector2 Velocity
  24. {
  25. get { return _velocity; }
  26. set
  27. {
  28. _velocity = value;
  29. }
  30. }
  31. public PowerUp(Texture2D texture, Attributes attributes)
  32. : base(texture)
  33. {
  34. Attributes = attributes;
  35. }
  36. public override void Update(GameTime gameTime)
  37. {
  38. if (_goingDown)
  39. _velocity.Y = FloatingSpeed;
  40. else _velocity.Y = -FloatingSpeed;
  41. _distanceTravelled += FloatingSpeed;
  42. if (_distanceTravelled >= FloatingDistance)
  43. {
  44. _distanceTravelled = 0;
  45. _goingDown = !_goingDown;
  46. }
  47. Position += _velocity;
  48. if (Rectangle.Right < 0)
  49. IsRemoved = true;
  50. }
  51. }
  52. }