-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathGameMusic.cs
232 lines (192 loc) · 5.96 KB
/
GameMusic.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
using UnityEngine;
using System.Collections;
public class GameMusic : MonoBehaviour
{
// Reference to the GameMusic instance (singleton)
public static GameMusic instance;
// Audio sources for playing music
public AudioSource music1;
public AudioSource music2;
// AudioClip for the death music
public AudioClip deathMusic;
// Speed at which music fades
public float musicFadeSpeed = 5;
// Time at which music will return to normal if no danger is present
public float dangerMusicLength = 10;
// Boolean variables for tracking game state
public bool danger = false;
public bool dead = false;
// Maximum volume for the music
public float maxVolume = 1;
// Optional transition audio clip and audio source
public AudioClip optionalTransition;
public AudioSource optionalTransitionAudioSource;
// Countdown for transitioning from danger state to chill state
private float countDown;
private void Awake()
{
// Set the instance reference to this script
instance = this;
// Retrieve the MaxVolume value from PlayerPrefs
maxVolume = PlayerPrefs.GetFloat("MusicVolume");
// Check if optional transition audio is added before proceeding
if (!optionalTransition || !optionalTransitionAudioSource)
return;
// Create an AudioSource component for the optional transition audio if none exists
optionalTransitionAudioSource = gameObject.AddComponent<AudioSource>();
optionalTransitionAudioSource.spatialBlend = 0.0f;
optionalTransitionAudioSource.playOnAwake = false;
optionalTransitionAudioSource.clip = optionalTransition;
}
private void Start()
{
// If music1 is enabled, play the music
if (music1.enabled)
music1.Play();
// Set the volume of Music1 to the MaxVolume value
music1.volume = MaxVolume;
}
public static void Danger()
{
if (instance)
{
instance.DangerState();
}
}
public static void Death()
{
if (instance)
{
instance.DeathState();
}
}
public static void ForceChill()
{
if (instance)
{
instance.countDown = 0;
}
}
// Called when danger is detected in the game
public void DangerState()
{
// Start a countdown
countDown = dangerMusicLength;
// Check if danger is not already active and the player is not dead
if (!danger && !dead)
{
// Play the optional transition audio
if (optionalTransition)
optionalTransitionAudioSource.Play();
// Fade the music to danger state
StartCoroutine(FadeToDanger());
// Set danger to true (game state)
danger = true;
}
}
// Called when the player dies
public void DeathState()
{
if (!dead)
{
// Stop the current music
if (music1.clip)
{
music1.Stop();
music2.Stop();
}
// Set the volume and clip for music1 to play the death music
music1.volume = 0.33f;
music1.clip = deathMusic;
// Enable Music1 if its disabled
if (music1.enabled == false)
music1.enabled = true;
// Set the loop property to false and play the death music
music1.loop = false;
music1.Play();
// Set dead to true
dead = true;
}
}
// Set the maximum volume for the music
public static void SetMaxVolume(float x)
{
maxVolume = x;
if (instance)
{
// Adjust the volume of music1 or music2 based on the game state
if (danger)
{
instance.music2.volume = maxVolume;
}
else
{
instance.music1.volume = maxVolume;
}
}
}
// Coroutine for fading the music to the danger state
private IEnumerator FadeToDanger()
{
while (true)
{
// Reduce the volume of Music1 over time
music1.volume -= Time.deltaTime;
// Increase the volume of music2 up to the maxVolume value
if (music2.volume < maxVolume)
{
music2.volume += Time.deltaTime;
}
// Check if music1 volume has reached 0
if (music1.volume == 0)
{
// Start the countdown
StartCoroutine(CountDown());
yield break;
}
yield return null;
}
}
private IEnumerator CountDown()
{
while (true)
{
// Decrease the countdown
countDown--;
// Check if the countdown has reached 0
if (countDown == 0)
{
// Check if the player is not dead
if (!dead)
{
// Fade the music to the chill state
StartCoroutine(FadeToChill());
yield break;
}
}
yield return new WaitForSeconds(1);
}
}
// Coroutine for fading the music to the chill state
private IEnumerator FadeToChill()
{
while (true)
{
// Increase the volume of Music1 up to the MaxVolume value
if (music1.volume < maxVolume)
{
music1.volume += Time.deltaTime;
}
// Reduce the volume of music2 over time
music2.volume -= Time.deltaTime;
// Check if Music2 volume has reached 0
if (music2.volume == 0)
{
// Set danger to false (game state)
danger = false;
yield break;
}
yield return null;
}
}
}