// Resolves problem where enemies can be recoiled on top of one another at the map's edge private void spreadApart(GameChar enemy) { foreach (GameChar otherEnemy in world.Enemies) { if (!enemy.Equals(otherEnemy)) // Ensure enemy is not referencing itself { if (enemy.Bounds.Intersects(otherEnemy.Bounds)) { // Enemy is left of other enemy if (enemy.Position.X < otherEnemy.Position.X) { enemy.Position.X -= SpreadApartSpeed; } // Enemy is right of other enemy else if (enemy.Position.X > otherEnemy.Position.X) { enemy.Position.X += SpreadApartSpeed; } // Enemy is above other enemy if (enemy.Position.Y < otherEnemy.Position.Y) { enemy.Position.Y -= SpreadApartSpeed; } // Enemy is below other enemy else if (enemy.Position.Y > otherEnemy.Position.Y) { enemy.Position.Y += SpreadApartSpeed; } enemy.SetBounds(); } } } }
public override void Update(float dt) { for (int i = world.Enemies.Count - 1; i >= 0; i--) { GameChar enemy = world.Enemies[i]; ai = setAI(enemy); handleAttack(enemy, dt); setVelocity(enemy, dt); setPosition(enemy, dt); enemy.SetBounds(); spreadApart(enemy); boundsCheck(enemy); // Remove dead enemies if (enemy.Health <= 0) { world.AllGameChars.Remove(enemy); world.Enemies.RemoveAt(i); } } }