Inheritance: MonoBehaviour
    ///////////////////////////////////////////////////////////
    // reloads the current shooter with 'amount' ammo and deals
    // with states and timers concerning reloading
    ///////////////////////////////////////////////////////////
    public void Reload(int amount)
    {
        if (CurrentShooter == null)
        {
            return;
        }

        if (Time.time < CurrentShooter.NextAllowedReloadTime)
        {
            return;
        }

        if (m_ReenableWeaponStatesTimer != null)
        {
            m_ReenableWeaponStatesTimer.Cancel();
        }

        SetState("Reload", true);

        CurrentShooter.Reload(amount);

        if (m_DoneReloadingTimer != null)
        {
            m_DoneReloadingTimer.Cancel();
        }
        m_DoneReloadingTimer = vp_Timer.In(CurrentShooter.AmmoReloadTime, delegate()
        {
            SetState("Reload", false);
        });
    }
Beispiel #2
0
 ///////////////////////////////////////////////////////////
 // static version of the Cancel method. NOTE: calling
 // 'CancelInvoke' on a vp_Timer is the same as calling
 // 'Cancel'.
 ///////////////////////////////////////////////////////////
 public static void Cancel(vp_Timer timer)
 {
     if (timer == null)
     {
         return;
     }
     timer.Cancel();
 }
    /// <summary>
    ///
    /// </summary>
    private void OnEnable()
    {
        vp_Timer timer = (vp_Timer)target;

        if (!timer.WasAddedCorrectly)
        {
            EditorUtility.DisplayDialog("Ooops!", "vp_Timer can't be added in the Inspector. It must be called from script. See the documentation for more info.", "OK");
            DestroyImmediate(timer);
        }
    }
    ///////////////////////////////////////////////////////////
    // this method schedules a check in 'seconds' to reenable
    // any active states on the controller that may have been
    // temporarily disabled on the current weapon
    ///////////////////////////////////////////////////////////
    public void ReenableWeaponStatesIn(float seconds)
    {
        // cancel the timer if it's currently running, to prevent
        // problems with button spamming
        if (m_ReenableWeaponStatesTimer != null)
        {
            m_ReenableWeaponStatesTimer.Cancel();
        }

        m_ReenableWeaponStatesTimer = vp_Timer.In(seconds, delegate()
        {
            if (Controller.StateEnabled("Zoom"))
            {
                if (CurrentWeapon != null)
                {
                    CurrentWeapon.SetState("Zoom", true);
                }
                if (CurrentShooter != null)
                {
                    CurrentShooter.SetState("Zoom", true);
                }
                return;                 // don't reenable 'Crouch' or 'Run' on the weapon while zooming
            }
            if (Controller.StateEnabled("Crouch"))
            {
                if (CurrentWeapon != null)
                {
                    CurrentWeapon.SetState("Crouch", true);
                }
                if (CurrentShooter != null)
                {
                    CurrentShooter.SetState("Crouch", true);
                }
            }
            if (Controller.StateEnabled("Run"))
            {
                if (CurrentWeapon != null)
                {
                    CurrentWeapon.SetState("Run", true);
                }
                if (CurrentShooter != null)
                {
                    CurrentShooter.SetState("Run", true);
                }
            }
        });
    }
Beispiel #5
0
    // internal schedule method
    private static vp_Timer In(float time, Callback func, ArgCallback argFunc, object args, int iterations, float interval)
    {
        if (m_GameObject == null)
        {
            m_GameObject = new GameObject("Timers");
            Object.DontDestroyOnLoad(m_GameObject);

            // by default gameobject will be invisible in the hierarchy.
            // disabling this may be useful for debugging
            m_GameObject.hideFlags = HideFlags.HideInHierarchy;
        }

        vp_Timer firstTimer = null;

        float currentTime = time;

        interval = (interval == 0.0f) ? time : interval;

        for (int i = 0; i < iterations; i++)
        {
            vp_Timer timer = m_GameObject.AddComponent <vp_Timer>();
            if (i == 0)
            {
                firstTimer = timer;
            }
            else
            {
                firstTimer.m_Iterations.Add(timer);
            }

            if (func != null)
            {
                timer.Schedule(currentTime, func);
            }
            else if (argFunc != null)
            {
                timer.Schedule(currentTime, argFunc, args);
            }

            currentTime += interval;
        }

        return(firstTimer);
    }
