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.

287 lines
6.4 KiB

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