Guest User

Untitled

a guest
May 30th, 2017
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.33 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3.  
  4. namespace SpartanFist
  5. {
  6. public class TwiddlerTimedReset : Twiddler
  7. {
  8. [Tooltip("The length of time before the twiddler will set the switch back to its original value")]
  9. public float resetTime = 0.0f;
  10.  
  11. [Header("References"), Tooltip("(optional) the sound source for the looping tick-tock sounds")]
  12. public AudioSource tickTockAudioSource;
  13.  
  14. [Header("Sounds")]
  15. public AudioClip tickTockSound;
  16. public AudioClip fastTickTockSound;
  17.  
  18. protected bool lastSwitchState = false;
  19. protected bool hasLastState = false;
  20.  
  21. protected float timeUntilReset = 0.0f;
  22.  
  23. protected const float FastTickTockPercentRemaining = 0.25f;
  24.  
  25. #region Standard Functions
  26. protected void Update()
  27. {
  28. if (toTwiddle == null) { return; }
  29.  
  30. // Every time the switch changes state, we stop whatever we're doing, and see if we need to start a reset countdown
  31. if (hasLastState && (lastSwitchState != toTwiddle.SwitchState))
  32. {
  33. ResetTickTock();
  34.  
  35. if (toTwiddle.SwitchState && onToOff) { timeUntilReset = resetTime; }
  36. if (!toTwiddle.SwitchState && offToOn) { timeUntilReset = resetTime; }
  37. }
  38. lastSwitchState = toTwiddle.SwitchState; hasLastState = true;
  39.  
  40. // Now, we just count down to a reset (since apparently we're allowed to, if we got this far)
  41. if (timeUntilReset > 0.0f)
  42. {
  43. float deltaTime = PhysicsManager.GetDeltaTimeFor(gameObject);
  44. timeUntilReset -= deltaTime;
  45.  
  46. // If we've finished counting down, reset!
  47. if (timeUntilReset <= 0.0f)
  48. {
  49. ResetTickTock();
  50.  
  51. Twiddle(!toTwiddle.SwitchState);
  52. }
  53. // If we're not resetting this frame, then make sure we're playing the right audio
  54. else if (tickTockAudioSource != null)
  55. {
  56. float percentRemaining = timeUntilReset / resetTime;
  57.  
  58. AudioClip desiredAudio = tickTockSound;
  59. if (percentRemaining <= FastTickTockPercentRemaining) { desiredAudio = fastTickTockSound; }
  60.  
  61. if (!tickTockAudioSource.isPlaying || (tickTockAudioSource.clip != desiredAudio))
  62. {
  63. tickTockAudioSource.Stop();
  64. tickTockAudioSource.clip = desiredAudio;
  65. tickTockAudioSource.Play();
  66. }
  67. }
  68. }
  69. }
  70. #endregion
  71.  
  72. protected void ResetTickTock()
  73. {
  74. timeUntilReset = 0.0f;
  75. if (tickTockAudioSource.isPlaying) { tickTockAudioSource.Stop(); }
  76. }
  77. }
  78. }
Advertisement
Add Comment
Please, Sign In to add comment