Beispiel #6
0
    private void DoExamplePistolReloadSequence()
    {
        // NOTE: (the 'Reload' state is activated in the 'Reload'
        // method of vp_FPSPlayer so the pistol is already tilted
        // to the side)

        // always disable the timer if it's running, to avoid hickups
        // caused by button spamming
        if (m_PistolReloadTimer != null)
            m_PistolReloadTimer.Cancel();

        // after 0.4 seconds, simulate replacing the clip
        m_PistolReloadTimer = vp_Timer.In(0.4f, delegate()
        {
            // but first make sure we're still reloading since the player
            // may have switched weapons
            if (!m_Player.Camera.CurrentWeapon.StateEnabled("Reload"))
                return;

            // apply a force as if hitting the gun from below
            m_Player.Camera.CurrentWeapon.AddForce2(new Vector3(0, 0.05f, 0), new Vector3(0, 0, 0));

            // 0.15 seconds later, twist the gun backwards
            if (m_PistolReloadTimer != null)
                m_PistolReloadTimer.Cancel();
            m_PistolReloadTimer = vp_Timer.In(0.15f, delegate()
            {
                if (!m_Player.Camera.CurrentWeapon.StateEnabled("Reload"))
                    return;
                // to do this, switch from the pistol 'Reload' state to
                // its 'Reload2' state
                m_Player.Camera.SetState("Reload", false);
                m_Player.Camera.CurrentWeapon.SetState("Reload", false);
                m_Player.Camera.CurrentWeapon.SetState("Reload2", true);
                m_Player.Camera.CurrentWeapon.RotationOffset.z = 0;
                m_Player.Camera.CurrentWeapon.Refresh();

                // after 0.35 seconds, pull the slide
                if (m_PistolReloadTimer != null)
                    m_PistolReloadTimer.Cancel();
                m_PistolReloadTimer = vp_Timer.In(0.35f, delegate()
                {
                    if (!m_Player.Camera.CurrentWeapon.StateEnabled("Reload2"))
                        return;

                    // apply a force pulling the whole gun backwards briefly
                    m_Player.Camera.CurrentWeapon.AddForce2(new Vector3(0, 0, -0.05f), new Vector3(5, 0, 0));

                    // 0.1 seconds later, disable the reload state to point
                    // the gun forward again
                    if (m_PistolReloadTimer != null)
                        m_PistolReloadTimer.Cancel();
                    m_PistolReloadTimer = vp_Timer.In(0.1f, delegate()
                    {
                        m_Player.SetState("Reload2", false);
                        // if the player is crouching, restore the zoom state on the pistol
                        if (m_Player.Controller.StateEnabled("Zoom"))
                            m_Player.Camera.CurrentWeapon.SetState("Zoom", true);
                        m_Player.CurrentShooter.NextAllowedFireTime = Time.time + 0.5f;
                        m_Player.CurrentShooter.NextAllowedReloadTime = Time.time + 0.5f;
                    });
                });
            });

        });
    }
    ///////////////////////////////////////////////////////////
    // demo screen to explain external forces
    ///////////////////////////////////////////////////////////
    private void DemoForces()
    {
        Demo.DrawBoxes("external forces", "The camera and weapon are mounted on 8 positional and angular SPRINGS.\nEXTERNAL FORCES can be applied to these in various ways, creating unique movement patterns every time. This is useful for shockwaves, explosion knockback and earthquakes.", m_ImageLeftArrow, m_ImageRightArrow);

        if (Demo.FirstFrame)
        {
            Demo.DrawCrosshair = false;
            LoadCamera(StompingCamera);
            SwitchWeapon(1, ModernWeapon, null, -1, false, false);
            m_Player.Controller.Load(SmackController);
            m_Player.Camera.SnapZoom();
            Demo.FirstFrame = false;
            Demo.Teleport(m_ForcesPos, m_ForcesAngle);
            Demo.ButtonColumnArrowY = -100.0f;
        }

        if (Demo.ShowGUI)
        {

            // draw toggle column showing force examples
            Demo.ButtonSelection = -1;
            string[] strings = new string[] { "Earthquake", "Boss Stomp", "Incoming Artillery", "Crashing Airplane"};
            Demo.ButtonSelection = Demo.ButtonColumn(150, Demo.ButtonSelection, strings, m_ImageRightPointer);
            if (Demo.ButtonSelection != -1)
            {
                switch (Demo.ButtonSelection)
                {
                    case 0:	// --- Earthquake ---
                        LoadCamera(StompingCamera);
                        m_Player.Controller.Load(SmackController);
                        m_Player.Camera.DoEarthQuake(0.1f, 0.1f, 10.0f);
                        Demo.ButtonColumnArrowFadeoutTime = Time.time + 9;
                        m_AudioSource.Stop();
                        m_AudioSource.PlayOneShot(m_EarthquakeSound);
                        break;
                    case 1:	// --- Boss Stomp ---
                        m_Player.Camera.StopEarthQuake();
                        LoadCamera(ArtilleryCamera);
                        m_Player.Controller.Load(SmackController);
                        m_Player.Camera.DoStomp(1.0f);
                        Demo.ButtonColumnArrowFadeoutTime = Time.time;
                        m_AudioSource.Stop();
                        m_AudioSource.PlayOneShot(m_StompSound);
                        break;
                    case 2:	// --- Incoming Artillery ---
                        m_Player.Camera.StopEarthQuake();
                        LoadCamera(ArtilleryCamera);
                        m_Player.Controller.Load(ArtilleryController);
                        m_Player.Camera.DoBomb(1);
                        m_Player.Controller.AddForce(UnityEngine.Random.Range(-1.5f, 1.5f), 0.1f,
                                                                        UnityEngine.Random.Range(-1.5f, 1.5f));
                        Demo.ButtonColumnArrowFadeoutTime = Time.time + 1;
                        m_AudioSource.Stop();
                        m_AudioSource.PlayOneShot(m_ExplosionSound);
                        break;
                    case 3:	// --- Crashing Airplane ---
                        LoadCamera(StompingCamera);
                        m_Player.Controller.Load(SmackController);
                        m_Player.Camera.DoEarthQuake(0.25f, 0.2f, 10.0f, 1.0f, 6.5f);
                        Demo.ButtonColumnArrowFadeoutTime = Time.time + 9;
                        m_AudioSource.Stop();
                        m_AudioSource.PlayOneShot(m_EarthquakeSound);
                        m_Player.Camera.RenderingFieldOfView = 80;
                        m_Player.Camera.Zoom();
                        if (m_ChrashingAirplaneRestoreTimer != null)
                            m_ChrashingAirplaneRestoreTimer.Cancel();
                        m_ChrashingAirplaneRestoreTimer = vp_Timer.In(9, delegate() { m_Player.Camera.RenderingFieldOfView = 60; m_Player.Camera.Zoom(); });
                        break;
                }
                Demo.LastInputTime = Time.time;
            }

            // show a screenshot preview of the mouse input editor section
            // in the bottom left corner.
            Demo.DrawEditorPreview(m_ImageWeaponPosition, m_ImageEditorPreview, m_ImageEditorScreenshot);
        }
    }
    ///////////////////////////////////////////////////////////
    // moves the current weapon model to its exit offset, changes
    // the weapon model and moves the new weapon into view.
    // an optional callback may be provided to execute a method
    // as the new weapon becomes active
    ///////////////////////////////////////////////////////////
    public void SwitchWeapon(int weapon, WeaponSwitchDelegate callback = null)
    {
        // prevent firing while putting the current weapon away
        if (CurrentShooter != null)
        {
            CurrentShooter.PreventFiring(0.5f);
        }

        if (CurrentWeapon != null)
        {
            CurrentWeapon.StateManager.Reset();

            // rotate and move the current weapon out of view
            CurrentWeapon.PositionOffset = CurrentWeapon.PositionExitOffset;
            CurrentWeapon.RotationOffset = CurrentWeapon.RotationExitOffset;
            CurrentWeapon.Refresh();

            // play unwield sound
            if (CurrentWeapon.audio != null)
            {
                if (CurrentWeapon.SoundUnWield != null)
                {
                    CurrentWeapon.audio.pitch = 1;
                    CurrentWeapon.audio.PlayOneShot(CurrentWeapon.SoundUnWield);
                }
            }
        }

        // cancel any already ongoing weapon switching activity
        CancelWeaponSwitch();

        // create a new event to switch the weapon once model has had
        // time enough to rotate out of view. 0.15 seconds should do.
        m_SwitchWeaponTimer = vp_Timer.In(0.15f, delegate()
        {
            // switch weapon
            SetWeapon(weapon);

            // prevent firing while taking out the new weapon
            if (CurrentShooter != null)
            {
                CurrentShooter.PreventFiring(0.5f);
            }

            // force the 'rotated down' angle and position onto the new
            // weapon when it spawns, or it will pop into view
            if (CurrentWeapon != null)
            {
                CurrentWeapon.RotationOffset = CurrentWeapon.RotationExitOffset;
                CurrentWeapon.PositionOffset = CurrentWeapon.PositionExitOffset;
                CurrentWeapon.SnapSprings();
                CurrentWeapon.SnapPivot();
                CurrentWeapon.SnapZoom();
                CurrentWeapon.Refresh();
            }

            // create an event to show the new weapon in 0.15 seconds
            m_ShowWeaponTimer = vp_Timer.In(0.15f, delegate()
            {
                if (CurrentWeapon != null)
                {
                    // we do this by smoothly restoring the loaded weapon's
                    // desired position and angle
                    CurrentWeapon.PositionOffset = CurrentWeapon.DefaultPosition;
                    CurrentWeapon.RotationOffset = CurrentWeapon.DefaultRotation;
                    CurrentWeapon.Refresh();

                    // play wield sound
                    if (CurrentWeapon.audio != null)
                    {
                        if (CurrentWeapon.SoundWield != null)
                        {
                            CurrentWeapon.audio.pitch = 1;
                            CurrentWeapon.audio.PlayOneShot(CurrentWeapon.SoundWield);
                        }
                    }
                }

                // execute user defined callback, if provided
                if (callback != null)
                {
                    callback();
                }
            });
        });
    }
    ///////////////////////////////////////////////////////////
    // demo screen to explain external forces
    ///////////////////////////////////////////////////////////
    private void DemoForces()
    {
        Demo.DrawBoxes("external forces", "The camera and weapon are mounted on 8 positional and angular SPRINGS.\nEXTERNAL FORCES can be applied to these in various ways, creating unique movement patterns every time. This is useful for shockwaves, explosion knockback and earthquakes.", m_ImageLeftArrow, m_ImageRightArrow);

        if (Demo.FirstFrame)
        {
            Demo.DrawCrosshair = false;
            LoadCamera(StompingCamera);
            SwitchWeapon(1, ModernWeapon, null, -1, false, false);
            m_Player.Controller.Load(SmackController);
            m_Player.Camera.SnapZoom();
            Demo.FirstFrame = false;
            Demo.Teleport(m_ForcesPos, m_ForcesAngle);
            Demo.ButtonColumnArrowY = -100.0f;
        }

        if (Demo.ShowGUI)
        {
            // draw toggle column showing force examples
            Demo.ButtonSelection = -1;
            string[] strings = new string[] { "Earthquake", "Boss Stomp", "Incoming Artillery", "Crashing Airplane" };
            Demo.ButtonSelection = Demo.ButtonColumn(150, Demo.ButtonSelection, strings, m_ImageRightPointer);
            if (Demo.ButtonSelection != -1)
            {
                switch (Demo.ButtonSelection)
                {
                case 0:                         // --- Earthquake ---
                    LoadCamera(StompingCamera);
                    m_Player.Controller.Load(SmackController);
                    m_Player.Camera.DoEarthQuake(0.1f, 0.1f, 10.0f);
                    Demo.ButtonColumnArrowFadeoutTime = Time.time + 9;
                    m_AudioSource.Stop();
                    m_AudioSource.PlayOneShot(m_EarthquakeSound);
                    break;

                case 1:                         // --- Boss Stomp ---
                    m_Player.Camera.StopEarthQuake();
                    LoadCamera(ArtilleryCamera);
                    m_Player.Controller.Load(SmackController);
                    m_Player.Camera.DoStomp(1.0f);
                    Demo.ButtonColumnArrowFadeoutTime = Time.time;
                    m_AudioSource.Stop();
                    m_AudioSource.PlayOneShot(m_StompSound);
                    break;

                case 2:                         // --- Incoming Artillery ---
                    m_Player.Camera.StopEarthQuake();
                    LoadCamera(ArtilleryCamera);
                    m_Player.Controller.Load(ArtilleryController);
                    m_Player.Camera.DoBomb(1);
                    m_Player.Controller.AddForce(UnityEngine.Random.Range(-1.5f, 1.5f), 0.1f,
                                                 UnityEngine.Random.Range(-1.5f, 1.5f));
                    Demo.ButtonColumnArrowFadeoutTime = Time.time + 1;
                    m_AudioSource.Stop();
                    m_AudioSource.PlayOneShot(m_ExplosionSound);
                    break;

                case 3:                         // --- Crashing Airplane ---
                    LoadCamera(StompingCamera);
                    m_Player.Controller.Load(SmackController);
                    m_Player.Camera.DoEarthQuake(0.25f, 0.2f, 10.0f, 1.0f, 6.5f);
                    Demo.ButtonColumnArrowFadeoutTime = Time.time + 9;
                    m_AudioSource.Stop();
                    m_AudioSource.PlayOneShot(m_EarthquakeSound);
                    m_Player.Camera.RenderingFieldOfView = 80;
                    m_Player.Camera.Zoom();
                    if (m_ChrashingAirplaneRestoreTimer != null)
                    {
                        m_ChrashingAirplaneRestoreTimer.Cancel();
                    }
                    m_ChrashingAirplaneRestoreTimer = vp_Timer.In(9, delegate() { m_Player.Camera.RenderingFieldOfView = 60; m_Player.Camera.Zoom(); });
                    break;
                }
                Demo.LastInputTime = Time.time;
            }


            // show a screenshot preview of the mouse input editor section
            // in the bottom left corner.
            Demo.DrawEditorPreview(m_ImageWeaponPosition, m_ImageEditorPreview, m_ImageEditorScreenshot);
        }
    }
