private void EnemyShipAsteroidCollisions( EnemyShip enemyShip) { List<LinkedList<Asteroid>> boundAsteroids = m_level.GetAsteroidPartitionTree().GetPartitionItems(enemyShip.GeneralSpriteBox); if (boundAsteroids.Count > 0) { foreach (LinkedList<Asteroid> asteroids in boundAsteroids) { foreach (Asteroid asteroid in asteroids) { //Check for asteroids within the vicinity of the enemy ship, and check for collisions against these asteroids. //Also, if there is no collision against the enemy ship itself use the evasion prediction box of the enemy ship //to pre-emptively try to avoid collisions //The evasion box is a simple technique used to evade asteroids, however it is not entirely accurate because it uses the //enemyship sprite box to find asteroids within vicinity, this will work for our purposes. However if you want more accurate //prediction, we have to get all asteroids within partitions of the evasion box. if (enemyShip.GeneralSpriteBox.Intersects(asteroid.GeneralSpriteBox)) enemyShip.ShipCollisionHandler(2); else if (enemyShip.GetEvasionRectangle().Intersects(asteroid.GeneralSpriteBox)) enemyShip.ShipCollisionHandler(3); } } } }
private void EnemyShipBorderCollisions(EnemyShip enemyShip) { //Get the specific sprite box of the player ship for collision calculation Rectangle levelBorder = m_level.GetLevelRectangle(); Rectangle enemyShipSpriteBox = enemyShip.GeneralSpriteBox; //Check for Collisions with level border and enemy ship , and calculates re-adjust necessary and passes it to the ship's collision handler to //re-position / other effects if (enemyShipSpriteBox.Left < levelBorder.Left || enemyShipSpriteBox.Right > levelBorder.Right || enemyShipSpriteBox.Top < levelBorder.Top || enemyShipSpriteBox.Bottom >= levelBorder.Bottom) enemyShip.ShipCollisionHandler(1); }
private void InitializeEnemies() { for( int i = 0; i < enemyCount; i++) { //Parameters to initialize position, and flag to see if the current position is viable or not int positionX = 0; int positionY = 0; bool goodPosition = false; //Make sure not to spawn enemies on top of any asteroids while (!goodPosition) { positionX = worldRand.Next(mapWidth - m_enemyShipTexture.Width); positionY = worldRand.Next(mapHeight - m_enemyShipTexture.Height); goodPosition = true; //Make the rectangle a tiny bit bigger to avoid ships starting near asteroids Rectangle enemyShipRect = new Rectangle(positionX-15, positionY-15, m_enemyShipTexture.Width+30, m_enemyShipTexture.Height+30); List< LinkedList<Asteroid>> boundAsteroids = m_asteroidPartitionTree.GetPartitionItems(enemyShipRect); //Make sure no enemies spawn near player at beginning if (positionX < mapWidth/4 && positionY < mapHeight/4) { goodPosition = false; } else if (boundAsteroids.Count > 0) { //ok no enemies near spawn area, use partition tree with the enemy ship rectangle to detect //if any collisions occur with the ship spawn area and nearby asteroids foreach (LinkedList<Asteroid> asteroids in boundAsteroids) { if (!goodPosition) break; foreach (Asteroid asteroid in asteroids) { if (!goodPosition) break; if (enemyShipRect.Intersects(asteroid.GeneralSpriteBox)) goodPosition = false; } } } } EnemyShip enemyShip = new EnemyShip(m_enemyShipTexture, new Vector2(positionX, positionY), m_enemyBulletTexture, m_playerShip); m_enemyShips.AddLast(enemyShip); } }