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.

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