コード例 #1
0
    void Update()
    {
//		Debug.Log ("current weapon " + currentWeapon);
//		Debug.Log ("current grenade " + currentGrenade);

        if (CurrentWeaponBehaviorComponent.ammo == 0 && CurrentWeaponBehaviorComponent.bulletsLeft == 0 && WatchedAdAmmo == false && CurrentWeaponBehaviorComponent.name != "Frag Grenade_cube")
        {
            if (FPSPlayer.isConnected)
            {
                weaponToAddAmmo = currentWeapon;
                pauseManager.ActivateAmmoCanvas();
            }
        }
        else
        {
            pauseManager.DeactivateAmmoCanvas();
        }

        /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
        //Switch Weapons
        /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

        //end offhand grenade throw and select original weapon
        if (grenadeThrownState)
        {
            StartCoroutine(SelectWeapon(prevWepToGrenIndex, false, true));
            pullGrenadeState   = false;
            grenadeThrownState = false;
            offhandThrowActive = false;
        }

        if (Time.timeSinceLevelLoad > 2.0f &&    //don't allow weapon switching when level is still loading/fading out
            Time.timeScale > 0.0f &&            //don't allow weapon switching when paused
            !(!FPSWalkerComponent.grounded && FPSWalkerComponent.sprintActive) &&            //don't allow switching if player is sprinting and airborn
            !switching &&            //only allow one weapon switch at once
            (!CurrentWeaponBehaviorComponent.shooting || FPSPlayerComponent.hitPoints < 1.0f)               //don't switch weapons if shooting
            //don't allow switching if player is holding an object
            && !FPSWalkerComponent.holdingObject)
        {
            //select next grenade
            if (InputComponent.selectGrenPress && !displayingGrenade)
            {
                if (currentGrenade + 1 <= grenadeOrder.Length - 1)
                {
                    if (grenadeOrder[currentGrenade + 1].GetComponent <WeaponBehavior>().haveWeapon &&
                        grenadeOrder[currentGrenade + 1].GetComponent <WeaponBehavior>().ammo > 0)
                    {
                        currentGrenade++;
                    }
                }
                else                  //start counting grenades from zero if last grenade in list
                {
                    if (grenadeOrder[0].GetComponent <WeaponBehavior>().haveWeapon &&
                        grenadeOrder[0].GetComponent <WeaponBehavior>().ammo > 0)
                    {
                        currentGrenade = 0;
                    }
                }
                GrenadeWeaponBehaviorComponent = grenadeOrder[currentGrenade].GetComponent <WeaponBehavior>();
                if (GrenadeWeaponBehaviorComponent.haveWeapon && GrenadeWeaponBehaviorComponent.ammo > 0)
                {
                    grenadeWeapon      = GrenadeWeaponBehaviorComponent.weaponNumber;
                    prevWepToGrenIndex = currentWeapon;
                    displayingGrenade  = true;
                    grenDisplayTime    = Time.time;
                    StartCoroutine(SelectWeapon(grenadeWeapon, true, false));
                    StartCoroutine(DisplayGrenadeSwitch());
                }
            }

            //begin offhand grenade throw
            if (currentWeapon != grenadeWeapon && GrenadeWeaponBehaviorComponent.ammo > 0 && !displayingGrenade)
            {
                if (InputComponent.grenadeHold && !pullGrenadeState)
                {
                    offhandThrowActive = true;
                    prevWepToGrenIndex = currentWeapon;
                    StartCoroutine(SelectWeapon(grenadeWeapon, true, false));
                    grenadeThrownState = false;
                    pullGrenadeState   = true;
                }
            }

            //don't allow weapon switching while sprint anim is active/transitioning
            if (!sprintSwitching)
            {
                //drop weapons
                if ((InputComponent.dropPress || InputComponent.xboxDpadDownPress) &&
                    currentWeapon != 0 &&
                    !pullGrenadeState &&
                    !FPSWalkerComponent.sprintActive &&
                    CurrentWeaponBehaviorComponent.droppable &&
                    !CurrentWeaponBehaviorComponent.dropWillDupe)                        //if drop button is pressed and weapon isn't holstered (null weap 0 selected)
                {
                    DropWeapon(currentWeapon);
                }

                //drop current weapon if player dies
                if (FPSPlayerComponent.hitPoints < 1.0f && !deadDropped)
                {
                    CurrentWeaponBehaviorComponent.droppable = true;
                    if (CurrentWeaponBehaviorComponent.muzzleFlash)
                    {
                        CurrentWeaponBehaviorComponent.muzzleFlash.GetComponent <Renderer>().enabled = false;
                    }
                    deadDropped = true;
                    DropWeapon(currentWeapon);
                }

                if (!CameraControlComponent.thirdPersonActive && Time.timeScale > 0f)
                {
                    //Cycle weapons using the mousewheel (cycle through FPS Weapon children) and skip weapons that are not in player inventory.
                    //weaponOrder.Length - 1 is the last weapon because the built in array starts counting at zero and weaponOrder.Length starts counting at one (index 0 of weaponOrder[] is null/unarmed weapon).
                    if (InputComponent.mouseWheel < 0 || InputComponent.selectPrevPress || InputComponent.xboxDpadLeftPress) //mouse wheel down or previous weapon button pressed
                    {
                        if (currentWeapon != 0)                                                                              //not starting at zero
                        {
                            for (int i = currentWeapon; i > -1; i--)
                            {
                                WeaponBehavior ThisWeaponBehavior = weaponOrder[i].GetComponent <WeaponBehavior>();
                                if (ThisWeaponBehavior.haveWeapon && ThisWeaponBehavior.cycleSelect && i != currentWeapon) //check that player has weapon and it is not currently selected weapon
                                {
                                    StartCoroutine(SelectWeapon(i));                                                       //run the SelectWeapon function with the next weapon index that was found
                                    break;
                                }
                                else if (i == 0)                                 //reached zero, count backwards from end of list to find next weapon
                                {
                                    for (int n = weaponOrder.Length - 1; n > -1; n--)
                                    {
                                        WeaponBehavior ThisWeaponBehavior2 = weaponOrder[n].GetComponent <WeaponBehavior>();
                                        if (ThisWeaponBehavior2.haveWeapon && ThisWeaponBehavior2.cycleSelect && n != currentWeapon)
                                        {
                                            StartCoroutine(SelectWeapon(n));
                                            break;
                                        }
                                    }
                                }
                            }
                        }
                        else                          //starting at 0
                        {
                            for (int i = weaponOrder.Length - 1; i > -1; i--)
                            {
                                WeaponBehavior ThisWeaponBehavior = weaponOrder[i].GetComponent <WeaponBehavior>();
                                if (ThisWeaponBehavior.haveWeapon && ThisWeaponBehavior.cycleSelect && i != currentWeapon)
                                {
                                    StartCoroutine(SelectWeapon(i));
                                    break;
                                }
                            }
                        }
                    }
                    else if (InputComponent.mouseWheel > 0 ||                  //mouse wheel up
                             InputComponent.selectNextPress ||                   //select next weapon button pressed
                             InputComponent.xboxDpadRightPress ||
                             (dropWeapon && totalWeapons != 0))                       //drop weapon button pressed and player has weapons in their inventory
                    {
                        if (currentWeapon < weaponOrder.Length - 1)                   //not starting at last weapon
                        {
                            for (int i = currentWeapon; i < weaponOrder.Length; i++)
                            {
                                WeaponBehavior ThisWeaponBehavior = weaponOrder[i].GetComponent <WeaponBehavior>();
                                //cycle weapon selection manually
                                if ((ThisWeaponBehavior.haveWeapon && ThisWeaponBehavior.cycleSelect &&
                                     i != currentWeapon && !dropWeapon)
                                    //do not select backupWeapon if dropping a weapon and automatically selecting the next weapon
                                    //but allow backupWeapon to be selected when cycling weapon selection manually
                                    || (ThisWeaponBehavior.haveWeapon && ThisWeaponBehavior.cycleSelect &&
                                        i != currentWeapon && i != backupWeapon && dropWeapon))
                                {
                                    StartCoroutine(SelectWeapon(i));
                                    break;
                                }
                                else if (i == weaponOrder.Length - 1)                                 //reached end of list, count forwards from zero to find next weapon
                                {
                                    for (int n = 0; n < weaponOrder.Length - 1; n++)
                                    {
                                        WeaponBehavior ThisWeaponBehavior2 = weaponOrder[n].GetComponent <WeaponBehavior>();
                                        //cycle weapon selection manually
                                        if ((ThisWeaponBehavior2.haveWeapon && ThisWeaponBehavior2.cycleSelect &&
                                             n != currentWeapon && !dropWeapon)
                                            //do not select backupWeapon if dropping a weapon and automatically selecting the next weapon
                                            //but allow backupWeapon to be selected when cycling weapon selection manually
                                            || (ThisWeaponBehavior2.haveWeapon && ThisWeaponBehavior2.cycleSelect &&
                                                n != currentWeapon && n != backupWeapon && dropWeapon))
                                        {
                                            StartCoroutine(SelectWeapon(n));
                                            break;
                                        }
                                    }
                                }
                            }
                        }
                        else                          //starting at last weapon
                        {
                            for (int i = 0; i < weaponOrder.Length - 1; i++)
                            {
                                WeaponBehavior ThisWeaponBehavior = weaponOrder[i].GetComponent <WeaponBehavior>();
                                //cycle weapon selection manually
                                if ((ThisWeaponBehavior.haveWeapon && ThisWeaponBehavior.cycleSelect &&
                                     i != currentWeapon && !dropWeapon)
                                    //do not select backupWeapon if dropping a weapon and automatically selecting the next weapon
                                    //but allow backupWeapon to be selected when cycling weapon selection manually
                                    || (ThisWeaponBehavior.haveWeapon && ThisWeaponBehavior.cycleSelect &&
                                        i != currentWeapon && i != backupWeapon && dropWeapon))
                                {
                                    StartCoroutine(SelectWeapon(i));
                                    break;
                                }
                            }
                        }
                    }
                }

                //select weapons with number keys
                if (InputComponent.holsterPress)
                {
                    if (currentWeapon != 0)
                    {
                        StartCoroutine(SelectWeapon(0));
                    }
                }
                else if (InputComponent.selectWeap1Press && weaponOrder.Length - 1 > 0)
                {
                    if (currentWeapon != 1)
                    {
                        StartCoroutine(SelectWeapon(1));
                    }
                }
                else if (InputComponent.selectWeap2Press && weaponOrder.Length - 1 > 1)
                {
                    if (currentWeapon != 2)
                    {
                        StartCoroutine(SelectWeapon(2));
                    }
                }
                else if (InputComponent.selectWeap3Press && weaponOrder.Length - 1 > 2)
                {
                    if (currentWeapon != 3)
                    {
                        StartCoroutine(SelectWeapon(3));
                    }
                }
                else if (InputComponent.selectWeap4Press && weaponOrder.Length - 1 > 3)
                {
                    if (currentWeapon != 4)
                    {
                        StartCoroutine(SelectWeapon(4));
                    }
                }
                else if (InputComponent.selectWeap5Press && weaponOrder.Length - 1 > 4)
                {
                    if (currentWeapon != 5)
                    {
                        StartCoroutine(SelectWeapon(5));
                    }
                }
                else if (InputComponent.selectWeap6Press && weaponOrder.Length - 1 > 5)
                {
                    if (currentWeapon != 6)
                    {
                        StartCoroutine(SelectWeapon(6));
                    }
                }
                else if (InputComponent.selectWeap7Press && weaponOrder.Length - 1 > 6)
                {
                    if (currentWeapon != 7)
                    {
                        StartCoroutine(SelectWeapon(7));
                    }
                }
                else if (InputComponent.selectWeap8Press && weaponOrder.Length - 1 > 7)
                {
                    if (currentWeapon != 8)
                    {
                        StartCoroutine(SelectWeapon(8));
                    }
                }
                else if (InputComponent.selectWeap9Press && weaponOrder.Length - 1 > 8)
                {
                    if (currentWeapon != 9)
                    {
                        StartCoroutine(SelectWeapon(9));
                    }
                }
            }
        }

        //check timer for switch to prevent shooting
        //this var checked in "WeaponBehavior" script in the Fire() function
        if (switchTime + 0.87f > Time.time)
        {
            switching = true;
        }
        else
        {
            switching = false;
        }

        if (grenDisplayTime + 2f < Time.time)
        {
            displayingGrenade = false;
        }

        //define time that sprinting anim is active/transitioning to disable weapon switching
        if (sprintSwitchTime + 0.44f > Time.time)
        {
            sprintSwitching = true;
        }
        else
        {
            sprintSwitching = false;
        }

        //pause and resume reloading sound based on timescale/game pausing state
        if (Time.timeScale > 0)
        {
            if (audioPaused)
            {
                aSources[1].Play();
                audioPaused = false;
            }
        }
        else
        {
            if (!audioPaused && aSources[1].isPlaying)
            {
                aSources[1].Pause();
                audioPaused = true;
            }
        }
    }