Exemplo n.º 1
0
    /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    //Select Weapons
    /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

    public IEnumerator SelectWeapon(int index)
    {
        FPSRigidBodyWalker FPSWalkerComponent = playerObj.GetComponent <FPSRigidBodyWalker>();

        //we are not dropping a weapon anymore if one has been selected
        dropWeapon = false;

        //do not proceed with selecting weapon if player doesn't have it in their inventory
        //but make an exception for the null/unarmed weapon for when the player presses the holster button
        //also dont allow weapon switch if player is climbing, swimming, or holding object and their weapon is lowered
        if ((!weaponOrder[index].GetComponent <WeaponBehavior>().haveWeapon&& index != 0) ||
            FPSWalkerComponent.hideWeapon)
        {
            yield break;
        }

        if (index != 0)        //if a weapon is selected, prevent unarmed/null weapon from being selected in selection cycle
        {
            weaponOrder[0].GetComponent <WeaponBehavior>().haveWeapon = false;
        }

        //set up external script references
        Ironsights     IronsightsComponent            = playerObj.GetComponent <Ironsights>();
        CameraKick     CameraKickComponent            = Camera.main.GetComponent <CameraKick>();
        FPSPlayer      FPSPlayerComponent             = playerObj.GetComponent <FPSPlayer>();
        WeaponBehavior CurrentWeaponBehaviorComponent = weaponOrder[currentWeapon].GetComponent <WeaponBehavior>();

        //cancel zooming when switching
        FPSPlayerComponent.zoomed = false;

        //reset non-magazine reload if interrupted by weapon switch
        if (CurrentWeaponBehaviorComponent.bulletsToReload != CurrentWeaponBehaviorComponent.bulletsPerClip &&
            CurrentWeaponBehaviorComponent.weaponMesh.GetComponent <Animation>()["Neutral"] &&
            IronsightsComponent.reloading)
        {
            //play neutral animation when putting weapon away to prevent neutral anim glitch at start of next reload
            CurrentWeaponBehaviorComponent.weaponMesh.GetComponent <Animation>()["Neutral"].speed = 1.5f;
            CurrentWeaponBehaviorComponent.weaponMesh.GetComponent <Animation>().Play("Neutral", PlayMode.StopAll);
            //reset bulletsReloaded to prevent delay of reloading the next time we reload this weapon
            CurrentWeaponBehaviorComponent.bulletsReloaded = 0;
        }

        //cancel reloading when switching
        IronsightsComponent.reloading = false;                  //set IronSights Reloading var to false
        CurrentWeaponBehaviorComponent.StopCoroutine("Reload"); //stop the Reload function if it is running

        //make timer active during switch to prevent shooting
        switchTime = Time.time - Time.deltaTime;

        if (Time.timeSinceLevelLoad > 2)
        {
            //play weapon switch sound if not the first call to this function after level load
            AudioSource.PlayClipAtPoint(changesnd, mainCamTransform.position);

            //play camera weapon switching animation
            Camera.main.GetComponent <Animation>().Rewind("CameraSwitch");
            Camera.main.GetComponent <Animation>().CrossFade("CameraSwitch", 0.35f, PlayMode.StopAll);
        }

        //if weapon uses rifle sprinting animation, set speed and play animation
        if (!CurrentWeaponBehaviorComponent.PistolSprintAnim)
        {
            //animate previous weapon down
            if (!FPSWalkerComponent.canRun)
            {
                weaponOrder[currentWeapon].GetComponent <Animation>()["RifleSprinting"].normalizedTime = 0;
                weaponOrder[currentWeapon].GetComponent <Animation>()["RifleSprinting"].speed          = 1.5f;
                weaponOrder[currentWeapon].GetComponent <Animation>().CrossFade("RifleSprinting", 0.00025f, PlayMode.StopAll);
            }
            else
            {
                //if player is sprinting, keep weapon in sprinting position during weapon switch
                weaponOrder[currentWeapon].GetComponent <Animation>()["RifleSprinting"].normalizedTime = 1;
            }
        }
        else          //weapon uses pistol sprinting animation
                      //animate previous weapon down
        {
            if (!FPSWalkerComponent.canRun)
            {
                weaponOrder[currentWeapon].GetComponent <Animation>()["PistolSprinting"].normalizedTime = 0;
                weaponOrder[currentWeapon].GetComponent <Animation>()["PistolSprinting"].speed          = 1.5f;
                weaponOrder[currentWeapon].GetComponent <Animation>().CrossFade("PistolSprinting", 0.00025f, PlayMode.StopAll);
            }
            else
            {
                //if player is sprinting, keep weapon in sprinting position during weapon switch
                weaponOrder[currentWeapon].GetComponent <Animation>()["PistolSprinting"].normalizedTime = 1;
            }
        }

        if (Time.timeSinceLevelLoad > 2)
        {
            if (CurrentWeaponBehaviorComponent.meleeSwingDelay == 0)
            {
                //move weapon down while switching
                IronsightsComponent.switchMove = -0.4f;
            }
            else
            {
                //move melee weapons down further while switching because they take more vertical screen space than guns
                IronsightsComponent.switchMove = -1.2f;
            }

            //wait for weapon down animation to play before switching weapons and animating weapon up
            yield return(new WaitForSeconds(0.2f));
        }

        //immediately switch weapons (activate called weaponOrder index and deactivate all others)
        for (int i = 0; i < weaponOrder.Length; i++)
        {
            if (i == index)
            {
                                #if UNITY_3_5
                // Activate the selected weapon
                weaponOrder[i].SetActiveRecursively(true);
                                #else
                // Activate the selected weapon
                weaponOrder[i].SetActive(true);
                                #endif

                //get current weapon value from index
                currentWeapon = index;

                //synchronize current and previous weapon's y pos for correct offscreen switching, use localPosition not position for correct transforms
                weaponOrder[i].transform.localPosition = weaponOrder[i].transform.localPosition + new Vector3(0, weaponOrder[i].transform.localPosition.y - 0.3f, 0);

                if (Time.timeSinceLevelLoad > 2)
                {
                    //move weapon up when switch finishes
                    IronsightsComponent.switchMove = 0;
                }

                //if weapon uses rifle sprinting animation set speed and animate
                if (!weaponOrder[i].GetComponent <WeaponBehavior>().PistolSprintAnim)
                {
                    //animate selected weapon up by setting time of animation to it's end and playing in reverse
                    if (!FPSWalkerComponent.canRun)
                    {
                        weaponOrder[i].GetComponent <Animation>()["RifleSprinting"].normalizedTime = 1.0f;
                        weaponOrder[i].GetComponent <Animation>()["RifleSprinting"].speed          = -1.5f;
                        weaponOrder[i].GetComponent <Animation>().CrossFade("RifleSprinting", 0.00025f, PlayMode.StopAll);
                    }
                    else
                    {
                        //if player is sprinting, keep weapon in sprinting position during weapon switch
                        weaponOrder[i].GetComponent <Animation>()["RifleSprinting"].normalizedTime = 1.0f;
                        weaponOrder[i].GetComponent <Animation>()["RifleSprinting"].speed          = 1.5f;
                        weaponOrder[i].GetComponent <Animation>().CrossFade("RifleSprinting", 0.00025f, PlayMode.StopAll);
                    }
                }
                else                  //weapon uses pistol sprinting animation
                                      //animate selected weapon up by setting time of animation to it's end and playing in reverse
                {
                    if (!FPSWalkerComponent.canRun)
                    {
                        weaponOrder[i].GetComponent <Animation>()["PistolSprinting"].normalizedTime = 1.0f;
                        weaponOrder[i].GetComponent <Animation>()["PistolSprinting"].speed          = -1.5f;
                        weaponOrder[i].GetComponent <Animation>().CrossFade("PistolSprinting", 0.00025f, PlayMode.StopAll);
                    }
                    else
                    {
                        //if player is sprinting, keep weapon in sprinting position during weapon switch
                        weaponOrder[i].GetComponent <Animation>()["PistolSprinting"].normalizedTime = 1.0f;
                        weaponOrder[i].GetComponent <Animation>()["PistolSprinting"].speed          = 1.5f;
                        weaponOrder[i].GetComponent <Animation>().CrossFade("PistolSprinting", 0.00025f, PlayMode.StopAll);
                    }
                }

                //update transform reference of active weapon object in other scipts
                IronsightsComponent.gun = weaponOrder[i].transform;
                //update active weapon object reference in other scipts
                IronsightsComponent.gunObj = weaponOrder[i];
                CameraKickComponent.gun    = weaponOrder[i];
            }
            else
            {
                //reset transform of deactivated gun to make it in neutral position when selected again
                //use weapon parent transform.position instead of Camera.main.transform.position
                //or Camera.main.transform.localPosition to avoid positioning bugs due to camera pos changing with walking bob and kick
                weaponOrder[i].transform.position = myTransform.position;

                if (!weaponOrder[i].GetComponent <WeaponBehavior>().PistolSprintAnim)               //weapon uses rifle sprinting animation
                //reset animation
                {
                    weaponOrder[i].GetComponent <Animation>()["RifleSprinting"].normalizedTime = 1.0f;
                }
                else                  //weapon uses pistol sprinting animation
                                      //reset animation
                {
                    weaponOrder[i].GetComponent <Animation>()["PistolSprinting"].normalizedTime = 1.0f;
                }
                //synchronize sprintState var in WeaponBehavior script
                weaponOrder[i].GetComponent <WeaponBehavior>().sprintState = true;

                                #if UNITY_3_5
                // Activate the selected weapon
                weaponOrder[i].SetActiveRecursively(false);
                                #else
                // Activate the selected weapon
                weaponOrder[i].SetActive(false);
                                #endif
            }
        }
    }
