Ejemplo n.º 1
0
    private void CheckInput()
    {
        #region Gravity

        if (_object.isGrounded)
        {
            _velocity.y = 0;
            canFloat    = false; //
            floatLength = 1f;    //
        }

        _velocity.y += gravity * Time.deltaTime;

        #endregion

        #region Horizontal Movement

        normalizedHorizontalSpeed = moveStick.Horizontal;

        if (normalizedHorizontalSpeed > 0 || Input.GetKey(KeyCode.RightArrow)) // Right
        {
            if (normalizedHorizontalSpeed < .33)
            {
                normalizedHorizontalSpeed = .5f;
            }
            else
            {
                normalizedHorizontalSpeed = 1;
            }
            photonView.RPC("FlipSprite_RIGHT", RpcTarget.AllBuffered);
            cameraController.m_XOffset = cameraOffset;
        }
        else if (normalizedHorizontalSpeed < 0 || Input.GetKey(KeyCode.LeftArrow)) // Left
        {
            if (normalizedHorizontalSpeed > -.33)
            {
                normalizedHorizontalSpeed = -.5f;
            }
            else
            {
                normalizedHorizontalSpeed = -1;
            }
            photonView.RPC("FlipSprite_LEFT", RpcTarget.AllBuffered);
            cameraController.m_XOffset = -cameraOffset;
        }

        // Apply horizontal speed smoothing it. dont really do this with Lerp. Use SmoothDamp or something that provides more control
        var smoothedMovementFactor = _object.isGrounded ? groundFriction : airStrafe;
        _velocity.x = Mathf.Lerp(_velocity.x, normalizedHorizontalSpeed * runSpeed, Time.deltaTime * smoothedMovementFactor);

        // Platform Dropping
        if (_object.isGrounded && Input.GetKey(KeyCode.DownArrow))
        {
            _velocity.y *= 3f;
            _object.ignoreOneWayPlatformsThisFrame = true;
        }

        #endregion

        #region Vertical Movement

        if (_object.isGrounded && (jumpButton.pressed || Input.GetKey(KeyCode.Space))) //Input.GetKeyDown( KeyCode.UpArrow ) )
        {
            _velocity.y = Mathf.Sqrt(2f * jumpForce * -gravity);
            //_animator.Play( Animator.StringToHash( "Jump" ) );

            floatTimer = .5f;
        }

        if (!_object.isGrounded)
        {
            if (floatTimer < 0 && floatLength > 0)
            {
                canFloat = true;
            }
            else
            {
                floatTimer -= Time.deltaTime;
            }
        }

        if (canFloat && (jumpButton.pressed || Input.GetKey(KeyCode.Space))) // Floating
        {
            _velocity.y = 0;
            if (floatLength < 0)
            {
                canFloat = false;
            }
            else
            {
                floatLength -= Time.deltaTime;
            }
        }

        if (_object.collisionState.right)
        {
            _velocity.y = wallCling;
            canFloat    = false;
            floatLength = 0f;
            if (jumpButton.pressed || Input.GetKey(KeyCode.Space)) //Input.GetKeyDown(KeyCode.UpArrow))
            {
                _velocity.y = Mathf.Sqrt(2f * jumpForce * -gravity);
                _velocity.x = -Mathf.Sqrt(2f * jumpForce * -gravity);
                //_animator.Play( Animator.StringToHash( "Jump" ) );
            }
        }

        if (_object.collisionState.left)
        {
            _velocity.y = wallCling;
            canFloat    = false;
            floatLength = 0f;
            if (jumpButton.pressed || Input.GetKey(KeyCode.Space)) //Input.GetKeyDown(KeyCode.UpArrow))
            {
                _velocity.y = Mathf.Sqrt(2f * jumpForce * -gravity);
                _velocity.x = Mathf.Sqrt(2f * jumpForce * -gravity);
                //_animator.Play( Animator.StringToHash( "Jump" ) );
            }
        }

        #endregion

        #region Attacking

        // Normal Attack
        if (attackTimer < 0)
        {
            canAttack = true;
        }
        else
        {
            canAttack    = false;
            attackTimer -= Time.deltaTime;
        }

        if (attackStick.IsPressed)
        {
            AimingLocation = attackStick.Direction;
            if (Mathf.Abs(AimingLocation.x) >= .08 || Mathf.Abs(AimingLocation.y) >= .08)
            {
                AimingLastFrame = true;
                AimingBox.SetActive(true);
                Aim(AimingLocation);
            }
            else
            {
                AimingLastFrame = false;
                AimingBox.SetActive(false);
            }
        }
        else if (AimingLastFrame && (Mathf.Abs(AimingLocation.x) >= .08 || Mathf.Abs(AimingLocation.y) >= .08))
        {
            AimingBox.SetActive(false);
            if (canAttack)
            {
                Attack(AimingLocation);
            }
            AimingLastFrame = false;
        }

        // Special Attack
        if (specialStick.IsPressed)
        {
            AimingLocation = specialStick.Direction;
            if (Mathf.Abs(AimingLocation.x) >= .08 || Mathf.Abs(AimingLocation.y) >= .08)
            {
                AimingSpecialLastFrame = true;
                SpecialAimingBox.SetActive(true);
                AimSpecial(AimingLocation);
            }
            else
            {
                AimingSpecialLastFrame = false;
                SpecialAimingBox.SetActive(false);
            }
        }
        else if (AimingSpecialLastFrame && (Mathf.Abs(AimingLocation.x) >= .08 || Mathf.Abs(AimingLocation.y) >= .08))
        {
            SpecialAimingBox.SetActive(false);
            SpecialAttack(AimingLocation);
            AimingSpecialLastFrame = false;
        }

        #endregion

        // Apply calculated velocity to player
        _object.move(_velocity * Time.deltaTime);
        _velocity = _object.velocity; // Update calculated object velocity
    }