Beispiel #1
0
    private IEnumerator ClimbOntoObstacleCoroutine(Vector2 position, float changeRate)
    {
        while (playerStatusVariables.isCrouching)
        {
            yield return(new WaitForFixedUpdate());
        }

        var playerSizeX = (position.x > rigidbody2D.position.x)
            ? capsuleCollider2D.size.x / 2
            : -capsuleCollider2D.size.x / 2;
        var playerSizeY      = capsuleCollider2D.size.y / 2;
        var desiredPositionX = position.x + playerSizeX;
        var desiredPositionY = position.y + playerSizeY;
        var f = 0.0f;
        var initialPositionForY = rigidbody2D.position;
        var initialPositionForX = new Vector2(rigidbody2D.position.x, desiredPositionY);

        // Bug Corrigido, usar a posição do rigidBody no lerp era a causa, pode ser um quirck do unity
        while (!MathHelpers.Approximately(rigidbody2D.position.x, desiredPositionX, 0.01f) ||
               !MathHelpers.Approximately(rigidbody2D.position.y, desiredPositionY, 0.01f))
        {
            if (!MathHelpers.Approximately(rigidbody2D.position.y, desiredPositionY, 0.001f))
            {
                f += changeRate;
                if (f >= 1)
                {
                    f = 1;
                }

                rigidbody2D.MovePosition(Vector2.Lerp(initialPositionForY,
                                                      new Vector2(initialPositionForY.x, desiredPositionY), f));
            }
            else
            {
                if (MathHelpers.Approximately(rigidbody2D.position.x, initialPositionForX.x, 0.01f) &&
                    MathHelpers.Approximately(rigidbody2D.position.y, initialPositionForX.y, 0.01f))
                {
                    f = 0;
                }
                f += changeRate;
                if (f >= 1)
                {
                    f = 1;
                }
                rigidbody2D.MovePosition(Vector2.Lerp(initialPositionForX,
                                                      new Vector2(desiredPositionX, initialPositionForX.y), f));
            }


            yield return(new WaitForFixedUpdate());
        }
        CoroutineManager.FindCoroutine("ClimbOntoObstacleCoroutine").IsRunning = false;
        playerController.RevokeControl(false, ControlTypeToRevoke.AllMovement);
    }
Beispiel #2
0
 protected static IEnumerator RevokeControlCoroutine(float time, ControlTypeToRevoke controlTypeToRevoke,
                                                     RevokeControlVariables revokeControlVariables,
                                                     bool revoke, MonoBehaviour monoBehaviour)
 {
     for (var i = 0; i < 1; i++)
     {
         RevokeControlSelection(revoke, controlTypeToRevoke, revokeControlVariables, false);
         yield return(new WaitForSeconds(time));
     }
     RevokeControlSelection(revoke, controlTypeToRevoke, revokeControlVariables, true);
     CoroutineManager.FindCoroutine("RevokeControlCoroutine", monoBehaviour).IsRunning = false;
 }
Beispiel #3
0
    private IEnumerator ClimbOntoLadderCoroutine(Vector2 position, float changeRate)
    {
        var f = 0.0f;
        var initialPosition = rigidbody2D.position;

        while (!MathHelpers.Approximately(rigidbody2D.position.x, position.x, 0.01f) ||
               !MathHelpers.Approximately(rigidbody2D.position.y, position.y, 0.01f))
        {
            f += changeRate;

            rigidbody2D.MovePosition(Vector2.Lerp(initialPosition, new Vector2(position.x, position.y), f));
            yield return(new WaitForFixedUpdate());
        }

        CoroutineManager.FindCoroutine("ClimbOntoLadderCoroutine").IsRunning = false;
        playerController.RevokeControl(false, ControlTypeToRevoke.LadderMovement);
    }
Beispiel #4
0
    public void ClimbOntoObstacle(Vector2 position)
    {
        PhysicsHelpers.SwitchGravity(rigidbody2D, false, currentGravityScale);
        PhysicsHelpers.ResetVelocityX(rigidbody2D);
        PhysicsHelpers.ResetVelocityY(rigidbody2D);
        rigidbody2D.isKinematic = true;

        playerController.RevokeControl(true, ControlTypeToRevoke.AllMovement);

        var coroutine = CoroutineManager.FindCoroutine("ClimbOntoObstacleCoroutine");

        if (coroutine == null)
        {
            CoroutineManager.AddCoroutine(ClimbOntoObstacleCoroutine(position, climbingObstacleSmoothness),
                                          "ClimbOntoObstacleCoroutine");
        }
    }
Beispiel #5
0
    protected virtual void RevokeControl(float timeToRevoke, bool revoke,
                                         ControlTypeToRevoke controlTypeToRevoke, RevokeControlVariables revokeControlVariables,
                                         MonoBehaviour monoBehaviour)
    {
        var coroutine = CoroutineManager.FindCoroutine("RevokeControlCoroutine", monoBehaviour);

        if (coroutine == null)
        {
            CoroutineManager.AddCoroutine(
                RevokeControlCoroutine(timeToRevoke, controlTypeToRevoke, revokeControlVariables, true, monoBehaviour),
                "RevokeControlCoroutine", monoBehaviour);
        }
        else if (!coroutine.IsRunning)
        {
            CoroutineManager.DeleteCoroutine("RevokeControlCoroutine", monoBehaviour);
            CoroutineManager.AddCoroutine(
                RevokeControlCoroutine(timeToRevoke, controlTypeToRevoke, revokeControlVariables, true, monoBehaviour),
                "RevokeControlCoroutine", monoBehaviour);
        }
    }