Exemplo n.º 2
0
    public IEnumerator SelectWeapon(int index, bool isOffhandThrow = false, bool endOffhandThrow = false)
    {
        CameraAnimationComponent     = Camera.main.GetComponent <Animation>();
        WeaponMeshAnimationComponent = CurrentWeaponBehaviorComponent.weaponMesh.GetComponent <Animation>();
        WeaponObjAnimationComponent  = weaponOrder[currentWeapon].GetComponent <Animation>();

        //we are not dropping a weapon anymore if one has been selected
        dropWeapon = false;

        //do not proceed with selecting weapon if player doesn't have it in their inventory
        //but make an exception for the null/unarmed weapon for when the player presses the holster button
        //also dont allow weapon switch if player is climbing, swimming, or holding object and their weapon is lowered
        WeaponBehavior ThisWeaponBehavior = weaponOrder[index].GetComponent <WeaponBehavior>();

        if ((!ThisWeaponBehavior.haveWeapon && index != 0) ||
            (!ThisWeaponBehavior.cycleSelect && !isOffhandThrow) ||
            FPSWalkerComponent.hideWeapon ||
            pullGrenadeState)
        {
            yield break;
        }

        if (index != 0)        //if a weapon is selected, prevent unarmed/null weapon from being selected in selection cycle
        {
            weaponOrder[0].GetComponent <WeaponBehavior>().haveWeapon = false;
        }

        //cancel zooming when switching
        FPSPlayerComponent.zoomed = false;

        if (CurrentWeaponBehaviorComponent.useLight)
        {
            if (CurrentWeaponBehaviorComponent.lightConeMesh)
            {
                CurrentWeaponBehaviorComponent.lightConeMesh.enabled = false;
            }
            if (CurrentWeaponBehaviorComponent.spot)
            {
                CurrentWeaponBehaviorComponent.spot.enabled = false;
            }
            if (CurrentWeaponBehaviorComponent.point)
            {
                CurrentWeaponBehaviorComponent.point.enabled = false;
            }
        }

        //reset non-magazine reload if interrupted by weapon switch
        if (CurrentWeaponBehaviorComponent.bulletsToReload != CurrentWeaponBehaviorComponent.bulletsPerClip &&
            WeaponMeshAnimationComponent["Neutral"] &&
            IronsightsComponent.reloading)
        {
            //play neutral animation when putting weapon away to prevent neutral anim glitch at start of next reload
            WeaponMeshAnimationComponent["Neutral"].speed = 1.5f;
            WeaponMeshAnimationComponent.Play("Neutral", PlayMode.StopSameLayer);
            //reset bulletsReloaded to prevent delay of reloading the next time we reload this weapon
            CurrentWeaponBehaviorComponent.bulletsReloaded = 0;
        }

        //cancel reloading when switching
        IronsightsComponent.reloading = false;                  //set IronSights Reloading var to false
        CurrentWeaponBehaviorComponent.StopCoroutine("Reload"); //stop the Reload function if it is running

        switchTime = Time.time;

        if (Time.timeSinceLevelLoad > 1)
        {
            if (!offhandThrowActive && !displayingGrenade)
            {
                //play weapon switch sound if not the first call to this function after level load
                aSource.clip   = changesnd;
                aSource.volume = 1.0f;
                aSource.Play();
            }

            //play camera weapon switching animation
            CameraAnimationComponent["CameraSwitch"].speed = 1.0f;            //set camera animation speed
            CameraAnimationComponent.Rewind("CameraSwitch");
            CameraAnimationComponent.CrossFade("CameraSwitch", 0.1f, PlayMode.StopSameLayer);
        }

        //if weapon uses rifle sprinting animation, set speed and play animation
        if (!CurrentWeaponBehaviorComponent.PistolSprintAnim)
        {
            //animate previous weapon down
            if (!FPSWalkerComponent.canRun && !FPSWalkerComponent.proneMove)
            {
                WeaponObjAnimationComponent["RifleSprinting"].normalizedTime = 0;
                WeaponObjAnimationComponent["RifleSprinting"].speed          = 1.5f;
                WeaponObjAnimationComponent.CrossFade("RifleSprinting", 0.00025f, PlayMode.StopSameLayer);
            }
            else
            {
                //if player is sprinting, keep weapon in sprinting position during weapon switch
                WeaponObjAnimationComponent["RifleSprinting"].normalizedTime = 1;
            }
        }
        else          //weapon uses pistol sprinting animation
                      //animate previous weapon down
        {
            if (!FPSWalkerComponent.canRun && !FPSWalkerComponent.proneMove)
            {
                WeaponObjAnimationComponent["PistolSprinting"].normalizedTime = 0;
                WeaponObjAnimationComponent["PistolSprinting"].speed          = 1.5f;
                WeaponObjAnimationComponent.CrossFade("PistolSprinting", 0.00025f, PlayMode.StopSameLayer);
            }
            else
            {
                //if player is sprinting, keep weapon in sprinting position during weapon switch
                WeaponObjAnimationComponent["PistolSprinting"].normalizedTime = 1;
            }
        }

        if (Time.timeSinceLevelLoad > 2)
        {
            if (!CurrentWeaponBehaviorComponent.verticalWeapon || isOffhandThrow)
            {
                //move weapon down while switching
                IronsightsComponent.switchMove = -0.4f;
            }
            else
            {
                //move vertical oriented weapons down further while switching because they take more vertical screen space than guns
                IronsightsComponent.switchMove = -1.2f;
            }

            //wait for weapon down animation to play before switching weapons and animating weapon up
            yield return(new WaitForSeconds(0.2f));
        }

        //immediately switch weapons (activate called weaponOrder index and deactivate all others)
        for (int i = 0; i < weaponOrder.Length; i++)
        {
            CurWeaponObjAnimationComponent = weaponOrder[i].GetComponent <Animation>();

            if (i == index)
            {
                //update transform reference of active weapon object in other scipts
                IronsightsComponent.gun = weaponOrder[i].transform;
                //update active weapon object reference in other scipts
                IronsightsComponent.gunObj = weaponOrder[i];
                IronsightsComponent.WeaponBehaviorComponent = weaponOrder[i].GetComponent <WeaponBehavior>();
                FPSPlayerComponent.WeaponBehaviorComponent  = weaponOrder[i].GetComponent <WeaponBehavior>();
                CameraControlComponent.gun     = weaponOrder[i];
                CurrentWeaponBehaviorComponent = weaponOrder[i].GetComponent <WeaponBehavior>();

                // Activate the selected weapon
                weaponOrder[i].SetActive(true);

                if (endOffhandThrow)
                {
                    CurrentWeaponBehaviorComponent.isOffhandThrow = true;
                    switchTime = Time.time - 0.5f;                    //allow weapon to start firing sooner after switching from offhand grenade throw than when recovering from sprinting
                }

                CurrentWeaponBehaviorComponent.InitializeWeapon();

                //get current weapon value from index
                currentWeapon = index;

                //synchronize current and previous weapon's y pos for correct offscreen switching, use localPosition not position for correct transforms
                weaponOrder[i].transform.localPosition = weaponOrder[i].transform.localPosition + new Vector3(0, weaponOrder[i].transform.localPosition.y - 0.3f, 0);

                if (Time.timeSinceLevelLoad > 2)
                {
                    //move weapon up when switch finishes
                    IronsightsComponent.switchMove = 0;
                }

                //if weapon uses rifle sprinting animation set speed and animate
                if (!weaponOrder[i].GetComponent <WeaponBehavior>().PistolSprintAnim)
                {
                    //animate selected weapon up by setting time of animation to it's end and playing in reverse
                    if (!FPSWalkerComponent.canRun && !FPSWalkerComponent.proneMove)
                    {
                        CurWeaponObjAnimationComponent["RifleSprinting"].normalizedTime = 1.0f;
                        CurWeaponObjAnimationComponent["RifleSprinting"].speed          = -1.5f;
                        CurWeaponObjAnimationComponent.CrossFade("RifleSprinting", 0.00025f, PlayMode.StopSameLayer);
                    }
                    else
                    {
                        //if player is sprinting, keep weapon in sprinting position during weapon switch
                        CurWeaponObjAnimationComponent["RifleSprinting"].normalizedTime = 1.0f;
                        CurWeaponObjAnimationComponent["RifleSprinting"].speed          = 1.5f;
                        CurWeaponObjAnimationComponent.CrossFade("RifleSprinting", 0.00025f, PlayMode.StopSameLayer);
                    }
                }
                else                  //weapon uses pistol sprinting animation
                                      //animate selected weapon up by setting time of animation to it's end and playing in reverse
                {
                    if (!FPSWalkerComponent.canRun && !FPSWalkerComponent.proneMove)
                    {
                        CurWeaponObjAnimationComponent["PistolSprinting"].normalizedTime = 1.0f;
                        CurWeaponObjAnimationComponent["PistolSprinting"].speed          = -1.5f;
                        CurWeaponObjAnimationComponent.CrossFade("PistolSprinting", 0.00025f, PlayMode.StopSameLayer);
                    }
                    else
                    {
                        //if player is sprinting, keep weapon in sprinting position during weapon switch
                        CurWeaponObjAnimationComponent["PistolSprinting"].normalizedTime = 1.0f;
                        CurWeaponObjAnimationComponent["PistolSprinting"].speed          = 1.5f;
                        CurWeaponObjAnimationComponent.CrossFade("PistolSprinting", 0.00025f, PlayMode.StopSameLayer);
                    }
                }
            }
            else
            {
                //reset transform of deactivated gun to make it in neutral position when selected again
                //use weapon parent transform.position instead of Camera.main.transform.position
                //or Camera.main.transform.localPosition to avoid positioning bugs due to camera pos changing with walking bob and kick
                weaponOrder[i].transform.position = myTransform.position;

                if (!weaponOrder[i].GetComponent <WeaponBehavior>().PistolSprintAnim)               //weapon uses rifle sprinting animation
                //reset animation
                {
                    CurWeaponObjAnimationComponent["RifleSprinting"].normalizedTime = 1.0f;
                }
                else                  //weapon uses pistol sprinting animation
                                      //reset animation
                {
                    CurWeaponObjAnimationComponent["PistolSprinting"].normalizedTime = 1.0f;
                }
                //synchronize sprintState var in WeaponBehavior script
                weaponOrder[i].GetComponent <WeaponBehavior>().sprintState = true;

                // Activate the selected weapon
                weaponOrder[i].SetActive(false);
            }
        }
        WatchedAdAmmo = false;
    }