Beispiel #10
0
 ///////////////////////////////////////////////////////////
 // static version of the Cancel method. NOTE: calling
 // 'CancelInvoke' on a vp_Timer is the same as calling
 // 'Cancel'.
 ///////////////////////////////////////////////////////////
 public static void Cancel(vp_Timer timer)
 {
     if (timer == null)
         return;
     timer.Cancel();
 }
Beispiel #11
0
	/// <summary>
	/// cancels a timer if the passed timer handle is still active
	/// </summary>
	private static void Cancel(vp_Timer.Handle handle)
	{

		if (handle == null)
			return;

		// NOTE: the below Active check is super-important for verifying timer
		// handle integrity. recycling 'handle.Event' if 'handle.Active' is false
		// will cancel the wrong event and lead to some other timer never firing
		// (this is because timer events are recycled but not their timer handles).
		if (handle.Active)
		{
			// setting the 'Id' property to zero will result in DueTime also
			// becoming zero, sending the event to 'Execute' in the next frame
			// where it will be recycled instead of executed
			handle.Id = 0;
			return;
		}

	}
Beispiel #12
0
    ///////////////////////////////////////////////////////////
    // moves the current weapon model to its exit offset, changes
    // the weapon model and moves the new weapon into view.
    // an optional callback may be provided to execute a method
    // as the new weapon becomes active
    ///////////////////////////////////////////////////////////
    public void SwitchWeapon(int weapon, WeaponSwitchDelegate callback = null)
    {
        // prevent firing while putting the current weapon away
        if (CurrentShooter != null)
            CurrentShooter.PreventFiring(0.5f);

        if (CurrentWeapon != null)
        {

            CurrentWeapon.StateManager.Reset();

            // rotate and move the current weapon out of view
            CurrentWeapon.PositionOffset = CurrentWeapon.PositionExitOffset;
            CurrentWeapon.RotationOffset = CurrentWeapon.RotationExitOffset;
            CurrentWeapon.Refresh();

            // play unwield sound
            if (CurrentWeapon.audio != null)
            {
                if (CurrentWeapon.SoundUnWield != null)
                {
                    CurrentWeapon.audio.pitch = 1;
                    CurrentWeapon.audio.PlayOneShot(CurrentWeapon.SoundUnWield);
                }
            }

        }

        // cancel any already ongoing weapon switching activity
        CancelWeaponSwitch();

        // create a new event to switch the weapon once model has had
        // time enough to rotate out of view. 0.15 seconds should do.
        m_SwitchWeaponTimer = vp_Timer.In(0.15f, delegate()
        {

            // switch weapon
            SetWeapon(weapon);

            // prevent firing while taking out the new weapon
            if (CurrentShooter != null)
                CurrentShooter.PreventFiring(0.5f);

            // force the 'rotated down' angle and position onto the new
            // weapon when it spawns, or it will pop into view
            if (CurrentWeapon != null)
            {
                CurrentWeapon.RotationOffset = CurrentWeapon.RotationExitOffset;
                CurrentWeapon.PositionOffset = CurrentWeapon.PositionExitOffset;
                CurrentWeapon.SnapSprings();
                CurrentWeapon.SnapPivot();
                CurrentWeapon.SnapZoom();
                CurrentWeapon.Refresh();
            }

            // create an event to show the new weapon in 0.15 seconds
            m_ShowWeaponTimer = vp_Timer.In(0.15f, delegate()
            {

                if (CurrentWeapon != null)
                {
                    // we do this by smoothly restoring the loaded weapon's
                    // desired position and angle
                    CurrentWeapon.PositionOffset = CurrentWeapon.DefaultPosition;
                    CurrentWeapon.RotationOffset = CurrentWeapon.DefaultRotation;
                    CurrentWeapon.Refresh();

                    // play wield sound
                    if (CurrentWeapon.audio != null)
                    {
                        if (CurrentWeapon.SoundWield != null)
                        {
                            CurrentWeapon.audio.pitch = 1;
                            CurrentWeapon.audio.PlayOneShot(CurrentWeapon.SoundWield);
                        }
                    }
                }

                // execute user defined callback, if provided
                if (callback != null)
                    callback();

            });
        });
    }
