private void Update()
    {
        if (Input.GetKeyDown(KeyCode.Space))
        {
            _jumpController.Jump();             // Make the player jump when "Space" is pressed
        }

        _controller.IsRunning = Input.GetKey(KeyCode.LeftShift);            // Make player run when "Left Shift" is being held

        // Get the movement direction from WASD and Arrow keys
        Vector2 MoveDir = new Vector2(Input.GetAxis("Horizontal"), Input.GetAxis("Vertical"));

        _controller.Move(MoveDir);              // move player in a specified direction
        _controller.Rotate(MoveDir);            // make player look in a specified direction
    }
Exemple #2
0
    void Update()
    {
        // Reduce pause time or unpause if pause time has run out.
        if (pauseTime > 0)
        {
            pauseTime -= Time.deltaTime;
        }

        if (pauseTime <= 0 && pausedForTime)
        {
            UnPause();
        }

        if (paused)
        {
            return;             //End method if game is paused.
        }
        // If previous move finished, reset isPlayingMove and enable the animator.
        if (!fightMoveController.IsDoingMove())
        {
            isPlayingMove = false;
            SetAnimatorEnabled(true);
        }

        //Check fight move input.
        if (Input.anyKeyDown)
        {
            string pressedButton = "";
            foreach (string button in InputSettings.allUsedButtons)
            {
                try {
                    if (Input.GetKeyDown(button))
                    {
                        pressedButton = button;
                    }
                }
                catch
                {
                }
                try {
                    if (Input.GetButtonDown(button))
                    {
                        pressedButton = button;
                    }
                }
                catch
                {
                }
            }
            if (InputSettings.HasButton(characterIndex, pressedButton) && !isPlayingMove && !isCrouching && !animator.GetCurrentAnimatorStateInfo(0).IsName("crouch"))
            {
                isPlayingMove = true;
                SetAnimatorEnabled(false);
                string moveName = InputSettings.GetMoveName(pressedButton);
                fightMoveController.DoMove(moveName);
            }
        }

        float horizontal         = Input.GetAxisRaw(horizontalAxis);
        float horizontalJoystick = Input.GetAxisRaw(horizontalAxisJoystick);
        float vertical           = Input.GetAxisRaw(verticalAxis);
        float verticalJoystick   = Input.GetAxisRaw(verticalAxisJoystick);

        //isPlaying is whether a fight move is being played. If it is, don't move the character.
        if (!isPlayingMove)
        {
            // Move sideways
            if ((horizontal < 0 || horizontalJoystick < 0) && !this.isCrouching && !this.animator.GetBool("CrouchWalking"))
            {
                //Time left on doubleclick cooldown means this button press is within time frame of a doubleclick.
                //PressedLeftAndReleased means button has been pressed once, this press is the doubleclick.
                if (leftDoubleClickCooldown > 0 && pressedLeftAndReleased && this.collisionDown)
                {
                    // If dashcooldown is below zero, do a dash, otherwise, do nothing and reset doubleclick cooldown.
                    if (leftDashCoolDown <= 0)
                    {
                        this.movementController.DashLeft();
                        pressedLeftAndReleased  = false;
                        pressedLeft             = false;
                        leftDoubleClickCooldown = 0.0f;
                        leftDashCoolDown        = 0.7f;
                    }
                    else
                    {
                        pressedLeft             = true;
                        leftDoubleClickCooldown = 0.0f;
                        this.movementController.MoveLeft();
                    }
                }
                else
                {
                    pressedLeft             = true;
                    leftDoubleClickCooldown = 0.5f;
                    this.movementController.MoveLeft();
                }
            }
            else if ((horizontal > 0 || horizontalJoystick > 0) && !this.isCrouching && !this.animator.GetBool("CrouchWalking"))
            {
                //Time left on doubleclick cooldown means this button press is within time frame of a doubleclick.
                //PressedRightAndReleased means button has been pressed once, this press is the doubleclick.
                if (rightDoubleClickCooldown > 0 && pressedRightAndReleased && this.collisionDown)
                {
                    // If dashcooldown is below zero, do a dash, otherwise, do nothing and reset doubleclick cooldown.
                    if (rightDashCoolDown <= 0)
                    {
                        this.movementController.DashRight();
                        pressedRightAndReleased  = false;
                        pressedRight             = false;
                        rightDoubleClickCooldown = 0.0f;
                        rightDashCoolDown        = 0.7f;
                    }
                    else
                    {
                        pressedRight             = true;
                        rightDoubleClickCooldown = 0.0f;
                        this.movementController.MoveRight();
                    }
                }
                else
                {
                    pressedRight             = true;
                    rightDoubleClickCooldown = 0.5f;
                    this.movementController.MoveRight();
                }
            }
            else if ((horizontal < 0 || horizontalJoystick < 0) && this.isCrouching)
            {
                this.movementController.CrouchLeft();
            }
            else if ((horizontal > 0 || horizontalJoystick > 0) && this.isCrouching)
            {
                this.movementController.CrouchRight();
            }
            else if (horizontal == 0 || horizontalJoystick == 0)
            {
                this.movementController.Stop();
            }

            if ((vertical > 0 || verticalJoystick < 0) && !movementController.isKnockedBack())
            {
                bool jumpSucessful = jumpController.Jump();

                //Only show jump animation if jump is successful.
                if (jumpSucessful)
                {
                    SetAnimatorBool("Crouching", false);
                    SetAnimatorBool("Jumping", true);
                    this.collisionDown = false;
                }
            }
            else if ((Mathf.Abs(horizontal) > 0 || Mathf.Abs(horizontalJoystick) > 0) && this.collisionDown && !this.isCrouching)
            {
                SetAnimatorBool("Running", true);
                this.isRunning = true;
            }
            else if ((Mathf.Abs(horizontal) > 0 || Mathf.Abs(horizontalJoystick) > 0) && this.collisionDown && this.isCrouching)
            {
                SetAnimatorBool("CrouchWalking", true);
                this.isRunning = true;
            }
            else if (vertical < 0 || verticalJoystick > 0)
            {
                SetAnimatorBool("Crouching", true);
                this.isCrouching = true;
            }

            if (this.collisionDown)             // If character touches the ground, it is not jumping.
            {
                SetAnimatorBool("Jumping", false);
                this.jumpController.jumping = false;
            }

            if (vertical == 0 && verticalJoystick == 0 && collisionDown)
            {
                SetAnimatorBool("CrouchWalking", false);
                SetAnimatorBool("Crouching", false);
                this.isCrouching = false;
            }

            if (Mathf.Abs(horizontal) == 0 && Mathf.Abs(horizontalJoystick) == 0)
            {
                SetAnimatorBool("Running", false);
                SetAnimatorBool("CrouchWalking", false);

                if (pressedLeft)
                {
                    pressedLeftAndReleased = true;
                    pressedLeft            = false;
                }

                if (pressedRight)
                {
                    pressedRightAndReleased = true;
                    pressedRight            = false;
                }
            }

            if (leftDoubleClickCooldown > 0)
            {
                leftDoubleClickCooldown -= 1 * Time.deltaTime;
            }
            else
            {
                pressedLeftAndReleased = false;
                pressedLeft            = false;
            }

            if (rightDoubleClickCooldown > 0)
            {
                rightDoubleClickCooldown -= 1 * Time.deltaTime;
            }
            else
            {
                pressedRightAndReleased = false;
                pressedRight            = false;
            }

            if (leftDashCoolDown > 0)
            {
                leftDashCoolDown -= 1 * Time.deltaTime;
            }
            if (rightDashCoolDown > 0)
            {
                rightDashCoolDown -= 1 * Time.deltaTime;
            }
        }
    }