Example #1
0
    void Update()
    {
        ////////////////////////////////////////
        //USED ONLY FOR DEMO NOT FOR ACTUAL GAME
        if (coins >= winCoins)
        {
            SceneManager.LoadScene(3);
        }

        ////////////////////////////////////////
        doDashIcon();
        doDoubleJumpIcon();
        doGrappleIcon();
        if (_touchingHurtZone)
        {
            AttemptTakeDamage(1);
        }

        //DRAWING
        //Used for drawing the health
        for (int i = 0; i < hearts.Length; i++)
        {
            if (i < currentHealth)
            {
                hearts[i].sprite = fullHeart;
            }
            else
            {
                hearts[i].sprite = emptyHeart;
            }
        }


        //determines if the player is on the ground
        if (onGround)
        {
            //animator.SetBool("onGround", true);
        }
        else
        {
            //animator.SetBool("onGround", false);
        }


        //facing
        //		if (moveInput > 0)
        //		{
        //			ChangeFacingTo(1);
        //		}
        //
        //		if (moveInput < 0)
        //		{
        //			ChangeFacingTo(-1);
        //		}
        if (Math.Abs(rb.velocity.x) > 0.01)
        {
            ChangeFacingTo(rb.velocity.x);
        }


        //Actions


        if (canGrapple)
        {
            if (grappling)
            {
                dj.autoConfigureDistance = false;
                if (dj.distance >= 0)
                {
                    dj.distance -= grappleSpeed;
                }
            }

            if (Input.GetMouseButtonDown(0))
            {
                if (!grappling && currentEnergy >= grappleCost)
                {
                    //  Physics2D.Linecast(transform.position, Input.mousePosition, groundLayer);
                    Vector2 from = transform.position;
                    Vector2 to   = Camera.main.ScreenToWorldPoint(Input.mousePosition);

                    RaycastHit2D grappleDetector = Physics2D.Linecast(from, to, groundLayer);

                    //Debug.DrawLine(from, to, new Color(0, 1, 0, 0.5f), 0.1f); //only shows up when gizmos show up

                    if (grappleDetector.collider != null)
                    {
                        if (grappleDetector.collider.CompareTag("Wood"))
                        {
                            dj.connectedAnchor       = grappleDetector.point;
                            dj.autoConfigureDistance = true; //set distance
                            dj.enabled = true;


                            grappling = true;
                            //animator.SetBool("Grappling", true);

                            spendEnergy(grappleCost);
                        }
                    }
                }
                else
                {
                    dj.enabled = false;
                    grappling  = false;
                    //animator.SetBool("Grappling", false);
                }
            }
        }

        //DASH
        //https://www.youtube.com/watch?v=w4YV8s9Wi3w
        if (canDash)
        {
            if (!dashing && !grappling)
            {
                if (onGround && Input.GetKeyDown(KeyCode.LeftShift) && currentEnergy >= dashCost) //todo: replace this with the input system.
                {
                    dashing = true;
                    spendEnergy(dashCost);
                    //animator.SetBool("Dashing", true);
                    _soundManager.PlayDashSFX();
                    //jumpCount--;
                }
            }

            if (dashing)
            {
                if (currentDashTime <= 0)
                {
                    dashing = false;
                    //animator.SetBool("Dashing", false);
                    currentDashTime = dashTime;
                    rb.velocity     = Vector2.zero;
                }
                else
                {
                    currentDashTime -= Time.deltaTime;
                    if (facingRight)
                    {
                        rb.velocity = Vector2.right * dashSpeed;
                    }
                    else
                    {
                        rb.velocity = Vector2.left * dashSpeed;
                    }
                }
            }
        }

        //WALL JUMPING
        if (_canWallJump && canJump && !onGround)
        {
            int wallDirection;
            if (TouchingWallCheck(out wallDirection) && AttemptingAscent())
            {
                if (currentEnergy >= _wallJumpCost)
                {
                    spendEnergy(_wallJumpCost);
                    rb.velocity = new Vector2(jumpForce * -wallDirection, doubleJumpForce);
                    _soundManager.PlayJumpSFX();
                }
            }
        }

        //DOUBLE JUMPS
        if (canJump && canDoubleJump)
        {
            if (onGround)
            {
                jumpCount = 1;
            }

            else if (AttemptingAscent() && !dashing && !grappling && !climbing && jumpCount > 0)
            {
                onGround      = false;
                doubleJumping = true;
                jumpCount--;

                //rb.velocity = Vector2.up * doubleJumpForce;
                if (rb.velocity.y > 0)
                {
                    rb.AddForce(Vector2.up * doubleJumpForce * 25);
                }
                else
                {
                    rb.velocity = Vector2.up * doubleJumpForce;
                }

                _soundManager.PlayDashSFX();
            }
        }
        //REGULAR JUMPING
        if (canJump && onGround)
        {
            if (AttemptingAscent() && !grappling)
            {
                onGround = false;
                if (!climbing)
                {
                    Jump();
                }
            }
        }

        //LADDER CLIMBING

        if (onLadder && canClimb)
        {
            climbing = true;
        }
        if (!onLadder)
        {
            canClimb        = false;
            rb.gravityScale = gravityStore;
            climbing        = false;
        }
        if (climbing)
        {
            rb.gravityScale = 0f;


            //onGround = false;



            var climbVertical   = Input.GetAxisRaw("Vertical");
            var climbHorizontal = Input.GetAxisRaw("Horizontal");
            rb.velocity = new Vector2(climbHorizontal * climbingSpeed * 1.5f, climbVertical * climbingSpeed);
        }

        //PUSHING/PULLING
        if (canPushPull && box != null)
        {
            if (Input.GetKeyDown(KeyCode.C))
            {
                canGrapple     = !canGrapple;
                pullingPushing = !pullingPushing;
                if (pullingPushing)
                {
                    box.GetComponent <Rigidbody2D>().bodyType          = RigidbodyType2D.Dynamic;
                    box.GetComponent <RelativeJoint2D>().connectedBody = rb;
                }
            }

            else if (!pullingPushing && box != null)
            {
                box.GetComponent <Rigidbody2D>().bodyType          = RigidbodyType2D.Static;
                box.GetComponent <FixedJoint2D>().enabled          = false;
                box.GetComponent <RelativeJoint2D>().connectedBody = null;
            }
        }



        //Jumping off chains
        if (_swinging)
        {
            var swingScript = _chainJoint.connectedBody.GetComponent <ChainGrabScript>();
            if (swingScript.GetInteractionPermitted())
            {
                if (Input.GetAxisRaw("Vertical") < -0.1)
                {
                    swingScript.TriggerInteractionTimer(0.2f);
                    SetSwinging(false);
                }
                else if (Input.GetAxisRaw("Vertical") > 0.5)
                {
                    swingScript.TriggerInteractionTimer();
                    SetSwinging(false);
                    Jump();
                }
            }
        }



        //HEALING
        if (Input.GetKeyDown(KeyCode.E) && CurrentPotions != 0 && currentHealth != maxHealth && currentHealth != 0)
        {
            heal(1);
            Camera.main.GetComponent <CameraHintingCore>().SetHurtEffect(0);
            CurrentPotions--;
            setPotionText();

            _soundManager.PlayHealSFX();
        }


        //INTERACTING WITH DOORS AND CHESTS
        if (Input.GetKeyDown(KeyCode.F) && interactable != null)
        {
            interactable.GetComponent <InteractiveObjectScript>().PlayerInteract(this);
            interactable = null;
        }


        //TESTS
        //TESTING LOSING HEALTH THIS IS TEMPORARY
        if (Input.GetKeyDown(KeyCode.O))
        {
            AttemptTakeDamage(1);
        }

        if (Input.GetKeyDown(KeyCode.P))
        {
            heal(1);
        }

        //TESTING GAINING KEYS
        if (Input.GetKeyDown(KeyCode.K))
        {
            if (CurrentChestKeys != 0)
            {
                CurrentChestKeys--;
                setChestKeyText();
            }
        }

        if (Input.GetKeyDown(KeyCode.L))
        {
            CurrentChestKeys++;
            setChestKeyText();
        }

        //TESTING GAINING POTIONS
        if (Input.GetKeyDown(KeyCode.N))
        {
            if (CurrentPotions != 0)
            {
                CurrentPotions--;
                setPotionText();
            }
        }

        if (Input.GetKeyDown(KeyCode.M))
        {
            CurrentPotions++;
            setPotionText();
        }

        //PAUSE BUTTON
        if (Input.GetKeyDown(KeyCode.Escape))
        {
            if (!paused)
            {
                paused         = true;
                Time.timeScale = 0;
                pauseMenu.gameObject.SetActive(true);
            }
            else
            {
                paused         = false;
                Time.timeScale = 1;
                pauseMenu.gameObject.SetActive(false);
            }
        }

        _animator.UpdateAnimationState();
    }