EnemyManager.cs 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. using Microsoft.Xna.Framework;
  2. using Microsoft.Xna.Framework.Content;
  3. using Microsoft.Xna.Framework.Graphics;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. using Tutorial020.Sprites;
  10. namespace Tutorial020.Managers
  11. {
  12. public class EnemyManager
  13. {
  14. private float _timer;
  15. private List<Texture2D> _textures;
  16. public bool CanAdd { get; set; }
  17. public Bullet Bullet { get; set; }
  18. public int MaxEnemies { get; set; }
  19. public float SpawnTimer { get; set; }
  20. public EnemyManager(ContentManager content)
  21. {
  22. _textures = new List<Texture2D>()
  23. {
  24. content.Load<Texture2D>("Ships/Enemy_1"),
  25. content.Load<Texture2D>("Ships/Enemy_2"),
  26. };
  27. MaxEnemies = 10;
  28. SpawnTimer = 2.5f;
  29. }
  30. public void Update(GameTime gameTime)
  31. {
  32. _timer += (float)gameTime.ElapsedGameTime.TotalSeconds;
  33. CanAdd = false;
  34. if (_timer > SpawnTimer)
  35. {
  36. CanAdd = true;
  37. _timer = 0;
  38. }
  39. }
  40. public Enemy GetEnemy()
  41. {
  42. var texture = _textures[Game1.Random.Next(0, _textures.Count)];
  43. return new Enemy(texture)
  44. {
  45. Colour = Color.Red,
  46. Bullet = Bullet,
  47. Health = 5,
  48. Layer = 0.2f,
  49. Position = new Vector2(Game1.ScreenWidth + texture.Width, Game1.Random.Next(0, Game1.ScreenHeight)),
  50. Speed = 2 + (float)Game1.Random.NextDouble(),
  51. ShootingTimer = 1.5f + (float)Game1.Random.NextDouble(),
  52. };
  53. }
  54. }
  55. }