Beispiel #13
0
    ///////////////////////////////////////////////////////////
    // reloads the current shooter with 'amount' ammo and deals
    // with states and timers concerning reloading
    ///////////////////////////////////////////////////////////
    public void Reload(int amount)
    {
        if (CurrentShooter == null)
            return;

        if (Time.time < CurrentShooter.NextAllowedReloadTime)
            return;

        if (m_ReenableWeaponStatesTimer != null)
            m_ReenableWeaponStatesTimer.Cancel();

        SetState("Reload", true);

        CurrentShooter.Reload(amount);

        if (m_DoneReloadingTimer != null)
            m_DoneReloadingTimer.Cancel();
        m_DoneReloadingTimer = vp_Timer.In(CurrentShooter.AmmoReloadTime, delegate()
        {
            SetState("Reload", false);
        });
    }
Beispiel #14
0
    ///////////////////////////////////////////////////////////
    // this method schedules a check in 'seconds' to reenable
    // any active states on the controller that may have been
    // temporarily disabled on the current weapon
    ///////////////////////////////////////////////////////////
    public void ReenableWeaponStatesIn(float seconds)
    {
        // cancel the timer if it's currently running, to prevent
        // problems with button spamming
        if (m_ReenableWeaponStatesTimer != null)
            m_ReenableWeaponStatesTimer.Cancel();

        m_ReenableWeaponStatesTimer = vp_Timer.In(seconds, delegate()
        {
            if (Controller.StateEnabled("Zoom"))
            {
                if (CurrentWeapon != null)
                    CurrentWeapon.SetState("Zoom", true);
                if (CurrentShooter != null)
                    CurrentShooter.SetState("Zoom", true);
                return;	// don't reenable 'Crouch' or 'Run' on the weapon while zooming
            }
            if (Controller.StateEnabled("Crouch"))
            {
                if (CurrentWeapon != null)
                    CurrentWeapon.SetState("Crouch", true);
                if (CurrentShooter != null)
                    CurrentShooter.SetState("Crouch", true);
            }
            if (Controller.StateEnabled("Run"))
            {
                if (CurrentWeapon != null)
                    CurrentWeapon.SetState("Run", true);
                if (CurrentShooter != null)
                    CurrentShooter.SetState("Run", true);
            }
        });
    }
