-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathLevelModuleWaveMusic.cs
109 lines (93 loc) · 3.59 KB
/
LevelModuleWaveMusic.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
using System.Collections;
using ThunderRoad;
using UnityEngine;
using UnityEngine.Events;
namespace TOR {
public class LevelModuleWaveMusic : LevelModule {
public string musicWavePath;
AudioContainer musicWaveAsset;
Coroutine observe;
GameObject obj;
AudioSource audioSource;
public string dynamicMusic = "";
public override IEnumerator OnLoadCoroutine() {
if (GlobalSettings.TORSoundtrack) {
yield return Catalog.LoadAssetCoroutine(musicWavePath, delegate (AudioContainer value) {
musicWaveAsset = value;
obj = new GameObject("TOR_MusicPlayer");
audioSource = obj.AddComponent<AudioSource>();
audioSource.outputAudioMixerGroup = ThunderRoadSettings.GetAudioMixerGroup(AudioMixerName.Music);
audioSource.spatialBlend = 0;
audioSource.playOnAwake = false;
observe = level.StartCoroutine(Observe());
}, "ModuleWaveMusic");
} else {
if (!Catalog.gameData.useDynamicMusic) yield break;
if (level.options.TryGetValue(LevelOption.DynamicMusicId.Get(), out string musicOverride)) {
dynamicMusic = musicOverride;
}
if (!string.IsNullOrEmpty(dynamicMusic) && ThunderBehaviourSingleton<MusicManager>.HasInstance) {
MusicManager musicManager = ThunderBehaviourSingleton<MusicManager>.Instance;
musicManager.UnLoadMusic();
yield return musicManager.LoadMusics(dynamicMusic);
musicManager.Volume = 1f;
musicManager = null;
}
}
yield break;
}
IEnumerator Observe() {
var canExit = false;
while (true) {
yield return Utils.waitSeconds_01;
foreach (var waveSpawner in WaveSpawner.instances) {
var musicSelector = waveSpawner.gameObject.AddComponent<MusicSelector>();
musicSelector.musicWaveAsset = musicWaveAsset;
musicSelector.audioSource = audioSource;
musicSelector.waveSpawner = waveSpawner;
musicSelector.Setup();
canExit = true;
}
if (canExit) {
level.StopCoroutine(observe);
yield break;
}
}
}
public override void OnUnload() {
base.OnUnload();
if (ThunderBehaviourSingleton<MusicManager>.HasInstance) {
ThunderBehaviourSingleton<MusicManager>.Instance.UnLoadMusic();
}
if (musicWaveAsset) {
Catalog.ReleaseAsset(musicWaveAsset);
}
}
public class MusicSelector : ThunderBehaviour {
public override ManagedLoops EnabledManagedLoops => ManagedLoops.Update;
public WaveSpawner waveSpawner;
public AudioSource audioSource;
public AudioContainer musicWaveAsset;
public void Setup() {
waveSpawner.OnWaveBeginEvent.AddListener(new UnityAction(OnWaveBeginEvent));
waveSpawner.OnWaveAnyEndEvent.AddListener(new UnityAction(OnWaveAnyEndEvent));
}
void OnWaveBeginEvent() {
if (musicWaveAsset && audioSource) {
audioSource.clip = musicWaveAsset.GetRandomAudioClip();
audioSource.loop = false;
audioSource.Play();
}
}
void OnWaveAnyEndEvent() {
audioSource?.Stop();
}
protected override void ManagedUpdate() {
if (waveSpawner.isRunning && musicWaveAsset && audioSource && !audioSource.isPlaying) {
audioSource.clip = musicWaveAsset.GetRandomAudioClip();
audioSource.Play();
}
}
}
}
}