public static void CheckCollisionProjectile(Projectile projectile) { List<Enemy> enemies = instance.currentLevel.GetEnemies(); for (int i = 0; i < enemies.Count; i++) { if (enemies[i].bounds.Intersects(projectile.bounds)) { enemies[i].health -= 1; projectiles.Remove(projectile); } } }
//private void UpdateCollision() //{ // // Use the Rectangle's built-in intersect function to // // determine if two objects are overlapping // Rectangle rectangle1; // Rectangle rectangle2; // // Only create the rectangle once for the player // rectangle1 = new Rectangle((int)Character.bounds.X, (int)Character.bounds.Y, Character.bounds.Width, Character.bounds.Height); // // Do the collision between the player and the enemies // for (int i = 0; i < currentlevel.enemies.Count; i++) // { // rectangle2 = new Rectangle((int)enemies[i].Position.X, // (int)enemies[i].Position.Y, // enemies[i].Width, // enemies[i].Height); // // Determine if the two objects collided with each // // other // if (rectangle1.Intersects(rectangle2)) // { // // Subtract the health from the player based on // // the enemy damage // player.Health -= enemies[i].Damage; // // Since the enemy collided with the player // // destroy it // enemies[i].Health = 0; // // If the player health is less than zero we died // if (player.Health <= 0) // { // player.Active = false; // alive = false; // } // } // } // // Projectile vs Enemy Collision // for (int i = 0; i < projectiles.Count; i++) // { // for (int j = 0; j < enemies.Count; j++) // { // // Create the rectangles we need to determine if we collided with each other // rectangle1 = new Rectangle((int)projectiles[i].Position.X - // projectiles[i].Width / 2, (int)projectiles[i].Position.Y - // projectiles[i].Height / 2, projectiles[i].Width, projectiles[i].Height); // rectangle2 = new Rectangle((int)enemies[j].Position.X - enemies[j].Width / 2, // (int)enemies[j].Position.Y - enemies[j].Height / 2, // enemies[j].Width, enemies[j].Height); // // Determine if the two objects collided with each other // if (rectangle1.Intersects(rectangle2)) // { // enemies[j].Health -= projectiles[i].Damage; // projectiles[i].Active = false; // } // } // } //} public static void AddProjectile(Vector2 position) { Projectile projectile = new Projectile(); projectile.Initialize(projectileTexture, position); if (Character.currentFacing == Character.facing.right) projectile.projectileMoveSpeed = 6; else projectile.projectileMoveSpeed = -6; projectiles.Add(projectile); laserSound.Play(); }