Exemplo n.º 1
0
    // Update is called once per frame
    void Update()
    {
        if (!_enabled)
        {
            return;
        }
        _force = Vector3.zero;

        // Look at the the mouse
        Vector3    mousePos       = MouseToXZPlane(Input.mousePosition);
        Quaternion targetRotation = Quaternion.LookRotation(mousePos - this.transform.position);

        targetRotation.eulerAngles = new Vector3(0f, targetRotation.eulerAngles.y, 0f);

        this.transform.rotation = Quaternion.RotateTowards(transform.rotation, targetRotation, rotationSpeed * Time.deltaTime);


        // Get WASD key pressed. Calculate the trust
        if (Input.GetKey(KeyCode.W) && _enableMovement)
        {
            _force += transform.forward * mainThrusterForce;
            if (mainThrusterSlider.value > 1)
            {
                if (_consumeBool)
                {
                    mainThrusterSlider.value -= boostConsumptionRate * Time.deltaTime;
                }
            }
            else
            {
                mainThrusterSound.volume = .4f;
                mainBoostSound.Stop();
                _consumeBool = false;
                baseSpeed    = _currentSpeed;
            }
        }
        if (Input.GetKey(KeyCode.A) && _enableMovement)
        {
            _force += transform.right * -sideThrusterForce * _doubleTapBoostA;
        }
        if (Input.GetKey(KeyCode.S) && _enableMovement)
        {
            _force -= transform.forward * sideThrusterForce * .3f;
        }
        if (Input.GetKey(KeyCode.D) && _enableMovement)
        {
            _force += transform.right * sideThrusterForce * _doubleTapBoostD;
        }

        // get WASD keydown. Play thruster audio clips.
        if (Input.GetKeyDown(KeyCode.W) && _enableMovement)
        {
            mainThrusterSound.volume = .4f;

            float startKeyPress = Time.time;
            if (startKeyPress - _doubleTapTimeA < .185f && startKeyPress - _timeSinceLastBoost > _timeBetweenBoosts && mainThrusterSlider.value > 1)
            {
                mainBoostParticleBitsClone = Instantiate(mainBoostParticleBits, transform.position, transform.rotation);
                Destroy(mainBoostParticleBitsClone, 5.0f);
                _consumeBool = true;
                mainBoostSound.Play();
                mainThrusterSound.volume = 0f;
                baseSpeed = boostSpeed;
            }
            else
            {
                _doubleTapBoostA = 1;
            }
            _doubleTapTimeA = Time.time;
        }
        if (Input.GetKeyDown(KeyCode.A) && _enableMovement)
        {
            sideThrusterSound.volume = .2f;
            float startKeyPress = Time.time;
            if (startKeyPress - _doubleTapTimeA < .185f && startKeyPress - _timeSinceLastBoost > _timeBetweenBoosts)
            {
                _doubleTapBoostA = 80;
                sideBoostSound.Play();
                sideThrusterSound.Stop();
                StartCoroutine(SideThrusterBoost());
            }
            else
            {
                _doubleTapBoostA = 1;
            }
            _doubleTapTimeA = Time.time;
        }
        if (Input.GetKeyDown(KeyCode.S) && _enableMovement)
        {
            backingUpBeepsSound.Play();
        }
        if (Input.GetKeyDown(KeyCode.D) && _enableMovement)
        {
            sideThrusterSound.volume = .2f;
            float startKeyPress = Time.time;
            if (startKeyPress - _doubleTapTimeD < .185f && startKeyPress - _timeSinceLastBoost > _timeBetweenBoosts)
            {
                _doubleTapBoostD = 80;
                sideBoostSound.Play();
                sideThrusterSound.Stop();
                StartCoroutine(SideThrusterBoost());
            }
            else
            {
                _doubleTapBoostD = 1;
            }
            _doubleTapTimeD = Time.time;
        }

        // Get WASD keyup. Fade thruster audio clips.
        if (Input.GetKeyUp(KeyCode.W) && _enableMovement)
        {
            for (int i = 0; i < 400; i++)
            {
                mainThrusterSound.volume -= .001f;
                mainBoostSound.Stop();
                _consumeBool = false;
                baseSpeed    = _currentSpeed;
            }
        }
        if (Input.GetKeyUp(KeyCode.A) && _enableMovement)
        {
            for (int i = 0; i < 200; i++)
            {
                sideThrusterSound.volume -= .001f;
            }
        }
        if (Input.GetKeyUp(KeyCode.S) && _enableMovement)
        {
            backingUpBeepsSound.Stop();
        }
        if (Input.GetKeyUp(KeyCode.D) && _enableMovement)
        {
            for (int i = 0; i < 200; i++)
            {
                sideThrusterSound.volume -= .001f;
            }
        }

        if (Input.GetKey(KeyCode.LeftShift) && _enableShield)
        {
            if (shieldEnergySlider.value > 0)
            {
                shieldEnergySlider.value -= shieldDecayTime * Time.deltaTime;
            }
            else
            {
                shield.SetActive(false);
                _shieldDeployed = false;
            }
        }
        if (Input.GetKeyDown(KeyCode.LeftShift) && _enableShield)
        {
            shield.SetActive(true);
            _shieldDeployed = true;
        }
        if (Input.GetKeyUp(KeyCode.LeftShift) && _enableShield)
        {
            shield.SetActive(false);
            _shieldDeployed = false;
        }

        // recharge energy
        if (laserEnergySlider.value < laserEnergySlider.maxValue && !_mainAttackWeapon.IsFiring() && !_altAttackWeapon.IsFiring())
        {
            laserEnergySlider.value = Mathf.Min(laserEnergySlider.value + energyRechargeRate * Time.deltaTime, laserEnergySlider.maxValue);
        }
        // recharge shield
        if (shieldEnergySlider.value < shieldEnergySlider.maxValue && !_shieldDeployed)
        {
            shieldEnergySlider.value += shieldRechargeRate * Time.deltaTime;
        }

        // Get left mouse button down input
        if (Input.GetMouseButtonDown(0) && _enableFire)
        {
            if (laserEnergySlider.value - _mainAttackWeapon.EnergyToFire() >= 0)
            {
                laserEnergySlider.value -= _mainAttackWeapon.EnergyToFire();
                _mainAttackWeapon.Fire();
            }
        }
        else if (Input.GetMouseButton(0) && _mainAttackWeapon.IsFiring() && _enableFire)
        {
            float energyValue = _mainAttackWeapon.EnergyPerSecond() * Time.deltaTime;

            if (laserEnergySlider.value - energyValue >= 0)
            {
                laserEnergySlider.value -= energyValue;
            }
            else
            {
                _mainAttackWeapon.StopFire();
            }
        }
        else if (Input.GetMouseButtonUp(0) && _enableFire)
        {
            _mainAttackWeapon.StopFire();
        }

        // Get right mouse button down input
        if (Input.GetMouseButtonDown(1) && _enableAltFire)
        {
            if (laserEnergySlider.value - _altAttackWeapon.EnergyToFire() >= 0)
            {
                laserEnergySlider.value -= _altAttackWeapon.EnergyToFire();
                _altAttackWeapon.Fire();
            }
        }
        else if (Input.GetMouseButton(1) && _altAttackWeapon.IsFiring() && _enableAltFire)
        {
            float energyValue = _altAttackWeapon.EnergyPerSecond() * Time.deltaTime;

            if (laserEnergySlider.value - energyValue >= 0)
            {
                laserEnergySlider.value -= energyValue;
            }
            else
            {
                _altAttackWeapon.StopFire();
            }
        }
        else if (Input.GetMouseButtonUp(1) && _enableAltFire)
        {
            _altAttackWeapon.StopFire();
        }

        if (Input.GetKeyDown(KeyCode.Space) && _enableBoost)
        {
            if (mainThrusterSlider.value > 1)
            {
                mainBoostParticleBitsClone = Instantiate(mainBoostParticleBits, transform.position, transform.rotation);
                Destroy(mainBoostParticleBitsClone, 5.0f);
            }
            mainThrusterSound.Stop();
            mainBoostSound.Play();
        }
        if (Input.GetKey(KeyCode.Space) && _enableBoost)
        {
            mainThrusterSlider.value -= boostConsumptionRate * Time.deltaTime;
            if (mainThrusterSlider.value > 1)
            {
                baseSpeed = boostSpeed;
            }
            else
            {
                mainBoostSound.Stop();
                if (!mainThrusterSound.isPlaying)
                {
                    mainThrusterSound.Play();
                }
                baseSpeed = _currentSpeed;
            }
        }
        // recharge thruster
        else
        {
            if (mainThrusterSlider.value < mainThrusterSlider.maxValue)
            {
                mainThrusterSlider.value += thrusterRechargeRate * Time.deltaTime;
            }
        }
        if (Input.GetKeyUp(KeyCode.Space) && _enableBoost)
        {
            mainThrusterSound.Play();
            mainBoostSound.Stop();
            baseSpeed = _currentSpeed;
        }
    } // end Update()