Example #1
0
    // Update is called once per frame
    void Update()
    {
        if (_isPlayingAnimation || !_player.IsTouchingGround())
        {
            if (_movable != null)
            {
                ReleaseHold();
            }
            return;
        }

        Physics2D.queriesStartInColliders = false;
        float        direction = transform.localScale.x > 0 ? 1f : -1f;
        RaycastHit2D hitBot    = Physics2D.Raycast((Vector2)_rayOriginBot.position, Vector2.right * direction, _distance, _interactableMask);
        RaycastHit2D hitMid    = Physics2D.Raycast((Vector2)_rayOriginMid.position, Vector2.right * direction, _distance, _interactableMask);

        if (!hitBot && !hitMid)
        {
            return;
        }

        RaycastHit2D hit = hitBot;

        if (hitMid)
        {
            hit = hitMid;
        }

        Tutorial tutorial = hit.transform.GetComponent <Tutorial>();

        // level 1 pull gate
        //if (hit.transform.tag == "Gate" && Input.GetButtonDown("Hold"))
        //{
        //    if (tutorial)
        //    {
        //        tutorial.Destroy();
        //    }
        //    bool moveLeft = _player.transform.position.x < hit.transform.position.x ? true : false;
        //    StartCoroutine(PullOnGate(hit.transform.GetComponent<Animator>(), 1.5f, moveLeft));
        //    return;
        //}
        // level 1 collect gravity switch layer
        if (hit.transform.tag == "Grave" && Input.GetButtonDown("Hold"))
        {
            AnimationTrigger a = hit.transform.GetComponent <AnimationTrigger>();
            if (a)
            {
                if (tutorial)
                {
                    tutorial.Destroy();
                }
                a.TriggerAnimation();
                AbilityManager.Instance.PickUpGravityLayer();
            }
            return;
        }
        // push/pull boxes
        else if (hit.transform.tag.Contains("Movable") && Input.GetButton("Hold"))
        {
            if (tutorial)
            {
                tutorial.Destroy();
            }

            _isMoving = true;
            _movable  = hit.transform.gameObject.GetComponent <Movable>();
            if (_movable.IsGrounded() || _movable.GetIsPressingButton())
            {
                HoldObject();
            }
            else
            {
                ReleaseHold();
            }
        }
        else if (_movable != null && Input.GetButtonUp("Hold"))
        {
            ReleaseHold();
        }

        if (!_player.IsTouchingGround())
        {
            ReleaseHold();
        }

        if (_isMoving)
        {
            float moveDirection = _player.MoveDirection();
            if (moveDirection == 0) // doing nothing
            {
                _playerAnimator.SetBool("IsPushing", false);
                _playerAnimator.SetBool("IsPulling", false);
            }
            else if (moveDirection < 0)
            {
                if (this.transform.position.x < _movable.transform.position.x) // pulling
                {
                    //Debug.Log("pulling");
                    _playerAnimator.SetBool("IsPushing", false);
                    _playerAnimator.SetBool("IsPulling", true);
                }
                else // pushing
                {
                    //Debug.Log("pushing");
                    _playerAnimator.SetBool("IsPushing", true);
                    _playerAnimator.SetBool("IsPulling", false);
                }
            }
            else if (moveDirection > 0)
            {
                if (this.transform.position.x < _movable.transform.position.x) // pushing
                {
                    //Debug.Log("pushing");
                    _playerAnimator.SetBool("IsPushing", true);
                    _playerAnimator.SetBool("IsPulling", false);
                }
                else // pulling
                {
                    //Debug.Log("pulling");
                    _playerAnimator.SetBool("IsPushing", false);
                    _playerAnimator.SetBool("IsPulling", true);
                }
            }
        }
        else
        {
            _playerAnimator.SetBool("IsHolding", false);
            _playerAnimator.SetBool("IsPushing", false);
            _playerAnimator.SetBool("IsPulling", false);
        }
    }