void ex_Finished(Explosion ex) { explosionsToDelete.Insert(0, explosions.IndexOf(ex)); }
protected void DetectCollisions() { List <int> asteroidsToDelete = new List <int>(); List <int> missilesToDelete = new List <int>(); List <int> enemiesToDelete = new List <int>(); foreach (Asteroid a in asteroids) { foreach (EShip Enemy in enemies) { int sumOfWidths = Enemy.Image.Width / 2 + a.Image.Width / 2; Vector pointing = new Vector(Enemy.Pos - a.Pos); float dist = pointing.Magnitude; if (dist - 10 < sumOfWidths) { pointing = new Vector(a.Pos - Enemy.Pos); Enemy.Vel = new Vector(pointing.Unitized * -5f); } if (dist < sumOfWidths) { if (!enemiesToDelete.Contains(enemies.IndexOf(Enemy))) { enemiesToDelete.Insert(0, enemies.IndexOf(Enemy)); } } } foreach (Missile m in missiles) { float sumOfWidths = m.Image.Width / 2 + a.Image.Width / 2; Vector pointing = m.Pos - a.Pos; float dist = pointing.Magnitude; if ((dist < sumOfWidths) && (m.friendlyMissle == true)) { // Collision detected! Store index for later removal int idxAsteroid = asteroids.IndexOf(a); int idxMissile = missiles.IndexOf(m); if (!asteroidsToDelete.Contains(idxAsteroid)) { asteroidsToDelete.Insert(0, idxAsteroid); } if (!missilesToDelete.Contains(idxMissile)) { missilesToDelete.Insert(0, idxMissile); } } if (ship != null) { sumOfWidths = m.Image.Width / 2 + ship.Image.Width / 2; pointing = m.Pos - ship.Pos; dist = pointing.Magnitude; if (dist < sumOfWidths && !m.friendlyMissle) { // Ship was shot by enemy laser ship.Crashed = true; Explosion ex = new Explosion(new Vector(ship.Pos), Properties.Resources.explosionShip, 0, 8, 4, 256, 256); ex.Finished += ex_Finished; explosions.Add(ex); ship = null; livesLeft--; if (!missilesToDelete.Contains(missiles.IndexOf(m))) { missilesToDelete.Insert(0, missiles.IndexOf(m)); } // Create a "white hole" for repulsion... blackHole = new Asteroid(new Vector(width / 2, height / 2), new Vector(0, 0), 0, 0, Properties.Resources.blackhole); shipSpawnTmr.Elapsed += shipSpawnTmr_Elapsed; state = GameState.Resetting; } } if (m.friendlyMissle) {//killing the alien ships foreach (EShip enemy in enemies) { sumOfWidths = m.Image.Width / 2 + enemy.Image.Width / 2; pointing = m.Pos - enemy.Pos; dist = pointing.Magnitude; if (dist < sumOfWidths) { int idxofenemy = enemies.IndexOf(enemy); int idxMissile = missiles.IndexOf(m); if (!enemiesToDelete.Contains(idxofenemy)) { enemiesToDelete.Insert(0, idxofenemy); } if (!missilesToDelete.Contains(idxMissile)) { missilesToDelete.Insert(0, idxMissile); } } } } } if (ship != null) { float sumOfWidths2 = ship.Image.Width / 2 + a.Image.Width / 2; Vector pointing2 = ship.Pos - a.Pos; float dist2 = pointing2.Magnitude; if (dist2 < sumOfWidths2) { // Ship has collided with an asteroid ship.Crashed = true; Explosion ex = new Explosion(new Vector(ship.Pos), Properties.Resources.explosionShip, 0, 8, 4, 256, 256); ex.Finished += ex_Finished; explosions.Add(ex); ship = null; livesLeft--; if (!asteroidsToDelete.Contains(asteroids.IndexOf(a))) { asteroidsToDelete.Insert(0, asteroids.IndexOf(a)); } // Create a "white hole" for repulsion... blackHole = new Asteroid(new Vector(width / 2, height / 2), new Vector(0, 0), 0, 0, Properties.Resources.blackhole); shipSpawnTmr.Elapsed += shipSpawnTmr_Elapsed; state = GameState.Resetting; } } foreach (int idx in enemiesToDelete) { Image img = Properties.Resources.explosion3; Explosion ex = new Explosion(new Vector(enemies[idx].Pos), img, rand.Next(0, 360), 5, 5, 80, 80); ex.Finished += ex_Finished; explosions.Add(ex); Score += 1000; ak += 5; enemies[idx].shoot.Stop(); enemies.RemoveAt(idx); } foreach (int idx in missilesToDelete) { // Remove missiles which had been detected to collide if (missiles[idx].friendlyMissle) { shipMissles--; } missiles.RemoveAt(idx); } missilesToDelete.Clear(); enemiesToDelete.Clear(); } foreach (int idx in asteroidsToDelete) { // Remove asteroids which had been detected to collide Image img; int numAcross, numDown, spriteWidth, spriteHeight; Asteroid a = asteroids[idx]; if (a.Type == 0) { img = Properties.Resources.Explosion; numAcross = 9; numDown = 9; spriteWidth = 100; spriteHeight = 100; } else { img = Properties.Resources.explosion2; numAcross = 3; numDown = 4; spriteWidth = 256; spriteHeight = 128; } Explosion ex = new Explosion(new Vector(asteroids[idx].Pos), img, rand.Next(0, 360), numAcross, numDown, spriteWidth, spriteHeight); ex.Finished += ex_Finished; explosions.Add(ex); // Update the score... Score += 50 * (4 - asteroids[idx].Size); //Split the asteroids up if (asteroids[idx].Size == 3) { if (asteroids[idx].Type == 0) { Asteroid A1 = new Asteroid(new Vector(asteroids[idx].Pos), new Vector(rand.Next(-10, 10), rand.Next(-10, 10)), asteroids[idx].Angle + 45, rand.Next(-10, 10), Properties.Resources.AsteroidMed); A1.Size = asteroids[idx].Size - 1; Asteroid A2 = new Asteroid(new Vector(asteroids[idx].Pos), new Vector(rand.Next(-10, 10), rand.Next(-10, 10)), asteroids[idx].Angle - 45, rand.Next(-10, 10), Properties.Resources.AsteroidMed); A2.Size = asteroids[idx].Size - 1; asteroids.Add(A1); asteroids.Add(A2); } else { Asteroid A1 = new Asteroid(new Vector(asteroids[idx].Pos), new Vector(rand.Next(-10, 10), rand.Next(-10, 10)), asteroids[idx].Angle + 45, rand.Next(-10, 10), Properties.Resources.Asteroid2Med); A1.Size = asteroids[idx].Size - 1; Asteroid A2 = new Asteroid(new Vector(asteroids[idx].Pos), new Vector(rand.Next(-10, 10), rand.Next(-10, 10)), asteroids[idx].Angle - 45, rand.Next(-10, 10), Properties.Resources.Asteroid2Med); A2.Size = asteroids[idx].Size - 1; asteroids.Add(A1); asteroids.Add(A2); } } else if (asteroids[idx].Size == 2) { if (asteroids[idx].Type == 0) { Asteroid A1 = new Asteroid(new Vector(asteroids[idx].Pos), new Vector(rand.Next(-10, 10), rand.Next(-10, 10)), asteroids[idx].Angle + 45, rand.Next(-10, 10), Properties.Resources.AsteroidSmall); A1.Size = asteroids[idx].Size - 1; Asteroid A2 = new Asteroid(new Vector(asteroids[idx].Pos), new Vector(rand.Next(-10, 10), rand.Next(-10, 10)), asteroids[idx].Angle - 45, rand.Next(-10, 10), Properties.Resources.AsteroidSmall); A2.Size = asteroids[idx].Size - 1; asteroids.Add(A1); asteroids.Add(A2); } else { Asteroid A1 = new Asteroid(new Vector(asteroids[idx].Pos), new Vector(rand.Next(-10, 10), rand.Next(-10, 10)), asteroids[idx].Angle + 45, rand.Next(-10, 10), Properties.Resources.Asteroid2Small); A1.Size = asteroids[idx].Size - 1; Asteroid A2 = new Asteroid(new Vector(asteroids[idx].Pos), new Vector(rand.Next(-10, 10), rand.Next(-10, 10)), asteroids[idx].Angle - 45, rand.Next(-10, 10), Properties.Resources.Asteroid2Small); A2.Size = asteroids[idx].Size - 1; asteroids.Add(A1); asteroids.Add(A2); } } asteroids.RemoveAt(idx); ak++; } if (ak >= 20 && ak < 40) { numMissiles = 20; } else if (ak >= 40) { numMissiles = 30; } }