Пример #1
0
    void SwitchToState(State nextState)
    {
        if (currentState == nextState)
        {
            return;
        }

        if (nextState == State.Idle || nextState == State.Walking)
        {
            climbingLadder = null;
            GetComponent <Rigidbody2D>().isKinematic = false;
        }
        else if (nextState == State.Jumping)
        {
            shouldJump = true;
            soundEffectPlayer.PlayWalkEffect(false);
            soundEffectPlayer.PlayJumpEffect();
        }
        else if (nextState == State.Climbing)
        {
            GetComponent <Rigidbody2D>().isKinematic = true;
        }
        else if (nextState == State.Dead)
        {
            // Play the death animation
            if (anim != null)
            {
                anim.SetBool("Death", true);
            }

            // Set Mario's velocity to 0, and increase his mass so that the barrels can't push him around :)
            GetComponent <Rigidbody2D>().velocity = Vector2.zero;
            GetComponent <Rigidbody2D>().mass     = 1000;

            // Stop the background music & walking sound, and play the death sound effect instead
            soundEffectPlayer.PlayWalkEffect(false);
            soundEffectPlayer.StopBackgroundMusic();
            soundEffectPlayer.PlayDieEffect();

            // Don't spawn any more barrels
            if (barrelSpawner != null)
            {
                barrelSpawner.Stop();
            }

            // Show the Game Over sprite
            if (gameOverSprite != null)
            {
                gameOverSprite.enabled = true;
            }
        }

        currentState = nextState;
    }
Пример #2
0
    void FixedUpdate()
    {
        if (isDead)
        {
            return;
        }

        if (isJumping && grounded && GetComponent <Rigidbody2D>().velocity.y <= 0)
        {
            isJumping = false;
        }

        float h = Input.GetAxis("Horizontal");

        if (climbingLadder != null)
        {
            float v = Input.GetAxis("Vertical");

            float dir = v == 0 ? 0 : Mathf.Sign(v);

            Collider2D ladder = FindLadderInDirection((int)dir, spriteHeight * 0.2f);
            if (ladder != climbingLadder)
            {
                if (grounded)
                {
                    climbingLadder = null;
                    GetComponent <Rigidbody2D>().isKinematic = false;
                }
                else
                {
                    GetComponent <Rigidbody2D>().velocity = new Vector2(0, 0);
                }
            }
            else
            {
                GetComponent <Rigidbody2D>().velocity = new Vector2(0, dir * maxSpeed * 0.5f);
            }

            // Play walking sound
            if (Mathf.Abs(GetComponent <Rigidbody2D>().velocity.y) > 0.01)
            {
                soundEffectPlayer.PlayWalkEffect(true);
            }
            else
            {
                soundEffectPlayer.PlayWalkEffect(false);
            }
        }
        else
        {
            float dir = h == 0 ? 0 : Mathf.Sign(h);
            GetComponent <Rigidbody2D>().velocity = new Vector2(dir * maxSpeed, GetComponent <Rigidbody2D>().velocity.y);

            if (h > 0 && !facingRight)
            {
                Flip();
            }
            else if (h < 0 && facingRight)
            {
                Flip();
            }

            if (shouldJump)
            {
                soundEffectPlayer.PlayJumpEffect();

                // Add a vertical force to the player.
                GetComponent <Rigidbody2D>().AddForce(new Vector2(0f, jumpForce));

                // Make sure the player can't jump again until the jump conditions from Update are satisfied.
                shouldJump = false;
                isJumping  = true;
            }

            // Play walking sound
            if (!isJumping && Mathf.Abs(GetComponent <Rigidbody2D>().velocity.x) > 0.01f)
            {
                soundEffectPlayer.PlayWalkEffect(true);
            }
            else
            {
                soundEffectPlayer.PlayWalkEffect(false);
            }
        }

        // Update the animation state
        if (anim != null)
        {
            anim.SetFloat("Speed", Mathf.Abs(h));
        }
        if (anim != null)
        {
            anim.SetBool("Jumping", isJumping);
        }
        if (anim != null)
        {
            anim.SetBool("Climbing", climbingLadder != null);
        }
    }