Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- using System.Collections;
- namespace SpartanFist
- {
- public class TwiddlerTimedReset : Twiddler
- {
- [Tooltip("The length of time before the twiddler will set the switch back to its original value")]
- public float resetTime = 0.0f;
- [Header("References"), Tooltip("(optional) the sound source for the looping tick-tock sounds")]
- public AudioSource tickTockAudioSource;
- [Header("Sounds")]
- public AudioClip tickTockSound;
- public AudioClip fastTickTockSound;
- protected bool lastSwitchState = false;
- protected bool hasLastState = false;
- protected float timeUntilReset = 0.0f;
- protected const float FastTickTockPercentRemaining = 0.25f;
- #region Standard Functions
- protected void Update()
- {
- if (toTwiddle == null) { return; }
- // Every time the switch changes state, we stop whatever we're doing, and see if we need to start a reset countdown
- if (hasLastState && (lastSwitchState != toTwiddle.SwitchState))
- {
- ResetTickTock();
- if (toTwiddle.SwitchState && onToOff) { timeUntilReset = resetTime; }
- if (!toTwiddle.SwitchState && offToOn) { timeUntilReset = resetTime; }
- }
- lastSwitchState = toTwiddle.SwitchState; hasLastState = true;
- // Now, we just count down to a reset (since apparently we're allowed to, if we got this far)
- if (timeUntilReset > 0.0f)
- {
- float deltaTime = PhysicsManager.GetDeltaTimeFor(gameObject);
- timeUntilReset -= deltaTime;
- // If we've finished counting down, reset!
- if (timeUntilReset <= 0.0f)
- {
- ResetTickTock();
- Twiddle(!toTwiddle.SwitchState);
- }
- // If we're not resetting this frame, then make sure we're playing the right audio
- else if (tickTockAudioSource != null)
- {
- float percentRemaining = timeUntilReset / resetTime;
- AudioClip desiredAudio = tickTockSound;
- if (percentRemaining <= FastTickTockPercentRemaining) { desiredAudio = fastTickTockSound; }
- if (!tickTockAudioSource.isPlaying || (tickTockAudioSource.clip != desiredAudio))
- {
- tickTockAudioSource.Stop();
- tickTockAudioSource.clip = desiredAudio;
- tickTockAudioSource.Play();
- }
- }
- }
- }
- #endregion
- protected void ResetTickTock()
- {
- timeUntilReset = 0.0f;
- if (tickTockAudioSource.isPlaying) { tickTockAudioSource.Stop(); }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment