EnemySpawner.cs 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. //---------------------------------------------------------------------------------
  2. // Written by Michael Hoffman
  3. // Find the full tutorial at: http://gamedev.tutsplus.com/series/vector-shooter-xna/
  4. //----------------------------------------------------------------------------------
  5. using System;
  6. using System.Linq;
  7. using AtomicEngine;
  8. namespace AtomicBlaster
  9. {
  10. static class EnemySpawner
  11. {
  12. static Random rand = new Random();
  13. static float inverseSpawnChance = 90;
  14. static float inverseBlackHoleChance = 600;
  15. public static void Update()
  16. {
  17. if (!PlayerShip.Instance.IsDead && EntityManager.Count < 200)
  18. {
  19. if (rand.Next((int)inverseSpawnChance) == 0)
  20. EntityManager.Add(Enemy.CreateSeeker(GetSpawnPosition()));
  21. if (rand.Next((int)inverseSpawnChance) == 0)
  22. EntityManager.Add(Enemy.CreateWanderer(GetSpawnPosition()));
  23. if (EntityManager.BlackHoleCount < 2 && rand.Next((int)inverseBlackHoleChance) == 0)
  24. EntityManager.Add(new BlackHole(GetSpawnPosition()));
  25. }
  26. // slowly increase the spawn rate as time progresses
  27. if (inverseSpawnChance > 30)
  28. inverseSpawnChance -= 0.005f;
  29. }
  30. private static Vector2 GetSpawnPosition()
  31. {
  32. Vector2 pos;
  33. do
  34. {
  35. pos = new Vector2(rand.Next((int)GameRoot.ScreenSize.X), rand.Next((int)GameRoot.ScreenSize.Y));
  36. }
  37. while (Vector2.DistanceSquared(pos, PlayerShip.Instance.Position) < 250 * 250);
  38. return pos;
  39. }
  40. public static void Reset()
  41. {
  42. inverseSpawnChance = 90;
  43. }
  44. }
  45. }