예제 #1
0
        private void HandlePlayer()
        {
            #region Movement
            //var for player current top location
            double top  = _player.GetPositionInCanvas().Top;
            double left = _player.GetPositionInCanvas().Left;

            //move right
            if (_keys.Contains("Right"))
            {
                _newPosition = new Point(top, _player.GetMoveRight());
                if (!_player.IsCollidingWithMap(_map.GetBlocks(), _newPosition))
                {
                    _rightKeyTutorialFlag = true;
                    _player.MoveRight(_game);
                }
            }

            //move left
            if (_keys.Contains("Left"))
            {
                _newPosition = new Point(top, _player.GetMoveLeft());
                if (!_player.IsCollidingWithMap(_map.GetBlocks(), _newPosition))
                {
                    _leftKeTutorialFlag = true;
                    _player.MoveLeft();
                }
            }

            //Up to activate jetPack
            if (_keys.Contains("Up"))
            {
                _newPosition = new Point(_player.GetMoveUp(), left);

                if (_player.GetFuel() > 0)
                {
                    _player.ConsumeFuel();
                    InitializeFuelBar();
                    if (!_player.IsCollidingWithMap(_map.GetBlocks(), _newPosition))
                    {
                        _jump          = true;
                        _forceOnPlayer = 0;
                        _player.MoveUp();
                        jetPackFx.Play();
                    }
                }
                else if (_player.GetFuel() <= 0)
                {
                    jetPackEmptyFx.Play();
                    _jump = false;
                }
            }
            //released Up will resault in activating gravity
            if (!_keys.Contains("Up"))
            {
                _jump = false;
                jetPackFx.Stop();
            }
            #endregion
            #region Actions

            //Space to shoot
            if (_keys.Contains("Space"))
            {
                if (!_shoot)
                {
                    if (_player.GetAmmo() > 0)
                    {
                        _player.Shoot(_game, _map);
                        InitializeAmmo();
                        _shoot = true;
                        shotFx.Play();
                    }
                    else
                    {
                        emptyShotFx.Play();
                    }
                }
            }
            //only one shot per click
            if (!_keys.Contains("Space"))
            {
                _shoot = false;
                shotFx.Stop();
            }

            //flag for tutorial
            if (_keys.Contains("Enter"))
            {
                _enterKeyTutorialFlag = true;
            }

            if (_keys.Contains("Escape"))
            {
                if (_isPauseMenuOpen)
                {
                    ClosePauseMenu();
                }
                else
                {
                    OpenPauseMenu();
                }
            }
            #endregion
            #region Collisions with other objects
            //colision with enemy shots
            foreach (Enemy enemy in _map.GetEnemies())
            {
                if (_player.IsCollidingWithProjectile(enemy.GetShots(), _player.GetPositionInCanvas(), out Projectile projectile))
                {
                    //hit the player
                    _player.Hit(enemy.shotDmg);
                    InitializeHealthBar();
                    playerHit.Play();
                    //remove the shot from list
                    projectile.Remove(enemy.GetShots());
                }
            }

            //colision with fixed objects
            if (_player.IsCollidingWithFixedObject(_map.GetFixedObjects(), _player.GetPositionInCanvas(), out FixedObject fixedObject))
            //only null when false
            {
                pickUpFx.Play();

                //do action for the fixed object
                fixedObject.Action(_player, _map);

                if (fixedObject is HealthBoost)
                {
                    InitializeHealthBar();
                }
                if (fixedObject is FuelTank)
                {
                    InitializeFuelBar();
                    _fuelTutorialFlag = true;
                }
            }

            //collision with boss shots

            if (_map.GetBoss() != null)
            {
                if (_player.IsCollidingWithProjectile(_map.GetBoss().GetShots(), _player.GetPositionInCanvas(), out Projectile projectile))
                {
                    //hit the player
                    if (projectile is Bolder)
                    {
                        _player.Hit(Boss.BolderDamage);
                    }
                    if (projectile is Shot)
                    {
                        _player.Hit(Boss.ShotDmg);
                    }
                    InitializeHealthBar();

                    //remove the shot from list
                    projectile.Remove(_map.GetBoss().GetShots());
                }
            }
            #endregion
            //if fallse of the map end the game
            if (_player.GetPositionInCanvas().Top + _player.Image.ActualHeight >= _game.ActualHeight)
            {
                _player.Kill();
            }

            //check if player is dead
            if (!_player.IsAlive)
            {
                GameOver();
            }
        }