Exemplo n.º 1
0
    // Update is called once per frame
    void Update()
    {
        //inputs
        if (player != null)
        {
            horizontalInput    = player.GetAxis("Horizontal");
            verticalInput      = player.GetAxis("Vertical");
            aimHorizontalInput = player.GetAxis("AimHorizontal");
            aimVerticalInput   = player.GetAxis("AimVertical");
            if (aimHorizontalInput != 0 && aimVerticalInput != 0)
            {
                aimDirection = new Vector2(aimHorizontalInput, aimVerticalInput).normalized;
            }
            throwInput    = player.GetButton("Throw");
            interactInput = player.GetButton("Interact");
            jumpInput     = player.GetButton("Jump");
            if (player.GetButtonDown("Jump") && isGrounded)
            {
                //apply an impulse when the player presses jump
                rb.AddForce(Vector2.up * jumpImpulse, ForceMode2D.Impulse);
                jumpHold   = true;
                isGrounded = false;
                currentJumpHoldVelocity = jumpHoldVelocity;
                audioSource.PlayOneShot(audioJump);
                //jump particles
                if (jumpParticlesPrefab != null)
                {
                    Instantiate(jumpParticlesPrefab, transform.position, Quaternion.identity);
                }
            }
            if (!jumpInput)
            {
                jumpHold = false;
            }

            //swap resources
            if (player.GetButtonDown("SwapRight"))
            {
                resourceThrower.SwapResourceRight();
            }
            if (player.GetButtonDown("SwapLeft"))
            {
                resourceThrower.SwapResourceLeft();
            }
        }

        //deceleration
        if (horizontalInput >= 0 && currentSpeed < 0)
        {
            currentSpeed += deceleration * Time.deltaTime;
            if (currentSpeed > 0)
            {
                currentSpeed = 0;
            }
        }
        if (horizontalInput <= 0 && currentSpeed > 0)
        {
            currentSpeed -= deceleration * Time.deltaTime;
            if (currentSpeed < 0)
            {
                currentSpeed = 0;
            }
        }

        //acceleration
        if (horizontalInput > 0)
        {
            if (currentSpeed < 1)
            {
                currentSpeed += acceleration * Time.deltaTime;
                if (currentSpeed >= 1)
                {
                    currentSpeed = 1;
                }
            }
        }
        if (horizontalInput < 0)
        {
            if (currentSpeed > -1)
            {
                currentSpeed -= acceleration * Time.deltaTime;
                if (currentSpeed <= -1)
                {
                    currentSpeed = -1;
                }
            }
        }

        //the force gained by holding jump decreases over time
        currentJumpHoldVelocity = Mathf.Max(0, currentJumpHoldVelocity - jumpHoldVelocityDecay * Time.deltaTime);

        //are we grounded?
        RaycastHit2D boxCast = Physics2D.BoxCast(transform.position, new Vector2(boxCollider.size.x, boxCollider.size.y / 2), 0, Vector2.down, boxCollider.size.y / 2 + 0.1f, groundLayers);

        if (boxCast.collider != null)
        {
            if (!isGrounded)
            {
                if (landParticlesPrefab != null)
                {
                    Instantiate(landParticlesPrefab, transform.position, Quaternion.identity);
                }
            }
            isGrounded = true;
        }
        else
        {
            isGrounded = false;
        }


        // Interact with breakable objects
        if (interactInput)
        {
            Collider2D[] colliders = Physics2D.OverlapCircleAll(transform.position, interactRadius, 1 << LayerMask.NameToLayer("Breakable"));
            foreach (Collider2D breakableCollider in colliders)
            {
                Breakable breakable = breakableCollider.GetComponentInParent <Breakable>();
                if (breakable != null)
                {
                    breakable.Activate();
                }
            }
        }
    }