BlueGit

blue-star ggj-2020

Files

ggj-2020 / Assets / Oculus / AudioManager / Scripts / Audio / AmbienceEmitter.cs

AmbienceEmitter.cs

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