示例#1
0
    void Update()
    {
        #region Makes easy references to the appropriate sets of stats so I don't have to type "xModes[firingModes[firingModeIndex].xMode]" every single bloody time I need to reference a set of stats
        fireControls = fireControlModes[firingModes[firingModeIndex].fireControlMode];
        accuracy     = accuracyModes[firingModes[firingModeIndex].accuracyMode];
        projectile   = projectileModes[firingModes[firingModeIndex].projectileMode];
        optics       = GetOpticsStats(firingModeIndex);
        recoil       = GetRecoilStats(firingModeIndex);
        ammunition   = GetAmmunitionStats(firingModeIndex);
        magazine     = GetMagazineStats(firingModeIndex);
        #endregion

        // Checks following criteria to determine if the player should be able to control their weapon:
        // If the player is not currently switching weapon or firing mode
        // If the player's weapon selector is not active
        if (isSwitchingWeapon == false && isSwitchingFireMode == false && playerHolding.weaponSelector.MenuIsActive() == false)
        {
            if (Input.GetAxis("Mouse ScrollWheel") != 0 && firingModes.Length > 1) // Switch firing modes with the scroll wheel
            {
                int i = 0;
                if (Input.GetAxis("Mouse ScrollWheel") < 0)
                {
                    i = 1;
                }
                else if (Input.GetAxis("Mouse ScrollWheel") > 0)
                {
                    i = -1;
                }

                i += firingModeIndex;

                if (i > firingModes.Length - 1)
                {
                    i = 0;
                }
                else if (i < 0)
                {
                    i = firingModes.Length - 1;
                }

                SwitchWeaponMode(i);
            }

            fireControls.fireTimer += Time.deltaTime;

            // If player is active
            // If player is pressing fire button
            // If fireTimer has finished
            // If burst count has not exceeded the limit OR burst count is set to zero
            // If ammo is available OR supply is null
            // If magazine is not empty OR null
            if (playerHolding.ph.CurrentState() == PlayerState.Active && /*playerHolding.ph.isActive == true && */ Input.GetButton("Fire") && fireControls.fireTimer >= 60 / fireControls.roundsPerMinute && (fireControls.burstCounter < fireControls.maxBurst || fireControls.maxBurst <= 0) && (ammunition == null || (playerHolding.ph.a.GetStock(ammunition.ammoType) >= ammunition.ammoPerShot)) && (magazine == null || (magazine.magazine.current >= 1 /*ammoPerShot*/ && isReloading == false)))
            {
                // Adjust fire control variables
                fireControls.fireTimer     = 0; // Reset fire timer to count up to next shot
                fireControls.burstCounter += 1;

                #region Alter ammunition, magazine and recoil variables if present
                // Consume ammo if supply is present
                if (ammunition != null)
                {
                    playerHolding.ph.a.Spend(ammunition.ammoType, ammunition.ammoPerShot);
                }

                // Deplete magazine if present
                if (magazine != null)
                {
                    if (ammunition != null) // If ammunition supply is present, consume appropriate amount of
                    {
                        magazine.magazine.current -= ammunition.ammoPerShot;
                    }
                    else
                    {
                        magazine.magazine.current -= 1;
                    }
                }

                // Apply recoil if recoil stat exists
                if (recoil != null)
                {
                    recoilToApply += recoil.recoil;
                }
                #endregion

                #region Trigger cosmetic effects

                /*
                 * firingModes[firingModeIndex].muzzleFlash.Play();
                 * weaponSoundSource.PlayOneShot(firingModes[firingModeIndex].firingNoise);
                 * firingModes[firingModeIndex].shellEjection.Play();
                 */

                MuzzleFlashEffect m = firingModes[firingModeIndex].muzzleFlash;
                if (m != null)
                {
                    m.Play();
                }

                AudioClip a = firingModes[firingModeIndex].firingNoise;
                if (a != null)
                {
                    weaponSoundSource.PlayOneShot(a);
                }

                ParticleSystem s = firingModes[firingModeIndex].shellEjection;
                if (s != null)
                {
                    s.Play();
                }
                #endregion

                // Calculate direction to shoot in
                //Quaternion ar = Quaternion.Euler(Random.Range(-playerHolding.standingAccuracy, playerHolding.standingAccuracy), Random.Range(-playerHolding.standingAccuracy, playerHolding.standingAccuracy), Random.Range(-playerHolding.standingAccuracy, playerHolding.standingAccuracy));
                //Vector3 aimDirection = ar * transform.forward;

                Vector3 aimDirection = transform.forward;
                if (optics == null || isAiming == false)
                {
                    aimDirection = Quaternion.Euler(Random.Range(-playerHolding.standingAccuracy, playerHolding.standingAccuracy), Random.Range(-playerHolding.standingAccuracy, playerHolding.standingAccuracy), Random.Range(-playerHolding.standingAccuracy, playerHolding.standingAccuracy)) * aimDirection;
                }

                for (int i = 0; i < projectile.projectileCount; i++) // Shoots an amount of projectiles based on the projectileCount variable.
                {
                    Damage.ShootProjectile(projectile.projectile, accuracy.projectileSpread, accuracy.range, playerHolding.gameObject, playerHolding.ph.faction, transform, projectile.muzzle, aimDirection);
                }
            }
            else if (!Input.GetButton("Fire"))
            {
                fireControls.burstCounter = 0;
            }

            if (optics != null)
            {
                AimHandler(optics, firingModes[firingModeIndex].heldPosition, playerHolding.toggleAim);
            }

            if (magazine != null)
            {
                if (ammunition != null)
                {
                    ReloadHandler(magazine.reloadTime, fireControls.fireTimer, fireControls.roundsPerMinute, magazine.roundsReloaded, magazine.magazine, playerHolding, ammunition.ammoType);
                }
                else
                {
                    ReloadHandler(magazine.reloadTime, fireControls.fireTimer, fireControls.roundsPerMinute, magazine.roundsReloaded, magazine.magazine);
                }
            }
        }

        if (recoil != null)
        {
            RecoilHandler(recoil.recoilApplyRate, playerHolding);
        }
    }
示例#2
0
 private void Start()
 {
     defaultMagSize = currentClipSize;
     gunSound       = GetComponent <AudioSource>();
     muzzleFlash    = GetComponentInChildren <MuzzleFlashEffect>();
 }