Beispiel #6
0
    public void ClimbOntoLadder(Transform ladderTransform)
    {
        PhysicsHelpers.IgnoreCollision(capsuleCollider2D,
                                       ladderTransform.GetComponent <LadderController>().adjacentCollider, true);
        PhysicsHelpers.SwitchGravity(rigidbody2D, false, currentGravityScale);
        PhysicsHelpers.ResetVelocityX(rigidbody2D);
        PhysicsHelpers.ResetVelocityY(rigidbody2D);
        rigidbody2D.isKinematic = true;
        apostleController.RevokeControl(true, ControlTypeToRevoke.AllMovement);

        var coroutine = CoroutineManager.FindCoroutine("ClimbOntoLadderCoroutine", this);

        if (coroutine == null)
        {
            CoroutineManager.AddCoroutine(
                ladderTransform.gameObject.layer != LayerMask.NameToLayer("Top Ladder")
                    ? ClimbOntoLadderCoroutine(ladderTransform.GetChild(0).position, climbingLadderSmoothness)
                    : ClimbOntoLadderCoroutine(ladderTransform.GetChild(0).position, climbingLadderSmoothness * 0.50f),
                "ClimbOntoLadderCoroutine", this);
        }
    }
Beispiel #7
0
    public override void StartMovement()
    {
        playerController.CheckForVerticalInput();

        playerStatusVariables.canJump = CheckGroundForJump();

        playerStatusVariables.isOnAir = playerStatusVariables.CheckIsOnAir();

        if (playerStatusVariables.canClimbLadder && playerController.ClimbLadderPress)
        {
            playerStatusVariables.isClimbingLadder =
                playerStatusVariables.canClimbLadder && playerController.ClimbLadderPress ||
                playerStatusVariables.isClimbingLadder;
        }

        if (playerStatusVariables.canClimbObstacle && playerController.ClimbObstaclePress)
        {
            playerStatusVariables.isClimbingObstacle =
                playerStatusVariables.canClimbObstacle && playerController.ClimbObstaclePress ||
                playerStatusVariables.isClimbingObstacle;
        }

        playerStatusVariables.isJumping = playerStatusVariables.canJump && playerController.Jump &&
                                          !playerStatusVariables.isClimbingObstacle && player.CheckStamina(20, true);


        //Para velocidades ridiculamente altas, vai bugar
        if (playerStatusVariables.isClimbingLadder && playerStatusVariables.canJump)
        {
            var coroutine = CoroutineManager.FindCoroutine("ClimbOntoLadderCoroutine");
            if (coroutine != null && !coroutine.IsRunning)
            {
                PhysicsHelpers.IgnoreCollision(capsuleCollider2D,
                                               GetLadderPosition().GetComponent <LadderController>().adjacentCollider, false);
                playerStatusVariables.isClimbingLadder = false;
                playerController.RevokeControl(0.3f, false, ControlTypeToRevoke.AllMovement, monoBehaviour);
                PhysicsHelpers.SwitchGravity(rigidbody2D, true, currentGravityScale);
                PhysicsHelpers.ResetVelocityY(rigidbody2D);
                rigidbody2D.isKinematic = false;

                CoroutineManager.DeleteCoroutine("ClimbOntoLadderCoroutine");
            }
        }

        if (playerStatusVariables.isClimbingObstacle && playerStatusVariables.canJump)
        {
            var coroutine = CoroutineManager.FindCoroutine("ClimbOntoObstacleCoroutine");
            if (coroutine != null && !coroutine.IsRunning)
            {
                playerStatusVariables.isClimbingObstacle = false;
                playerController.RevokeControl(0.3f, false, ControlTypeToRevoke.AllMovement, monoBehaviour);
                PhysicsHelpers.SwitchGravity(rigidbody2D, true, currentGravityScale);
                rigidbody2D.isKinematic = false;

                CoroutineManager.DeleteCoroutine("ClimbOntoObstacleCoroutine");
            }
        }

        CheckForClimbingStairs();

        if (playerStatusVariables.isOnAir)
        {
            VerticalMovementState = VerticalMovementState.OnAir;
        }
        else
        {
            if (playerStatusVariables.isJumping)
            {
                VerticalPressMovementState = VerticalPressMovementState.Jump;
            }
            else if (playerStatusVariables.canClimbLadder && playerController.ClimbLadderPress)
            {
                VerticalPressMovementState = VerticalPressMovementState.ClimbLadder;
            }
            else if (playerStatusVariables.canClimbObstacle && playerController.ClimbObstaclePress)
            {
                VerticalPressMovementState = VerticalPressMovementState.ClimbObstacle;
            }

            if (playerStatusVariables.isClimbingLadder &&
                !MathHelpers.Approximately(playerController.VerticalMovement, 0, float.Epsilon))
            {
                VerticalMovementState = VerticalMovementState.ClimbingLadder;
            }
            else if (playerStatusVariables.isClimbingObstacle)
            {
                VerticalMovementState = VerticalMovementState.ClimbingObstacle;
            }
            else if (playerStatusVariables.isJumping)
            {
                VerticalMovementState = VerticalMovementState.OnAir;
            }
            else
            {
                VerticalMovementState = VerticalMovementState.Grounded;
            }
        }
    }