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.

118 lines
2.8 KiB

  1. using UnityEngine;
  2. using System.Collections;
  3. namespace OVR
  4. {
  5. /*
  6. -----------------------
  7. AmbienceEmitter()
  8. -----------------------
  9. */
  10. public class AmbienceEmitter : MonoBehaviour {
  11. public SoundFXRef[] ambientSounds = new SoundFXRef[0];
  12. public bool autoActivate = true;
  13. [Tooltip("Automatically play the sound randomly again when checked. Should be OFF for looping sounds")]
  14. public bool autoRetrigger = true;
  15. [MinMax( 2.0f, 4.0f, 0.1f, 10.0f )]
  16. public Vector2 randomRetriggerDelaySecs = new Vector2( 2.0f, 4.0f );
  17. [Tooltip( "If defined, the sounds will randomly play from these transform positions, otherwise the sound will play from this transform" )]
  18. public Transform[] playPositions = new Transform[0];
  19. private bool activated = false;
  20. private int playingIdx = -1;
  21. private float nextPlayTime = 0.0f;
  22. private float fadeTime = 0.25f;
  23. private int lastPosIdx = -1;
  24. /*
  25. -----------------------
  26. Awake()
  27. -----------------------
  28. */
  29. void Awake() {
  30. if ( autoActivate ) {
  31. activated = true;
  32. nextPlayTime = Time.time + Random.Range( randomRetriggerDelaySecs.x, randomRetriggerDelaySecs.y );
  33. }
  34. // verify all the play positions are valid
  35. foreach ( Transform t in playPositions ) {
  36. if ( t == null ) {
  37. Debug.LogWarning( "[AmbienceEmitter] Invalid play positions in " + name );
  38. playPositions = new Transform[0];
  39. break;
  40. }
  41. }
  42. }
  43. /*
  44. -----------------------
  45. Update()
  46. -----------------------
  47. */
  48. void Update() {
  49. if ( activated ) {
  50. if ( ( playingIdx == -1 ) || autoRetrigger ) {
  51. if ( Time.time >= nextPlayTime ) {
  52. Play();
  53. if ( !autoRetrigger ) {
  54. activated = false;
  55. }
  56. }
  57. }
  58. }
  59. }
  60. /*
  61. -----------------------
  62. OnTriggerEnter()
  63. -----------------------
  64. */
  65. public void OnTriggerEnter( Collider col ) {
  66. activated = !activated;
  67. }
  68. /*
  69. -----------------------
  70. Play()
  71. -----------------------
  72. */
  73. public void Play() {
  74. Transform transformToPlayFrom = transform;
  75. if ( playPositions.Length > 0 ) {
  76. int idx = Random.Range( 0, playPositions.Length );
  77. while ( ( playPositions.Length > 1 ) && ( idx == lastPosIdx ) ) {
  78. idx = Random.Range( 0, playPositions.Length );
  79. }
  80. transformToPlayFrom = playPositions[idx];
  81. lastPosIdx = idx;
  82. }
  83. playingIdx = ambientSounds[Random.Range(0, ambientSounds.Length)].PlaySoundAt( transformToPlayFrom.position );
  84. if ( playingIdx != -1 ) {
  85. AudioManager.FadeInSound( playingIdx, fadeTime );
  86. nextPlayTime = Time.time + Random.Range( randomRetriggerDelaySecs.x, randomRetriggerDelaySecs.y );
  87. }
  88. }
  89. /*
  90. -----------------------
  91. EnableEmitter()
  92. -----------------------
  93. */
  94. public void EnableEmitter( bool enable ) {
  95. activated = enable;
  96. if ( enable ) {
  97. Play();
  98. } else {
  99. if ( playingIdx != -1 ) {
  100. AudioManager.FadeOutSound( playingIdx, fadeTime );
  101. }
  102. }
  103. }
  104. }
  105. } // namespace OVR