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.

272 lines
6.0 KiB

  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. public class AudioController : MonoBehaviour
  5. {
  6. public Transform objectA;
  7. public Transform objectB;
  8. public AudioSource[] bgmSources;
  9. public AudioSource[] ambSources;
  10. public AudioSource[] sfxSources;
  11. public AudioClip[] bgmClips;
  12. public AudioClip[] ambClips;
  13. public AudioClip[] sfxClips;
  14. void Awake()
  15. {
  16. NotificationServer.register("play bgm", playBgm);
  17. NotificationServer.register("play amb", playAmb);
  18. NotificationServer.register("play sfx", playSfx);
  19. NotificationServer.register("fade bgm", fadeBgm);
  20. NotificationServer.register("fade amb", fadeAmb);
  21. NotificationServer.register("statechange Searchlight", searchlightStateChanged);
  22. }
  23. void Start()
  24. {
  25. foreach (AudioSource s in bgmSources)
  26. s.volume = 0f;
  27. foreach (AudioSource s in ambSources)
  28. s.volume = 0f;
  29. foreach (AudioSource s in sfxSources)
  30. s.volume = 0f;
  31. NotificationServer.notify("play amb", "oceanAmbience:0.4");
  32. }
  33. // void Update()
  34. // {
  35. // doMusicMix();
  36. // }
  37. public void doMusicMix()
  38. {
  39. NotificationServer.notify("play bgm", "tense:0.75");
  40. // float distance = Vector3.Distance(objectA.position, objectB.position);
  41. // Debug.LogWarning(distance);
  42. // distance = Mathf.Clamp(1000f / distance, 0f, 1f);
  43. // float tenseVol = 0.75f * distance;
  44. // float spottedVol = 0.5f * (1f-distance);
  45. // NotificationServer.notify("play bgm", "tense:"+tenseVol+"/spotted"+spottedVol);
  46. }
  47. public void searchlightStateChanged()
  48. {
  49. if (Searchlight.state == Searchlight.SearchState.Chasing)
  50. {
  51. NotificationServer.notify("play sfx", "shipSpotted:0.5");
  52. // NotificationServer.notify("play sfx", "spottedDialogue");
  53. NotificationServer.notify("play bgm", "attack:9");
  54. }
  55. else
  56. {
  57. doMusicMix();
  58. }
  59. }
  60. public void playAmb(object audioNameObj)
  61. {
  62. string audioName = audioNameObj as string;
  63. if (audioName == null || audioName == "")
  64. return;
  65. string[] tokens = audioName.ToLower().Split(':');
  66. audioName = tokens[0];
  67. float volume = 1f;
  68. if (tokens.Length > 1)
  69. float.TryParse(tokens[1], out volume);
  70. AudioClip clip = null;
  71. foreach (AudioClip c in ambClips)
  72. {
  73. if (c.name.ToLower() == audioName)
  74. {
  75. clip = c;
  76. break;
  77. }
  78. }
  79. if (clip == null)
  80. return;
  81. bool foundEmpty = false;
  82. foreach (AudioSource s in ambSources)
  83. {
  84. if (s.isPlaying)
  85. {
  86. LeanTween.cancel(s.gameObject);
  87. LeanTween.value(s.gameObject, s.volume, 0f, 1f).setOnUpdate((float val)=>{
  88. s.volume = val;
  89. }).setOnComplete(()=>{
  90. s.Stop();
  91. s.clip = null;
  92. }).setIgnoreTimeScale(true);
  93. }
  94. else if (!foundEmpty)
  95. {
  96. LeanTween.cancel(s.gameObject);
  97. foundEmpty = true;
  98. s.clip = clip;
  99. s.Play();
  100. LeanTween.value(s.gameObject, s.volume, volume, 1f).setOnUpdate((float val)=>{
  101. s.volume = val;
  102. }).setIgnoreTimeScale(true);
  103. }
  104. }
  105. }
  106. public void playSfx(object audioNameObj)
  107. {
  108. string audioName = audioNameObj as string;
  109. if (audioName == null || audioName == "")
  110. return;
  111. string[] tokens = audioName.ToLower().Split(':');
  112. audioName = tokens[0];
  113. float volume = 1f;
  114. if (tokens.Length > 1)
  115. float.TryParse(tokens[1], out volume);
  116. AudioClip clip = null;
  117. foreach (AudioClip c in sfxClips)
  118. {
  119. if (c.name.ToLower() == audioName)
  120. {
  121. clip = c;
  122. break;
  123. }
  124. }
  125. if (clip == null)
  126. return;
  127. foreach (AudioSource s in sfxSources)
  128. {
  129. if (!s.isPlaying)
  130. {
  131. LeanTween.cancel(s.gameObject);
  132. s.clip = clip;
  133. s.volume = volume;
  134. s.Play();
  135. }
  136. }
  137. }
  138. public void playBgm(object audioNameObj)
  139. {
  140. string audioNames = audioNameObj as string;
  141. if (audioNames == null || audioNames == "")
  142. return;
  143. audioNames = audioNames.ToLower();
  144. string[] tokens = audioNames.Split('/');
  145. if (tokens.Length > 1)
  146. {
  147. foreach (string t in tokens)
  148. playBgm(t.Trim());
  149. return;
  150. }
  151. string[] tokens2 = audioNames.Split(':');
  152. string audioName = tokens2[0];
  153. float volume = 1f;
  154. if (tokens2.Length > 1)
  155. float.TryParse(tokens2[1], out volume);
  156. AudioClip clip = null;
  157. foreach (AudioClip c in bgmClips)
  158. {
  159. if (c.name.ToLower() == audioName)
  160. {
  161. clip = c;
  162. break;
  163. }
  164. }
  165. if (clip == null)
  166. return;
  167. AudioSource foundEmpty = null;
  168. int timeSamples = 0;
  169. foreach (AudioSource s in bgmSources)
  170. {
  171. if (s.clip != null && s.clip == clip)
  172. {
  173. LeanTween.cancel(s.gameObject);
  174. foundEmpty = s;
  175. }
  176. else if (s.isPlaying)
  177. {
  178. timeSamples = s.timeSamples;
  179. LeanTween.cancel(s.gameObject);
  180. LeanTween.value(s.gameObject, s.volume, 0f, 1f).setOnUpdate((float val)=>{
  181. s.volume = val;
  182. }).setOnComplete(()=>{
  183. s.Stop();
  184. s.clip = null;
  185. }).setIgnoreTimeScale(true);
  186. }
  187. else if (foundEmpty == null)
  188. {
  189. LeanTween.cancel(s.gameObject);
  190. foundEmpty = s;
  191. }
  192. }
  193. if (foundEmpty != null && !foundEmpty.isPlaying)
  194. {
  195. LeanTween.cancel(foundEmpty.gameObject);
  196. foundEmpty.clip = clip;
  197. foundEmpty.timeSamples = timeSamples;
  198. foundEmpty.Play();
  199. LeanTween.value(foundEmpty.gameObject, foundEmpty.volume, volume, 1f).setOnUpdate((float val)=>{
  200. foundEmpty.volume = val;
  201. }).setIgnoreTimeScale(true);
  202. }
  203. else if (foundEmpty != null && foundEmpty.isPlaying)
  204. {
  205. LeanTween.cancel(foundEmpty.gameObject);
  206. LeanTween.value(foundEmpty.gameObject, foundEmpty.volume, volume, 1f).setOnUpdate((float val)=>{
  207. foundEmpty.volume = val;
  208. }).setIgnoreTimeScale(true);
  209. }
  210. }
  211. public void fadeBgm()
  212. {
  213. foreach (AudioSource s in bgmSources)
  214. {
  215. if (s.isPlaying)
  216. {
  217. LeanTween.cancel(s.gameObject);
  218. LeanTween.value(s.gameObject, s.volume, 0f, 1f).setOnUpdate((float val)=>{
  219. s.volume = val;
  220. }).setOnComplete(()=>{
  221. s.Stop();
  222. s.clip = null;
  223. }).setIgnoreTimeScale(true);
  224. }
  225. }
  226. }
  227. public void fadeAmb()
  228. {
  229. foreach (AudioSource s in ambSources)
  230. {
  231. if (s.isPlaying)
  232. {
  233. LeanTween.cancel(s.gameObject);
  234. LeanTween.value(s.gameObject, s.volume, 0f, 1f).setOnUpdate((float val)=>{
  235. s.volume = val;
  236. }).setOnComplete(()=>{
  237. s.Stop();
  238. s.clip = null;
  239. }).setIgnoreTimeScale(true);
  240. }
  241. }
  242. }
  243. }