// spawn a random asteroid at a random location with random attributes public void spawnAsteroid() { int asteroidIndex; bool bigAsteroid; Random rand = new Random(); if (rand.Next(0, 99) < 30) { // spawn small asteroid (30% chance) asteroidIndex = rand.Next(0, 4) + 2; bigAsteroid = false; } else { // spawn big asteroid (70% chance) asteroidIndex = rand.Next(0, 2) % 2; bigAsteroid = true; } // if there are too many asteroids of this type, do not spawn it if (bigAsteroid && m_numberOfBigAsteroids >= m_maxBigAsteroids) { return; } if (!bigAsteroid && m_numberOfSmallAsteroids >= m_maxSmallAsteroids) { return; } bool isInsideOfAsteroid; bool isTooCloseToShip; Asteroid newAsteroid = new Asteroid(m_asteroidSprites.getSprite(asteroidIndex), bigAsteroid, m_settings); do { // make sure that the new asteroid is not too close to a space ship isTooCloseToShip = false; for (int i = 0; i < m_spaceShipSystem.size(); i++) { if (m_spaceShipSystem.getSpaceShip(i).checkExtendedCollision(newAsteroid)) { isTooCloseToShip = true; } } // also make sure that the asteroid is not stuck inside of another asteroid isInsideOfAsteroid = false; if (!isTooCloseToShip) { for (int i = 0; i < m_asteroids.Count(); i++) { if (m_asteroids[i].checkCollision(newAsteroid)) { isInsideOfAsteroid = true; } } } // if it is too close to a ship or inside of another asteroid, then re-randomize its position until it isn't if (isTooCloseToShip || isInsideOfAsteroid) { newAsteroid.randomizePosition(); } } while(isTooCloseToShip || isInsideOfAsteroid); // store the asteroid if (newAsteroid.bigAsteroid) { m_numberOfBigAsteroids++; } else { m_numberOfSmallAsteroids++; } m_asteroids.Add(newAsteroid); }
public void handleCollisions() { if (m_projectileSystem == null || m_spaceShipSystem == null || m_asteroidSystem == null || m_explosionSystem == null || m_scoreSystem == null) { return; } // remove any collisions that have been handled and are no longer in collision for (int i = 0; i < m_asteroidSystem.size(); i++) { Asteroid a1 = m_asteroidSystem.getAsteroid(i); for (int j = 0; j < m_asteroidSystem.size(); j++) { Asteroid a2 = m_asteroidSystem.getAsteroid(j); if (i != j && (a1.isCollidingWith(a2) && !a1.checkCollision(a2)) || (a2.isCollidingWith(a1) && !a2.checkCollision(a1))) { a1.removeCollision(a2); a2.removeCollision(a1); } } } // check asteroid > asteroid collision for (int i = 0; i < m_asteroidSystem.size(); i++) { for (int j = i + 1; j < m_asteroidSystem.size(); j++) { if (i != j && m_asteroidSystem.getAsteroid(i).checkCollision(m_asteroidSystem.getAsteroid(j))) { handleCollision(m_asteroidSystem.getAsteroid(i), m_asteroidSystem.getAsteroid(j)); } } // check projectile > asteroid collision for (int j = 0; j < m_projectileSystem.size(); j++) { if (m_projectileSystem.getProjectile(j).checkCollision(m_asteroidSystem.getAsteroid(i))) { // [potentially] create a cluster of smaller asteroids if this was a big asteroid m_scoreSystem.addPoints(m_projectileSystem.getProjectile(j).getProjectileSource(), (m_asteroidSystem.getAsteroid(i).bigAsteroid) ? ScoreType.BigAsteroid : ScoreType.SmallAsteroid); if (m_asteroidSystem.getAsteroid(i).bigAsteroid) { m_asteroidSystem.spawnAsteroidCluster(m_asteroidSystem.getAsteroid(i)); } m_explosionSystem.spawnExplosion(m_asteroidSystem.getAsteroid(i)); clearCollisions(m_asteroidSystem.getAsteroid(i)); m_asteroidSystem.removeAsteroid(i); m_projectileSystem.removeProjectile(j); i--; j--; break; } } } // check ship > ship collision for (int i = 0; i < m_spaceShipSystem.size(); i++) { for (int j = i + 1; j < m_spaceShipSystem.size(); j++) { if (i != j && m_spaceShipSystem.getSpaceShip(i).checkCollision(m_spaceShipSystem.getSpaceShip(j))) { m_explosionSystem.spawnExplosion(m_spaceShipSystem.getSpaceShip(i)); m_explosionSystem.spawnExplosion(m_spaceShipSystem.getSpaceShip(j)); m_spaceShipSystem.getSpaceShip(i).reset(); m_spaceShipSystem.getSpaceShip(j).reset(); } } // check ship > asteroid collision for (int j = 0; j < m_asteroidSystem.size(); j++) { if (m_spaceShipSystem.getSpaceShip(i).checkCollision(m_asteroidSystem.getAsteroid(j))) { m_explosionSystem.spawnExplosion(m_spaceShipSystem.getSpaceShip(i)); m_spaceShipSystem.getSpaceShip(i).reset(); clearCollisions(m_asteroidSystem.getAsteroid(j)); m_asteroidSystem.removeAsteroid(j); j--; } } // check projectile > ship collision for (int j = 0; j < m_projectileSystem.size(); j++) { if (!m_projectileSystem.getProjectile(j).getProjectileSource().Equals(m_spaceShipSystem.getSpaceShip(i)) && m_projectileSystem.getProjectile(j).checkCollision(m_spaceShipSystem.getSpaceShip(i))) { m_scoreSystem.addPoints(m_projectileSystem.getProjectile(j).getProjectileSource(), ScoreType.SpaceShip); m_explosionSystem.spawnExplosion(m_spaceShipSystem.getSpaceShip(i)); m_spaceShipSystem.getSpaceShip(i).reset(); m_projectileSystem.removeProjectile(j); j--; } } } }