// 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(); } } } }
// Checks that a recoiled enemy does not land on top of any other enemies private bool checkForEnemyRecoilCollision(GameChar recoiled, Vector2 recoil) { bool intersects = false; Vector2 futurePos = new Vector2(recoiled.Position.X, recoiled.Position.Y); futurePos.X += recoil.X; futurePos.Y += recoil.Y; Rectangle futureBounds = new Rectangle(recoiled.Bounds.X, recoiled.Bounds.Y, recoiled.Bounds.Width, recoiled.Bounds.Height); futureBounds.X = (int)(futurePos.X - futureBounds.Width / 2); futureBounds.Y = (int)(futurePos.Y - futureBounds.Height / 2); foreach (GameChar enemy in world.Enemies) { if (!recoiled.Equals(enemy)) // Ensure enemy is not referencing itself { if (futureBounds.Intersects(enemy.Bounds)) { intersects = true; break; } } } return(intersects); }