EnemySpawner.cs 1.8 KB

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