Beispiel #15
0
    private void DoExamplePistolReloadSequence()
    {
        // NOTE: (the 'Reload' state is activated in the 'Reload'
        // method of vp_FPSPlayer so the pistol is already tilted
        // to the side)

        // always disable the timer if it's running, to avoid hickups
        // caused by button spamming
        if (m_PistolReloadTimer != null)
        {
            m_PistolReloadTimer.Cancel();
        }

        // after 0.4 seconds, simulate replacing the clip
        m_PistolReloadTimer = vp_Timer.In(0.4f, delegate()
        {
            // but first make sure we're still reloading since the player
            // may have switched weapons
            if (!m_Player.Camera.CurrentWeapon.StateEnabled("Reload"))
            {
                return;
            }

            // apply a force as if hitting the gun from below
            m_Player.Camera.CurrentWeapon.AddForce2(new Vector3(0, 0.05f, 0), new Vector3(0, 0, 0));

            // 0.15 seconds later, twist the gun backwards
            if (m_PistolReloadTimer != null)
            {
                m_PistolReloadTimer.Cancel();
            }
            m_PistolReloadTimer = vp_Timer.In(0.15f, delegate()
            {
                if (!m_Player.Camera.CurrentWeapon.StateEnabled("Reload"))
                {
                    return;
                }
                // to do this, switch from the pistol 'Reload' state to
                // its 'Reload2' state
                m_Player.Camera.SetState("Reload", false);
                m_Player.Camera.CurrentWeapon.SetState("Reload", false);
                m_Player.Camera.CurrentWeapon.SetState("Reload2", true);
                m_Player.Camera.CurrentWeapon.RotationOffset.z = 0;
                m_Player.Camera.CurrentWeapon.Refresh();

                // after 0.35 seconds, pull the slide
                if (m_PistolReloadTimer != null)
                {
                    m_PistolReloadTimer.Cancel();
                }
                m_PistolReloadTimer = vp_Timer.In(0.35f, delegate()
                {
                    if (!m_Player.Camera.CurrentWeapon.StateEnabled("Reload2"))
                    {
                        return;
                    }

                    // apply a force pulling the whole gun backwards briefly
                    m_Player.Camera.CurrentWeapon.AddForce2(new Vector3(0, 0, -0.05f), new Vector3(5, 0, 0));

                    // 0.1 seconds later, disable the reload state to point
                    // the gun forward again
                    if (m_PistolReloadTimer != null)
                    {
                        m_PistolReloadTimer.Cancel();
                    }
                    m_PistolReloadTimer = vp_Timer.In(0.1f, delegate()
                    {
                        m_Player.SetState("Reload2", false);
                        // if the player is crouching, restore the zoom state on the pistol
                        if (m_Player.Controller.StateEnabled("Zoom"))
                        {
                            m_Player.Camera.CurrentWeapon.SetState("Zoom", true);
                        }
                        m_Player.CurrentShooter.NextAllowedFireTime   = Time.time + 0.5f;
                        m_Player.CurrentShooter.NextAllowedReloadTime = Time.time + 0.5f;
                    });
                });
            });
        });
    }
