string AmmoInfo(RangedWeapon rw, int firingModeIndex)
    {
        AmmunitionStats a = rw.GetAmmunitionStats(firingModeIndex);
        MagazineStats   m = rw.GetMagazineStats(firingModeIndex);

        string s = "";

        if (a == null)
        {
            if (m == null)
            {
                s = "INFINITE";
            }
            else
            {
                s = m.magazine.current + "/INF";
            }
        }
        else
        {
            if (m == null)
            {
                s = ph.a.GetStock(a.ammoType).ToString();
            }
            else
            {
                s = m.magazine.current + "/" + (ph.a.GetStock(a.ammoType) - m.magazine.current);
            }
        }

        return(s);
    }
    // Update is called once per frame
    void Update()
    {
        /*
         * if (prs.healthCurrent != healthPrev)
         * {
         *  //HealthMeterUpdate();
         * }
         * healthPrev = prs.healthCurrent;
         */


        #region Health HUD
        healthCounter.text = ph.ph.health.current.ToString();
        FillMeter(healthBar, ph.ph.health.current, ph.ph.health.max);
        if (ph.ph.health.IsCritical())
        {
            healthCounter.color = resourceCriticalColour;
            // Do other stuff for critical health e.g. greyscale screen, warnings
        }
        else
        {
            healthCounter.color = resourceNormalColour;
        }
        #endregion

        #region Basic reticle and interacting
        RaycastHit lookingAt;
        if (Physics.Raycast(ph.pc.head.transform.position, transform.forward, out lookingAt, lookRange, lookDetection))
        {
            Character    c = null;
            DamageHitbox d = lookingAt.collider.GetComponent <DamageHitbox>();
            if (d != null)
            {
                c = Character.FromHitbox(d);
                if (c != null)
                {
                    switch (ph.faction.Affiliation(c.faction))
                    {
                    case FactionState.Allied:
                        ColourReticle(allyColour);
                        break;

                    case FactionState.Hostile:
                        ColourReticle(enemyColour);
                        break;

                    default:
                        ColourReticle(reticleDefaultColour);
                        break;
                    }
                }
                else
                {
                    ColourReticle(reticleDefaultColour);
                }
            }
            else
            {
                ColourReticle(reticleDefaultColour);
            }

            // Do interacting stuff
        }
        else
        {
            ColourReticle(reticleDefaultColour);
        }
        #endregion

        #region Weapon HUD
        RangedWeapon rw           = ph.wh.CurrentWeapon();
        bool         activeWeapon = !ph.wh.IsSwitchingWeapon;
        reticleUp.gameObject.SetActive(activeWeapon);
        reticleDown.gameObject.SetActive(activeWeapon);
        reticleLeft.gameObject.SetActive(activeWeapon);
        reticleRight.gameObject.SetActive(activeWeapon);
        ammoDisplay.gameObject.SetActive(activeWeapon);
        opticsOverlay.gameObject.SetActive(activeWeapon);
        opticsTransition.gameObject.SetActive(activeWeapon);

        if (activeWeapon == true)
        {
            if (rw.optics == null)
            {
                ADSTransition(0, null);
            }

            float accuracy = ph.wh.accuracyModifier.NewFloat(ph.wh.standingAccuracy);
            float rp       = (accuracy + rw.accuracy.projectileSpread) * Screen.height / ph.pc.fieldOfView;
            reticleUp.rectTransform.anchoredPosition    = Vector3.up * rp;
            reticleDown.rectTransform.anchoredPosition  = Vector3.down * rp;
            reticleLeft.rectTransform.anchoredPosition  = Vector3.left * rp;
            reticleRight.rectTransform.anchoredPosition = Vector3.right * rp;

            firingMode.text = rw.firingModes[rw.firingModeIndex].name;

            ammoCounter.text = AmmoInfo(rw, rw.firingModeIndex);

            AmmunitionStats a = rw.GetAmmunitionStats(rw.firingModeIndex);
            MagazineStats   m = rw.GetMagazineStats(rw.firingModeIndex);

            if (a == null)
            {
                if (m == null)
                {
                    FillMeter(ammoBar, 1, 1);
                }
                else
                {
                    FillMeter(ammoBar, m.magazine.current, m.magazine.max);
                }
            }
            else
            {
                if (m == null)
                {
                    FillMeter(ammoBar, ph.a.GetStock(a.ammoType), ph.a.GetMax(a.ammoType));
                }
                else
                {
                    FillMeter(ammoBar, m.magazine.current, m.magazine.max);
                }
            }
        }
        #endregion
    }
Ejemplo n.º 3
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);
        }
    }