예제 #1
0
    void Update()
    {
        if (Health <= 0)
        {
            if (_timerInv == 2.0f)
            {
                _animator.SetTrigger("RIP");
                Death.Play();
                _isDead = true;
            }
            _timerInv            -= Time.deltaTime;
            _spriteRenderer.color = new Color(1.0f, 1.0f, 1.0f, 1.0f);

            if (_timerInv < 0)
            {
                SceneManager.LoadScene("Menu", LoadSceneMode.Single);
            }
        }
        if (Health > 0)
        {
            Health -= AntiHealth * Time.deltaTime;
            UIController.Instance.SetHealth((int)Health);
        }

        if (_inv && Health > 0)
        {
            UIController.Instance.SetHealth((int)Health);

            UIController.Instance.SetHealth((int)Health);
            _timerInv -= Time.deltaTime;
            if (_timerInv < 0)
            {
                _spriteRenderer.color = new Color(1.0f, 1.0f, 1.0f, 1.0f);
                _inv      = false;
                _timerInv = 2.0f;
            }
        }

        if (!_isDead)
        {
            float velX     = Input.GetAxisRaw("Horizontal");
            float velY     = Input.GetAxisRaw("Vertical");
            bool  isMoving = false;

            if (_knockback <= 0)
            {
                if (Mathf.Abs(velX) > float.Epsilon || Mathf.Abs(velY) > float.Epsilon)
                {
                    _spriteRenderer.flipX = velX < 0;
                    isMoving    = true;
                    Walk.volume = Random.Range(0.8f, 1.0f);
                    Walk.pitch  = Random.Range(0.8f, 1.2f);
                    if (!Walk.isPlaying)
                    {
                        Walk.Play();
                    }
                }
                _moveInput    = new Vector2(velX, velY);
                _moveVelocity = _moveInput.normalized * Speed;
            }
            _knockback -= Time.deltaTime;

            if (Input.GetButton("Fire2") && Time.time > _nextFire)
            {
                _nextFire = Time.time + FireRate;
                Fireball?.Play();
                Shoot();
                _animator.SetTrigger("Projectile");
            }

            if (Input.GetButton("Fire1") && !_isMelee)
            {
                Sword?.Play();
                _isMelee    = true;
                _meleeTimer = 0.5f;
                Melee.gameObject.SetActive(true);
            }
            if (_isMelee)
            {
                if (_meleeTimer > 0)
                {
                    if (velX > 0 || velX == 0 && _attackRight)
                    {
                        Melee.transform.localScale = new Vector3(1, 1, 1);
                        Melee.transform.Rotate(new Vector3(0, 0, -240) * MeleeSpeed * Time.deltaTime);
                        _attackRight = true;
                    }
                    else
                    {
                        Melee.transform.localScale = new Vector3(1, -1, 1);
                        Melee.transform.Rotate(new Vector3(0, 0, 240) * MeleeSpeed * Time.deltaTime);
                        _attackRight = false;
                    }
                    _meleeTimer -= Time.deltaTime;
                }
                else
                {
                    _isMelee = false;
                    if (velX > 0)
                    {
                        Melee.transform.eulerAngles = new Vector3(0, 0, 100);
                    }
                    else
                    {
                        Melee.transform.eulerAngles = new Vector3(0, 0, 70);
                    }
                    Melee.gameObject.SetActive(false);
                }
            }
            _animator.SetBool("Walk", isMoving);

            if (Input.GetKeyDown("q") && PotionData.Boost != 0)
            {
                Health += PotionData.Boost;
                Potion.Play();
                PotionData = EmptyPotionSlot;
                UpdateSprites();
            }
            if (Input.GetKeyDown("e") && ItemData != null)
            {
                Throw();
                ItemData = null;
                Click.Play();
                CameraShaker.Instance.ShakeOnce(0.2f, 0.2f, 0.3f, 0.3f);

                ItemData = EmptyItemSlot;
                UpdateSprites();
            }

            _defenceTimer -= Time.deltaTime;
            if (_defenceTimer <= 0)
            {
                Defence.gameObject.SetActive(false);
            }
        }
    }