Bullet.cs 864 B

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. using Microsoft.Xna.Framework;
  2. using Microsoft.Xna.Framework.Graphics;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Linq;
  6. using System.Text;
  7. using System.Threading.Tasks;
  8. namespace Tutorial019.Sprites
  9. {
  10. public class Bullet : Sprite
  11. {
  12. private float _timer;
  13. public Bullet(Texture2D texture)
  14. : base(texture)
  15. {
  16. }
  17. public override void Update(GameTime gameTime)
  18. {
  19. _timer += (float)gameTime.ElapsedGameTime.TotalSeconds;
  20. if (_timer >= LifeSpan)
  21. IsRemoved = true;
  22. Position += Direction * LinearVelocity;
  23. }
  24. public override void OnCollide(Sprite sprite)
  25. {
  26. if (sprite == this.Parent)
  27. return;
  28. // Bullets don't collide with eachother
  29. if (sprite is Bullet)
  30. return;
  31. IsRemoved = true;
  32. }
  33. }
  34. }