EnemyManager.cs 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using Microsoft.Xna.Framework;
  6. using Microsoft.Xna.Framework.Graphics;
  7. namespace Asteroid_Belt_Assault
  8. {
  9. class EnemyManager
  10. {
  11. private Texture2D texture;
  12. private Rectangle initialFrame;
  13. private int frameCount;
  14. public List<Enemy> Enemies = new List<Enemy>();
  15. public ShotManager EnemyShotManager;
  16. private PlayerManager playerManager;
  17. public int MinShipsPerWave = 5;
  18. public int MaxShipsPerWave = 8;
  19. private float nextWaveTimer = 0.0f;
  20. private float nextWaveMinTimer = 8.0f;
  21. private float shipSpawnTimer = 0.0f;
  22. private float shipSpawnWaitTime = 0.5f;
  23. private float shipShotChance = 0.2f;
  24. private List<List<Vector2>> pathWaypoints =
  25. new List<List<Vector2>>();
  26. private Dictionary<int, int> waveSpawns = new Dictionary<int, int>();
  27. public bool Active = false;
  28. private Random rand = new Random();
  29. private void setUpWaypoints()
  30. {
  31. List<Vector2> path0 = new List<Vector2>();
  32. path0.Add(new Vector2(850, 300));
  33. path0.Add(new Vector2(-100, 300));
  34. pathWaypoints.Add(path0);
  35. waveSpawns[0] = 0;
  36. List<Vector2> path1 = new List<Vector2>();
  37. path1.Add(new Vector2(-50, 225));
  38. path1.Add(new Vector2(850, 225));
  39. pathWaypoints.Add(path1);
  40. waveSpawns[1] = 0;
  41. List<Vector2> path2 = new List<Vector2>();
  42. path2.Add(new Vector2(-100, 50));
  43. path2.Add(new Vector2(150, 50));
  44. path2.Add(new Vector2(200, 75));
  45. path2.Add(new Vector2(200, 125));
  46. path2.Add(new Vector2(150, 150));
  47. path2.Add(new Vector2(150, 175));
  48. path2.Add(new Vector2(200, 200));
  49. path2.Add(new Vector2(600, 200));
  50. path2.Add(new Vector2(850, 600));
  51. pathWaypoints.Add(path2);
  52. waveSpawns[2] = 0;
  53. List<Vector2> path3 = new List<Vector2>();
  54. path3.Add(new Vector2(600, -100));
  55. path3.Add(new Vector2(600, 250));
  56. path3.Add(new Vector2(580, 275));
  57. path3.Add(new Vector2(500, 250));
  58. path3.Add(new Vector2(500, 200));
  59. path3.Add(new Vector2(450, 175));
  60. path3.Add(new Vector2(400, 150));
  61. path3.Add(new Vector2(-100, 150));
  62. pathWaypoints.Add(path3);
  63. waveSpawns[3] = 0;
  64. }
  65. public EnemyManager(
  66. Texture2D texture,
  67. Rectangle initialFrame,
  68. int frameCount,
  69. PlayerManager playerManager,
  70. Rectangle screenBounds)
  71. {
  72. this.texture = texture;
  73. this.initialFrame = initialFrame;
  74. this.frameCount = frameCount;
  75. this.playerManager = playerManager;
  76. EnemyShotManager = new ShotManager(
  77. texture,
  78. new Rectangle(0, 300, 5, 5),
  79. 4,
  80. 2,
  81. 150f,
  82. screenBounds);
  83. setUpWaypoints();
  84. }
  85. public void SpawnEnemy(int path)
  86. {
  87. Enemy thisEnemy = new Enemy(
  88. texture,
  89. pathWaypoints[path][0],
  90. initialFrame,
  91. frameCount);
  92. for (int x = 0; x < pathWaypoints[path].Count(); x++)
  93. {
  94. thisEnemy.AddWaypoint(pathWaypoints[path][x]);
  95. }
  96. Enemies.Add(thisEnemy);
  97. }
  98. public void SpawnWave(int waveType)
  99. {
  100. waveSpawns[waveType] +=
  101. rand.Next(MinShipsPerWave, MaxShipsPerWave + 1);
  102. }
  103. private void updateWaveSpawns(GameTime gameTime)
  104. {
  105. shipSpawnTimer += (float)gameTime.ElapsedGameTime.TotalSeconds;
  106. if (shipSpawnTimer > shipSpawnWaitTime)
  107. {
  108. for (int x = waveSpawns.Count - 1; x >= 0; x--)
  109. {
  110. if (waveSpawns[x] > 0)
  111. {
  112. waveSpawns[x]--;
  113. SpawnEnemy(x);
  114. }
  115. }
  116. shipSpawnTimer = 0f;
  117. }
  118. nextWaveTimer += (float)gameTime.ElapsedGameTime.TotalSeconds;
  119. if (nextWaveTimer > nextWaveMinTimer)
  120. {
  121. SpawnWave(rand.Next(0, pathWaypoints.Count));
  122. nextWaveTimer = 0f;
  123. }
  124. }
  125. public void Update(GameTime gameTime)
  126. {
  127. EnemyShotManager.Update(gameTime);
  128. for (int x = Enemies.Count - 1; x >= 0; x--)
  129. {
  130. Enemies[x].Update(gameTime);
  131. if (Enemies[x].IsActive() == false)
  132. {
  133. Enemies.RemoveAt(x);
  134. }
  135. else
  136. {
  137. if ((float)rand.Next(0, 1000) / 10 <= shipShotChance)
  138. {
  139. Vector2 fireLoc = Enemies[x].EnemySprite.Location;
  140. fireLoc += Enemies[x].gunOffset;
  141. Vector2 shotDirection =
  142. playerManager.playerSprite.Center -
  143. fireLoc;
  144. shotDirection.Normalize();
  145. EnemyShotManager.FireShot(
  146. fireLoc,
  147. shotDirection,
  148. false);
  149. }
  150. }
  151. }
  152. if (Active)
  153. {
  154. updateWaveSpawns(gameTime);
  155. }
  156. }
  157. public void Draw(SpriteBatch spriteBatch)
  158. {
  159. EnemyShotManager.Draw(spriteBatch);
  160. foreach (Enemy enemy in Enemies)
  161. {
  162. enemy.Draw(spriteBatch);
  163. }
  164. }
  165. }
  166. }