Beispiel #16
0
    ///////////////////////////////////////////////////////////
    // moves the current weapon model out of view, changes the weapon
    // model and loads a new weapon preset, then moves the new weapon
    // into view. an optional shooter preset can be provided.
    ///////////////////////////////////////////////////////////
    void SwitchWeapon(int weapon, string weaponPreset, string shooterPreset = "")
    {
        if (m_Camera.CurrentWeapon == null)
            return;

        // switch weapons

        // prevent firing while putting the current weapon away
        vp_FPSShooter shooter = m_Camera.CurrentWeapon.GetComponent<vp_FPSShooter>();
        if (shooter != null)
            shooter.PreventFiring(0.75f);

        // rotate and move the weapon downwards
        m_Camera.CurrentWeapon.RotationOffset.x = 40;
        m_Camera.CurrentWeapon.PositionOffset.y = -1.0f;
        m_Camera.CurrentWeapon.RefreshSettings();

        // cancel any already ongoing weapon switching activity
        if (m_SwitchWeaponTimer != null)
            m_SwitchWeaponTimer.Cancel();

        // create a new event to switch the weapon once camera has had
        // time enough to rotate out of view. 0.15 seconds should do.
        m_SwitchWeaponTimer = vp_Timer.At(0.15f, delegate()
        {

            // switch weapon and load new weapon settings
            m_Camera.SetWeapon(weapon);
            m_Camera.CurrentWeapon.Load(weaponPreset);

            // prevent firing while taking out the new weapon
            shooter = m_Camera.CurrentWeapon.GetComponent<vp_FPSShooter>();
            if (shooter != null)
                shooter.PreventFiring(0.75f);

            // store the loaded weapon's desired position and angle
            Vector3 pos = m_Camera.CurrentWeapon.PositionOffset;
            Vector3 rot = m_Camera.CurrentWeapon.RotationOffset;

            // force the 'rotated down' angle and position onto the new
            // weapon when it spawns, or it will pop into view
            m_Camera.CurrentWeapon.RotationOffset.x = 40;
            m_Camera.CurrentWeapon.PositionOffset.y = -1.0f;
            m_Camera.CurrentWeapon.SnapSprings();
            m_Camera.CurrentWeapon.SnapPivot();
            m_Camera.CurrentWeapon.SnapZoom();
            m_Camera.CurrentWeapon.RefreshSettings();

            // if the calling method specified a shooter preset, load it
            if (!string.IsNullOrEmpty(shooterPreset))
            {
                if (m_Camera.CurrentShooter != null)
                    m_Camera.CurrentShooter.Load(shooterPreset);
            }

            // cancel any ongoing weapon showing activity
            if (m_ShowWeaponTimer != null)
                m_ShowWeaponTimer.Cancel();

            // create a new event to show the new weapon in 0.15 seconds
            m_ShowWeaponTimer = vp_Timer.At(0.15f, delegate()
            {
                // we do this by smoothly restoring the loaded weapon's
                // desired position and angle
                m_Camera.CurrentWeapon.PositionOffset = pos;
                m_Camera.CurrentWeapon.RotationOffset = rot;
                m_Camera.CurrentWeapon.RefreshSettings();
            });
        });
    }