示例#1
0
        private void HandleCollisions(GameTime gameTime)
        {
            // see if a player shot hit an enemy
            for (int i = 0; i < _playerShots.Count; i++)
            {
                PlayerShot playerShot = _playerShots[i];
                // check the shot and see if it it collided with an enemy
                if (playerShot != null && _enemyGroup.HandlePlayerShotCollision(_playerShots[i]))
                {
                    // remove the shot, add the score
                    _playerShots.RemoveAt(i);
                    _score += 100;
                    AudioManager.PlayCue(AudioManager.Cue.Explosion);
                }
            }

            // see if an enemy shot hit the player
            if (_player != null && _enemyGroup.HandleEnemyShotCollision(_player))
            {
                // blow up the player
                _playerExplosion = new Explosion();
                Vector2 center = _player.Position + (_player.Size / 2.0f);
                _playerExplosion.Position = center - (_playerExplosion.Size / 2.0f);
                _player = null;
                AudioManager.PlayCue(AudioManager.Cue.Explosion);
            }

            // see if an enemy hit the player directly
            if (_player != null && _enemyGroup.HandleEnemyPlayerCollision(_player))
            {
                // blow up the player
                _playerExplosion = new Explosion();
                Vector2 center = _player.Position + (_player.Size / 2.0f);
                _playerExplosion.Position = center - (_playerExplosion.Size / 2.0f);
                _player = null;
                AudioManager.PlayCue(AudioManager.Cue.Explosion);
                _loseGame = true;
            }

            // if the player explosion animation is running, update it
            if (_playerExplosion != null)
            {
                // if this is the last frame
                if (_playerExplosion.Update(gameTime) && !_loseGame)
                {
                    // remove it
                    _playerExplosion = null;

                    // we lose if we have no lives left
                    if (_lives == 0)
                    {
                        _loseGame = true;
                    }
                    else
                    {
                        // subract 1 life and reset the board
                        _lives--;
                        _enemyGroup.Reset();
                        _playerShots.Clear();
                        _player          = new Player();
                        _player.Position = new Vector2(AlienAttackGame.ScreenWidth / 2 - _player.Width / 2, AlienAttackGame.ScreenHeight - 100);
                    }
                }
            }
        }