예제 #1
0
    // Update is called once per frame
    void Update()
    {
        // Features:
        // - Makes the aimer visible when right stick is in use.
        // - Rotates the aimer to point in same direction as stick.
        #region Aiming
        bulletAimer.transform.position = this.transform.position;
        if (!chargingSword)
        {
            Vector3 direction = new Vector3(Input.GetAxis("HorizontalRight"), Input.GetAxis("VerticalRight"), 0);

            aiming = !(direction.x == 0 && direction.y == 0);

            if (aiming)
            {
                aimerAngle = Mathf.Atan2(direction.y, direction.x) * 180 / Mathf.PI;
                bulletAimer.transform.eulerAngles = new Vector3(0, 0, aimerAngle);
            }
            else
            {
                bulletAimer.transform.eulerAngles = new Vector3(0, 90, 0);
            }
        }
        else
        {
            aiming = false;
            bulletAimer.transform.eulerAngles = new Vector3(0, 90, 0);
        }
        #endregion

        // Features:
        // - Moves based on input
        // - Jumps when jump button is pressed
        // - Dashes when dash button is pressed
        #region JumpAndMovementInput

        // Jumps when jump button is pressed.
        if (Input.GetButtonDown("Jump"))
        {
            Jump();
        }
        // Dashes when dash is pressed, tells dash method if aiming or not.
        if (Input.GetButtonDown("Dash"))
        {
            Dash();
        }

        // Moves in axis direction and stops when nothing is pressed.
        else if (Input.GetAxisRaw("Horizontal") > 0 && !dashing)
        {
            MovePlayer(1);
        }
        else if (Input.GetAxisRaw("Horizontal") < 0 && !dashing)
        {
            MovePlayer(-1);
        }
        else if (!dashing)
        {
            MovePlayer(0);
        }

        #endregion

        // Features:
        // - Shooting
        // - Sword Slash/Charge
        #region Attacks
        // For shooting and aiming the bullet attack.
        if (Input.GetButtonDown("Fire") && aiming)
        {
            FireBullet();
        }
        else if (aiming)
        {
            //Do nothing.
        }
        else if (Input.GetButtonDown("Fire"))
        {
            BeginSwordSlash();
        }
        else if (Input.GetButtonUp("Fire") && chargingSword)
        {
            EndSwordSlash();
        }
        if (!Input.GetButton("Fire"))
        {
            chargingSword = false;
        }
        #endregion

        // Prob have to redo this when I add animations.
        #region Animator Code

        // Sets speed (for movement animation)
        anim.SetFloat("Speed", Mathf.Abs(GetComponent <Rigidbody2D>().velocity.x));

        // Tells animator if guy is on ground.
        anim.SetBool("Grounded", grounded);

        // Flips player if he is moving backwards (helpful for shooting).
        if (GetComponent <Rigidbody2D>().velocity.x > 0)
        {
            transform.localScale = new Vector3(1f, 1f, 1f);
        }
        else if (GetComponent <Rigidbody2D>().velocity.x < 0)
        {
            transform.localScale = new Vector3(-1f, 1f, 1f);
        }

        // Chooses a SwordCharge particle generator to show.
        if (chargingSword)
        {
            int stage = 0;
            if (Time.time > swordChargeStartTime + stageTwoSwordChargeTime)
            {
                stage = 2;
            }
            else if (Time.time > swordChargeStartTime + stageOneSwordChargeTime)
            {
                stage = 1;
            }

            swordChargeStageZeroParticles.SetActive(stage == 0);
            swordChargeStageOneParticles.SetActive(stage == 1);
            swordChargeStageTwoParticles.SetActive(stage == 2);
        }
        else
        {
            swordChargeStageZeroParticles.SetActive(false);
            swordChargeStageOneParticles.SetActive(false);
            swordChargeStageTwoParticles.SetActive(false);
        }
        #endregion

        #region UI

        // Handle Display Text.
        if (ui.AirDashNum != airDashesLeft)
        {
            ui.UpdateDashText(airDashesLeft);
        }
        if (ui.AirJumpNum != airJumpsLeft)
        {
            ui.UpdateJumpText(airJumpsLeft);
        }

        shootingCooldownLeft = Mathf.CeilToInt(shootingCooldownTime - Time.time);
        if (shootingCooldownLeft < 0)
        {
            shootingCooldownLeft = 0;
        }
        if (ui.ShotCooldownNum != shootingCooldownLeft)
        {
            ui.UpdateShotCooldownText(shootingCooldownLeft);
        }

        #endregion
    }