/// <summary> /// sets a new default weapon preset. an optional shooter /// preset may be provided. if the optional 'smoothFade' bool /// is set to false, the new preset will snap in place. /// NOTE: this is just demo walkthrough code. manipulating /// the default state like this is no longer recommended. /// use additional states instead. /// </summary> public void SetWeaponPreset(TextAsset weaponPreset, TextAsset shooterPreset = null, bool smoothFade = true) { if (WeaponHandler.CurrentWeapon == null) { return; } WeaponHandler.CurrentWeapon.Load(weaponPreset); if (!smoothFade) { ((vp_FPWeapon)WeaponHandler.CurrentWeapon).SnapSprings(); ((vp_FPWeapon)WeaponHandler.CurrentWeapon).SnapPivot(); ((vp_FPWeapon)WeaponHandler.CurrentWeapon).SnapZoom(); } WeaponHandler.CurrentWeapon.Refresh(); if (shooterPreset != null) { if (CurrentShooter != null) { CurrentShooter.Load(shooterPreset); } } CurrentShooter.Refresh(); }
/////////////////////////////////////////////////////////// // 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); }); }
/////////////////////////////////////////////////////////// // fire the current weapon when player presses the left // mouse button. this temporarily disables the 'Crouch' and // 'Run' states on the current weapon /////////////////////////////////////////////////////////// protected void InputFire() { // fire button pressed / held down if (Input.GetMouseButton(0)) { if (CurrentWeapon != null) { // if firing while crouching or running, point the weapon // straight forward if (CurrentWeapon.StateEnabled("Crouch")) { CurrentWeapon.SetState("Crouch", false); } if (CurrentWeapon.StateEnabled("Run")) { CurrentWeapon.SetState("Run", false); } // fire if we have a shooter component and the mouse cursor // is not visible if (CurrentShooter != null && LockCursor) { CurrentShooter.Fire(); } // TIP: to play a conventional animation upon firing (typically // used for melee weapons) uncomment one of the lines below // (see the Unity docs for more available methods & parameters). //CurrentWeapon.animation.Play(); // play the default animation //CurrentWeapon.animation.Play("your_animation"); // play the animation named 'your_animation' and stop all other animations in the same layer //CurrentWeapon.animation.Play("your_animation", PlayMode.StopAll); // play 'your_animation' and stop all other animations //CurrentWeapon.animation.CrossFade("your_animation", 0.2f); // fade in 'your_animation' and fade out all other animations over 0.2 seconds } } // fire button released if (Input.GetMouseButtonUp(0)) { // schedule to reenable 'Crouch' and / or 'Run' in half a second ReenableWeaponStatesIn(0.5f); // disregard refire delay when firing with no ammo if (CurrentShooter != null) { if (CurrentShooter.AmmoCount == 0) { CurrentShooter.NextAllowedFireTime = Time.time; } } } }
/////////////////////////////////////////////////////////// // 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); } } }); }
/////////////////////////////////////////////////////////// // disables all states except the default state, and enables // the default state for the controller and the current // weapon and shooter /////////////////////////////////////////////////////////// public void ResetState() { if (Controller != null) { Controller.ResetState(); } if (Camera != null) { Camera.ResetState(); if (CurrentWeapon != null) { CurrentWeapon.ResetState(); } if (CurrentShooter != null) { CurrentShooter.ResetState(); } } }
/////////////////////////////////////////////////////////// // fire the current weapon when player presses the left // mouse button. this temporarily disables the 'Crouch' and // 'Run' states on the current weapon /////////////////////////////////////////////////////////// protected void InputFire() { // fire button pressed / held down if (Input.GetMouseButton(0)) { if (CurrentWeapon != null) { // if firing while crouching or running, point the gun // straight forward if (CurrentWeapon.StateEnabled("Crouch")) { CurrentWeapon.SetState("Crouch", false); } if (CurrentWeapon.StateEnabled("Run")) { CurrentWeapon.SetState("Run", false); } // fire if we have a shooter component and the mouse cursor // is not visible if (CurrentShooter != null && LockCursor) { CurrentShooter.Fire(); } } } // fire button released if (Input.GetMouseButtonUp(0)) { // schedule to reenable 'Crouch' and / or 'Run' in half a second ReenableWeaponStatesIn(0.5f); // disregard refire delay when firing with no ammo if (CurrentShooter != null) { if (CurrentShooter.AmmoCount == 0) { CurrentShooter.NextAllowedFireTime = Time.time; } } } }
/// <summary> /// helper method to refresh all default states. for use /// after applying a preset to a vp_Component via script /// </summary> public void RefreshDefaultState() { if (Controller != null) { Controller.RefreshDefaultState(); } if (Camera != null) { Camera.RefreshDefaultState(); if (WeaponHandler.CurrentWeapon != null) { WeaponHandler.CurrentWeapon.RefreshDefaultState(); } if (CurrentShooter != null) { CurrentShooter.RefreshDefaultState(); } } }
/////////////////////////////////////////////////////////// // this method is called to reset various camera settings, // typically after creating or loading a camera /////////////////////////////////////////////////////////// public override void Refresh() { if (!Application.isPlaying) { return; } m_PositionSpring.Stiffness = new Vector3(PositionSpringStiffness, PositionSpringStiffness, PositionSpringStiffness); m_PositionSpring.Damping = Vector3.one - new Vector3(PositionSpringDamping, PositionSpringDamping, PositionSpringDamping); m_PositionSpring2.Stiffness = new Vector3(PositionSpring2Stiffness, PositionSpring2Stiffness, PositionSpring2Stiffness); m_PositionSpring2.Damping = Vector3.one - new Vector3(PositionSpring2Damping, PositionSpring2Damping, PositionSpring2Damping); m_RotationSpring.Stiffness = new Vector3(RotationSpringStiffness, RotationSpringStiffness, RotationSpringStiffness); m_RotationSpring.Damping = Vector3.one - new Vector3(RotationSpringDamping, RotationSpringDamping, RotationSpringDamping); m_PositionSpring.MinState.y = PositionGroundLimit; m_PositionSpring.RestState = PositionOffset; m_PositionSpring2.MinState.y = (-PositionOffset.y) + PositionGroundLimit; // we don't force a position offset for position spring 2 // BobStepCallback = null; Zoom(); if (CurrentWeapon != null) { CurrentWeapon.Refresh(); } if (CurrentShooter != null) { CurrentShooter.Refresh(); } }
/////////////////////////////////////////////////////////// // sets a state on the controller, camera, current weapon // and current shooter. NOTE: SetState does not update // currently disabled weapons or shooters /////////////////////////////////////////////////////////// public void SetState(string state, bool isEnabled) { if (Controller != null) { Controller.SetState(state, isEnabled); } if (Camera != null) { Camera.SetState(state, isEnabled); if (CurrentWeapon != null) { CurrentWeapon.SetState(state, isEnabled); } if (CurrentShooter != null) { CurrentShooter.SetState(state, isEnabled); } } }
/// <summary> /// helper method to reset all states. for use after applying /// a preset to a vp_Component via script /// </summary> public void ResetState() { if (Controller != null) { Controller.ResetState(); } if (Camera != null) { Camera.ResetState(); if (WeaponHandler.CurrentWeapon != null) { WeaponHandler.CurrentWeapon.ResetState(); } if (CurrentShooter != null) { CurrentShooter.ResetState(); } } if (Input != null) { Input.ResetState(); } }
/////////////////////////////////////////////////////////// // 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(); } }); }); }