EntityManager.cs 5.1 KB

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