EntityManager.cs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. //---------------------------------------------------------------------------------
  2. // Written by Michael Hoffman
  3. // Find the full tutorial at: http://gamedev.tutsplus.com/series/vector-shooter-xna/
  4. //----------------------------------------------------------------------------------
  5. using System.Collections.Generic;
  6. using System.Linq;
  7. using AtomicEngine;
  8. namespace AtomicBlaster
  9. {
  10. static class EntityManager
  11. {
  12. static List<Entity> entities = new List<Entity>();
  13. static List<Enemy> enemies = new List<Enemy>();
  14. static List<Bullet> bullets = new List<Bullet>();
  15. static List<BlackHole> blackHoles = new List<BlackHole>();
  16. public static IEnumerable<BlackHole> BlackHoles { get { return blackHoles; } }
  17. static bool isUpdating;
  18. static List<Entity> addedEntities = new List<Entity>();
  19. public static int Count { get { return entities.Count; } }
  20. public static int BlackHoleCount { get { return blackHoles.Count; } }
  21. public static void Add(Entity entity)
  22. {
  23. if (!isUpdating)
  24. AddEntity(entity);
  25. else
  26. addedEntities.Add(entity);
  27. }
  28. private static void AddEntity(Entity entity)
  29. {
  30. entities.Add(entity);
  31. if (entity is Bullet)
  32. bullets.Add(entity as Bullet);
  33. else if (entity is Enemy)
  34. enemies.Add(entity as Enemy);
  35. else if (entity is BlackHole)
  36. blackHoles.Add(entity as BlackHole);
  37. }
  38. public static void Update()
  39. {
  40. isUpdating = true;
  41. HandleCollisions();
  42. foreach (var entity in entities)
  43. entity.Update();
  44. isUpdating = false;
  45. foreach (var entity in addedEntities)
  46. AddEntity(entity);
  47. addedEntities.Clear();
  48. entities = entities.Where(x => !x.IsExpired).ToList();
  49. bullets = bullets.Where(x => !x.IsExpired).ToList();
  50. enemies = enemies.Where(x => !x.IsExpired).ToList();
  51. blackHoles = blackHoles.Where(x => !x.IsExpired).ToList();
  52. }
  53. static void HandleCollisions()
  54. {
  55. // handle collisions between enemies
  56. for (int i = 0; i < enemies.Count; i++)
  57. for (int j = i + 1; j < enemies.Count; j++)
  58. {
  59. if (IsColliding(enemies[i], enemies[j]))
  60. {
  61. enemies[i].HandleCollision(enemies[j]);
  62. enemies[j].HandleCollision(enemies[i]);
  63. }
  64. }
  65. // handle collisions between bullets and enemies
  66. for (int i = 0; i < enemies.Count; i++)
  67. for (int j = 0; j < bullets.Count; j++)
  68. {
  69. if (IsColliding(enemies[i], bullets[j]))
  70. {
  71. enemies[i].WasShot();
  72. bullets[j].IsExpired = true;
  73. }
  74. }
  75. // handle collisions between the player and enemies
  76. for (int i = 0; i < enemies.Count; i++)
  77. {
  78. if (enemies[i].IsActive && IsColliding(PlayerShip.Instance, enemies[i]))
  79. {
  80. KillPlayer();
  81. break;
  82. }
  83. }
  84. // handle collisions with black holes
  85. for (int i = 0; i < blackHoles.Count; i++)
  86. {
  87. for (int j = 0; j < enemies.Count; j++)
  88. if (enemies[j].IsActive && IsColliding(blackHoles[i], enemies[j]))
  89. enemies[j].WasShot();
  90. for (int j = 0; j < bullets.Count; j++)
  91. {
  92. if (IsColliding(blackHoles[i], bullets[j]))
  93. {
  94. bullets[j].IsExpired = true;
  95. blackHoles[i].WasShot();
  96. }
  97. }
  98. if (IsColliding(PlayerShip.Instance, blackHoles[i]))
  99. {
  100. KillPlayer();
  101. break;
  102. }
  103. }
  104. }
  105. private static void KillPlayer()
  106. {
  107. PlayerShip.Instance.Kill();
  108. enemies.ForEach(x => x.WasShot());
  109. blackHoles.ForEach(x => x.Kill());
  110. EnemySpawner.Reset();
  111. }
  112. private static bool IsColliding(Entity a, Entity b)
  113. {
  114. float radius = a.Radius + b.Radius;
  115. return !a.IsExpired && !b.IsExpired && Vector2.DistanceSquared(a.Position, b.Position) < radius * radius;
  116. }
  117. public static IEnumerable<Entity> GetNearbyEntities(Vector2 position, float radius)
  118. {
  119. return entities.Where(x => Vector2.DistanceSquared(position, x.Position) < radius * radius);
  120. }
  121. public static void Draw(/*SpriteBatch spriteBatch*/)
  122. {
  123. /*
  124. foreach (var entity in entities)
  125. entity.Draw(spriteBatch);
  126. */
  127. }
  128. }
  129. }