Assignment for RMIT Mixed Reality in 2020
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

114 lines
3.1 KiB

  1. /************************************************************************************
  2. Copyright (c) Facebook Technologies, LLC and its affiliates. All rights reserved.
  3. See SampleFramework license.txt for license terms. Unless required by applicable law
  4. or agreed to in writing, the sample code is provided AS IS WITHOUT WARRANTIES OR
  5. CONDITIONS OF ANY KIND, either express or implied. See the license for specific
  6. language governing permissions and limitations under the license.
  7. ************************************************************************************/
  8. using System.Collections;
  9. using UnityEngine;
  10. using UnityEngine.Assertions;
  11. namespace OculusSampleFramework
  12. {
  13. public class WindmillBladesController : MonoBehaviour
  14. {
  15. private const float MAX_TIME = 1f;
  16. [SerializeField] private AudioSource _audioSource = null;
  17. [SerializeField] private AudioClip _windMillRotationSound = null;
  18. [SerializeField] private AudioClip _windMillStartSound = null;
  19. [SerializeField] private AudioClip _windMillStopSound = null;
  20. public bool IsMoving { get; private set; }
  21. private float _currentSpeed = 0f;
  22. private Coroutine _lerpSpeedCoroutine;
  23. private Coroutine _audioChangeCr;
  24. private Quaternion _originalRotation;
  25. private float _rotAngle = 0.0f;
  26. private void Start()
  27. {
  28. Assert.IsNotNull(_audioSource);
  29. Assert.IsNotNull(_windMillRotationSound);
  30. Assert.IsNotNull(_windMillStartSound);
  31. Assert.IsNotNull(_windMillStopSound);
  32. _originalRotation = transform.localRotation;
  33. }
  34. private void Update()
  35. {
  36. _rotAngle += _currentSpeed * Time.deltaTime;
  37. if (_rotAngle > 360.0f)
  38. {
  39. _rotAngle = 0.0f;
  40. }
  41. transform.localRotation = _originalRotation * Quaternion.AngleAxis(_rotAngle, Vector3.forward);
  42. }
  43. public void SetMoveState(bool newMoveState, float goalSpeed)
  44. {
  45. IsMoving = newMoveState;
  46. if (_lerpSpeedCoroutine != null)
  47. {
  48. StopCoroutine(_lerpSpeedCoroutine);
  49. }
  50. _lerpSpeedCoroutine = StartCoroutine(LerpToSpeed(goalSpeed));
  51. }
  52. private IEnumerator LerpToSpeed(float goalSpeed)
  53. {
  54. var totalTime = 0f;
  55. var startSpeed = _currentSpeed;
  56. if (_audioChangeCr != null)
  57. {
  58. StopCoroutine(_audioChangeCr);
  59. }
  60. // start up
  61. if (IsMoving)
  62. {
  63. _audioChangeCr = StartCoroutine(PlaySoundDelayed(_windMillStartSound,
  64. _windMillRotationSound, _windMillStartSound.length * 0.95f));
  65. } // stop
  66. else
  67. {
  68. PlaySound(_windMillStopSound);
  69. }
  70. var diffSpeeds = Mathf.Abs(_currentSpeed - goalSpeed);
  71. while (diffSpeeds > Mathf.Epsilon)
  72. {
  73. _currentSpeed = Mathf.Lerp(startSpeed, goalSpeed, totalTime / MAX_TIME);
  74. totalTime += Time.deltaTime;
  75. yield return null;
  76. diffSpeeds = Mathf.Abs(_currentSpeed - goalSpeed);
  77. }
  78. _lerpSpeedCoroutine = null;
  79. }
  80. private IEnumerator PlaySoundDelayed(AudioClip initial, AudioClip clip, float timeDelayAfterInitial)
  81. {
  82. PlaySound(initial, false);
  83. yield return new WaitForSeconds(timeDelayAfterInitial);
  84. PlaySound(clip, true);
  85. }
  86. private void PlaySound(AudioClip clip, bool loop = false)
  87. {
  88. _audioSource.loop = loop;
  89. _audioSource.timeSamples = 0;
  90. _audioSource.clip = clip;
  91. _audioSource.Play();
  92. }
  93. }
  94. }