Example #1
0
    void Update()
    {
        // check for win condition
        if (Win())
        {
            Debug.Log("win!");
            SceneManager.LoadScene("VictoryScene", LoadSceneMode.Single);
        }

        if (TouchingWall())
        {
            lastTimeTouchWall = Time.time;
        }

        input.RegisterInput();

        #region Movement

        float oldVelocityX = velocity.x;

        if (raycastCollider.collisionInfo.AnyBot || raycastCollider.platformCollisionInfo.AnyBot)
        {
            timers.StartTimer("coyoteBuffer");
        }


        velocity.x  = input.axisInput.x * moveSpeed;
        velocity.y -= gravity * Time.deltaTime;

        if (raycastCollider.collisionInfo.AnyBot || raycastCollider.platformCollisionInfo.AnyBot || raycastCollider.collisionInfo.AnyTop)
        {
            velocity.y = 0;
        }

        // Jump
        // Potential bug with releasing the jump button possibly cancelling upward momentum. Fix not needed rn
        if (input.jumpDown)
        {
            timers.StartTimer("jumpBuffer");
        }

        if (input.axisInput.y < 0)
        {
            timers.StartTimer("platformFallThrough");
        }

        if (timers.Active("jumpBuffer") && !timers.Expired("jumpBuffer") &&
            timers.Active("coyoteBuffer") && !timers.Expired("coyoteBuffer"))
        {
            velocity.y = jumpVelocityMax;
            timers.SetActive("jumpBuffer", false);
            timers.SetActive("coyoteBuffer", false);

            // if jump button released before touching the ground
            if (!input.jump)
            {
                velocity.y = Mathf.Min(velocity.y, jumpVelocityMin);
            }
        }
        else if (input.jumpRelease)
        {
            velocity.y = Mathf.Min(velocity.y, jumpVelocityMin);
        }

        Vector2 deltaPosition = velocity * Time.deltaTime;

        // Dash
        if (input.dash && (!timers.Active("dashBuffer") || timers.Expired("dashBuffer")) && velocity.x != 0)
        {
            timers.StartTimer("dashBuffer");
            if (velocity.x < 0)
            {
                deltaPosition += new Vector2(-dashDistance, 0);
            }
            else if (velocity.x > 0)
            {
                deltaPosition += new Vector2(dashDistance, 0);
            }
            anim?.SetState(AnimState.Dash);
        }

        // wall jump
        if (Time.time - lastTimeTouchWall <= wallJumpCooldownSeconds && input.jumpDown)
        {
            velocity.y = jumpVelocityMax;
        }

        // wall slide: touching a wall while in the air, also didn't change direction or jump
        if (TouchingWall() && oldVelocityX * velocity.x >= 0 && !input.jumpDown)
        {
            velocity.x = oldVelocityX;
            velocity.y = -wallSlideSpeed;
            Move(velocity * Time.deltaTime);
            return;
        }

        UpdateAnimStates();

        // terminal velocity
        velocity.y = Mathf.Clamp(velocity.y, -terminalVelocity, Mathf.Infinity);

        Move(deltaPosition);
        #endregion

        // // Shop
        // if (input.health && stats.getHealthUpgradeCost() <= currMoney) {
        //  currMoney -= stats.getHealthUpgradeCost();
        //  stats.incrementHealth();
        // } else if (input.health) {
        //  // Not enough money
        // }

        // if (input.defense && stats.getDefenseUpgradeCost() <= currMoney) {
        //  currMoney -= stats.getDefenseUpgradeCost();
        //  stats.incrementDefense();
        // } else if (input.defense) {
        //  // Not enough money
        // }

        // if (input.damage && stats.getDamageUpgradeCost() <= currMoney) {
        //  currMoney -= stats.getDamageUpgradeCost();
        //  stats.incrementDamage();
        // } else if (input.damage) {
        //  // Not enough money
        // }
    }