public void Vibrate(VibrationSettings settings, bool addToExternalObject = false) { if (GameManager.instance.CurrentState != GameState.Playing) { return; } //this check was gining a nice null pointer, so I added a null check on _settingsOnGoing, but now there noe more vibration, sorry Victor... //if (_settingsOnGoing != null && (settings.LeftSideVibration >= _settingsOnGoing.LeftSideVibration || settings.RightSideVibration >= _settingsOnGoing.RightSideVibration)) if (_vibrationOnGoing && _settingsOnGoing != null) { if (settings.LeftSideVibration <= _settingsOnGoing.LeftSideVibration || settings.RightSideVibration <= _settingsOnGoing.RightSideVibration) { return; } } StopVibration(); _vibrationCoroutine = StartVibration(settings); if (addToExternalObject) { var newGameObject = new GameObject() { name = _guidIdentifierForExternalObject }; var holder = newGameObject.AddComponent <CoroutineHolder>(); var destroyOnTimer = newGameObject.AddComponent <DestroyOnTimer>(); holder.StartAndKeepCoroutine(_vibrationCoroutine); destroyOnTimer.Timeout = settings.TimeToVibrate + 0.3f; } else { StartCoroutine(_vibrationCoroutine); } }
/// <summary> /// Creates a vibration for a given player. On Windows and Xbox the vibration strength uses a /// linear fall off to reduce the strength over time. On Windows Phone we don't have that control /// so the phone vibrates at the same strength for the duration of the vibration. /// </summary> /// <param name="player">The player whose GamePad should vibrate. On Windows Phone this should be /// PlayerIndex.One.</param> /// <param name="left">The initial strength of the left motor. This should be in the range of 0-1.</param> /// <param name="right">The initial strength of the right motor. This should be in the range of 0-1.</param> /// <param name="duration">The length time (in seconds) to vibrate the GamePad or phone.</param> public static void Vibrate(PlayerIndex player, float left, float right, float duration) { // We don't allow starting new vibrations when the system is paused. if (instance.isPaused) { return; } // Get our settings VibrationSettings settings = instance.vibrations[player]; // Initialize the settings object settings.Left = left; settings.Right = right; settings.Duration = duration; settings.Timer = 0f; // Set our initial vibration for the GamePad. GamePad.SetVibration(player, left, right); #if WINDOWS_PHONE // If we're setting the vibration for player one, we also start the VibrateController if (player == PlayerIndex.One) { Microsoft.Devices.VibrateController.Default.Start(TimeSpan.FromSeconds(duration)); } #endif }
public void Vibrate(VibrationSettings settings, bool addToExternalObject = false) { if (GameManager.instance.CurrentState != GameState.Playing) return; //this check was gining a nice null pointer, so I added a null check on _settingsOnGoing, but now there noe more vibration, sorry Victor... //if (_settingsOnGoing != null && (settings.LeftSideVibration >= _settingsOnGoing.LeftSideVibration || settings.RightSideVibration >= _settingsOnGoing.RightSideVibration)) if (_vibrationOnGoing && _settingsOnGoing != null) { if (settings.LeftSideVibration <= _settingsOnGoing.LeftSideVibration || settings.RightSideVibration <= _settingsOnGoing.RightSideVibration) { return; } } StopVibration(); _vibrationCoroutine = StartVibration(settings); if (addToExternalObject) { var newGameObject = new GameObject() { name = _guidIdentifierForExternalObject }; var holder = newGameObject.AddComponent<CoroutineHolder>(); var destroyOnTimer = newGameObject.AddComponent<DestroyOnTimer>(); holder.StartAndKeepCoroutine(_vibrationCoroutine); destroyOnTimer.Timeout = settings.TimeToVibrate + 0.3f; } else { StartCoroutine(_vibrationCoroutine); } }
public override void Update(GameTime gameTime) { // Do nothing if we're currently paused if (instance.isPaused) { return; } // Get the elapsed frame time. float time = (float)gameTime.ElapsedGameTime.TotalSeconds; // Update all of the vibrations for (PlayerIndex player = PlayerIndex.One; player <= PlayerIndex.Four; player++) { VibrationSettings settings = vibrations[player]; // If the vibration is not complete if (settings.Timer < settings.Duration) { // Update our timer, clamping at the duration settings.Timer = (float)Math.Min(settings.Timer + time, settings.Duration); // Update the vibration UpdateVibration(player, settings); } } }
private IEnumerator StartVibration(VibrationSettings settings, float startTime = 0) { _vibrationOnGoing = true; _settingsOnGoing = settings; _currentTimePassed = startTime; SetVibration(settings.LeftSideVibration, settings.RightSideVibration); while (_currentTimePassed <= settings.TimeToVibrate) { _currentTimePassed += Time.deltaTime; yield return(null); } SetVibration(0, 0); _vibrationOnGoing = false; }
/// <summary> /// Cancels vibrations for all players. /// </summary> public static void CancelAll() { // Iterate all vibrations, setting their durations to 0 and turning off the vibration // of the GamePads. for (PlayerIndex player = PlayerIndex.One; player <= PlayerIndex.Four; player++) { VibrationSettings settings = instance.vibrations[player]; settings.Duration = 0f; GamePad.SetVibration(player, 0, 0); } #if WINDOWS_PHONE // On the phone we also need to stop the VibrateController. Microsoft.Devices.VibrateController.Default.Stop(); #endif }
/// <summary> /// Updates the vibration for a given player. /// </summary> private static void UpdateVibration(PlayerIndex player, VibrationSettings settings) { // If the vibration is at its duration, stop the vibration of the GamePad. if (settings.Timer >= settings.Duration) { GamePad.SetVibration(player, 0, 0); } else { // Compute our progress in a [0, 1] range float progress = settings.Timer / settings.Duration; // Calculate our left and right motor strengths while applying a linear decay over time. float left = settings.Left * (1f - progress); float right = settings.Right * (1f - progress); // Set the vibration of the GamePad. GamePad.SetVibration(player, left, right); } }
/// <summary> /// Resumes all paused vibrations. /// </summary> public static void Resume() { // Unpause the instance instance.isPaused = false; for (PlayerIndex player = PlayerIndex.One; player <= PlayerIndex.Four; player++) { // We call UpdateVibration for all players which will resume our vibrations // from the correct point in our progress. VibrationSettings settings = instance.vibrations[player]; UpdateVibration(player, settings); #if WINDOWS_PHONE // If we're looking at player one and the vibration is not complete, we also // start the VibrateController with the remaining time needed to complete // the original duration. if (player == PlayerIndex.One && settings.Timer < settings.Duration) { Microsoft.Devices.VibrateController.Default.Start(TimeSpan.FromSeconds(settings.Duration - settings.Timer)); } #endif } }
public void AddToMenu(ref bool vibrationEnabled, ref VibrationSettings vibrationSettings, string title) { EditorGUILayout.BeginHorizontal(); vibrationEnabled = EditorGUILayout.Toggle(title, vibrationEnabled); if (vibrationEnabled) { vibrationSettings._averageBothSides = EditorGUILayout.Toggle("Average Vibration", vibrationSettings._averageBothSides); EditorGUILayout.EndHorizontal(); if (vibrationSettings._averageBothSides) { var slider = EditorGUILayout.Slider("Vibration", vibrationSettings._leftSideVibration, 0, 1); vibrationSettings._leftSideVibration = vibrationSettings._rightSideVibration = slider; } else { vibrationSettings._leftSideVibration = EditorGUILayout.Slider("Left Vibration", vibrationSettings._leftSideVibration, 0, 1); vibrationSettings._rightSideVibration = EditorGUILayout.Slider("Right Vibration", vibrationSettings._rightSideVibration, 0, 1); } vibrationSettings._timeToVibrate = EditorGUILayout.FloatField( "Time of Vibration", vibrationSettings._timeToVibrate); } else { EditorGUILayout.EndHorizontal(); } }
public void AddToMenu(ref bool vibrationEnabled, ref VibrationSettings vibrationSettings, string title) { EditorGUILayout.BeginHorizontal(); vibrationEnabled = EditorGUILayout.Toggle(title, vibrationEnabled); if (vibrationEnabled) { vibrationSettings._averageBothSides = EditorGUILayout.Toggle("Average Vibration", vibrationSettings._averageBothSides); EditorGUILayout.EndHorizontal(); if (vibrationSettings._averageBothSides) { var slider = EditorGUILayout.Slider("Vibration", vibrationSettings._leftSideVibration, 0, 1); vibrationSettings._leftSideVibration = vibrationSettings._rightSideVibration = slider; } else { vibrationSettings._leftSideVibration = EditorGUILayout.Slider("Left Vibration", vibrationSettings._leftSideVibration, 0, 1); vibrationSettings._rightSideVibration = EditorGUILayout.Slider("Right Vibration", vibrationSettings._rightSideVibration, 0, 1); } vibrationSettings._timeToVibrate = EditorGUILayout.FloatField( "Time of Vibration", vibrationSettings._timeToVibrate); } else { EditorGUILayout.EndHorizontal(); } }
void Update() { if (GameManager.instance.CurrentState != GameState.Playing && _vibrationOnGoing) { StopCoroutine(_vibrationCoroutine); _vibrationOnGoing = false; StopVibration(); _needToContinueVibration = GameManager.instance.CurrentState == GameState.Paused; } else if (_needToContinueVibration && GameManager.instance.CurrentState == GameState.Playing) { StartVibration(_settingsOnGoing, _currentTimePassed); _needToContinueVibration = false; _settingsOnGoing = null; } }
private IEnumerator StartVibration(VibrationSettings settings, float startTime = 0) { _vibrationOnGoing = true; _settingsOnGoing = settings; _currentTimePassed = startTime; SetVibration(settings.LeftSideVibration, settings.RightSideVibration); while (_currentTimePassed <= settings.TimeToVibrate) { _currentTimePassed += Time.deltaTime; yield return null; } SetVibration(0, 0); _vibrationOnGoing = false; }
/// <summary> /// Updates the vibration for a given player. /// </summary> private static void UpdateVibration(PlayerIndex player, VibrationSettings settings) { // If the vibration is at its duration, stop the vibration of the GamePad. if (settings.Timer >= settings.Duration) { GamePad.SetVibration(player, 0, 0); } else { // Compute our progress in a [0, 1] range float progress = settings.Timer / settings.Duration; // Calculate our left and right motor strengths while applying a linear decay over time. float left = settings.Left * (1f - progress); float right = settings.Right * (1f - progress); // Set the vibration of the GamePad. GamePad.SetVibration(player, left, right); } }