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.

277 lines
6.1 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", "spottedDialogue:0.9");
  52. NotificationServer.notify("play sfx", "shipSpotted:0.5");
  53. // NotificationServer.notify("play sfx", "spottedDialogue");
  54. NotificationServer.notify("play bgm", "attack:9");
  55. }
  56. else
  57. {
  58. doMusicMix();
  59. }
  60. }
  61. public void playAmb(object audioNameObj)
  62. {
  63. string audioName = audioNameObj as string;
  64. if (audioName == null || audioName == "")
  65. return;
  66. string[] tokens = audioName.ToLower().Split(':');
  67. audioName = tokens[0];
  68. float volume = 1f;
  69. if (tokens.Length > 1)
  70. float.TryParse(tokens[1], out volume);
  71. AudioClip clip = null;
  72. foreach (AudioClip c in ambClips)
  73. {
  74. if (c.name.ToLower() == audioName)
  75. {
  76. clip = c;
  77. break;
  78. }
  79. }
  80. if (clip == null)
  81. return;
  82. bool foundEmpty = false;
  83. foreach (AudioSource s in ambSources)
  84. {
  85. if (s.isPlaying)
  86. {
  87. LeanTween.cancel(s.gameObject);
  88. LeanTween.value(s.gameObject, s.volume, 0f, 1f).setOnUpdate((float val)=>{
  89. s.volume = val;
  90. }).setOnComplete(()=>{
  91. s.Stop();
  92. s.clip = null;
  93. }).setIgnoreTimeScale(true);
  94. }
  95. else if (!foundEmpty)
  96. {
  97. LeanTween.cancel(s.gameObject);
  98. foundEmpty = true;
  99. s.clip = clip;
  100. s.Play();
  101. LeanTween.value(s.gameObject, s.volume, volume, 1f).setOnUpdate((float val)=>{
  102. s.volume = val;
  103. }).setIgnoreTimeScale(true);
  104. }
  105. }
  106. }
  107. public void playSfx(object audioNameObj)
  108. {
  109. string audioName = audioNameObj as string;
  110. if (audioName == null || audioName == "")
  111. return;
  112. string[] tokens = audioName.ToLower().Split(':');
  113. audioName = tokens[0];
  114. float volume = 1f;
  115. if (tokens.Length > 1)
  116. float.TryParse(tokens[1], out volume);
  117. AudioClip clip = null;
  118. foreach (AudioClip c in sfxClips)
  119. {
  120. if (c.name.ToLower() == audioName)
  121. {
  122. clip = c;
  123. break;
  124. }
  125. }
  126. if (clip == null)
  127. return;
  128. foreach (AudioSource s in sfxSources)
  129. {
  130. if (!s.isPlaying)
  131. {
  132. LeanTween.cancel(s.gameObject);
  133. s.clip = clip;
  134. s.volume = volume;
  135. s.Play();
  136. return;
  137. }
  138. }
  139. }
  140. public void playBgm(object audioNameObj)
  141. {
  142. string audioNames = audioNameObj as string;
  143. if (audioNames == null || audioNames == "")
  144. return;
  145. audioNames = audioNames.ToLower();
  146. string[] tokens = audioNames.Split('/');
  147. if (tokens.Length > 1)
  148. {
  149. foreach (string t in tokens)
  150. playBgm(t.Trim());
  151. return;
  152. }
  153. string[] tokens2 = audioNames.Split(':');
  154. string audioName = tokens2[0];
  155. float volume = 1f;
  156. if (tokens2.Length > 1)
  157. float.TryParse(tokens2[1], out volume);
  158. AudioClip clip = null;
  159. foreach (AudioClip c in bgmClips)
  160. {
  161. if (c.name.ToLower() == audioName)
  162. {
  163. clip = c;
  164. break;
  165. }
  166. }
  167. if (clip == null)
  168. return;
  169. AudioSource foundEmpty = null;
  170. int timeSamples = 0;
  171. foreach (AudioSource s in bgmSources)
  172. {
  173. if (s.clip != null && s.clip == clip)
  174. {
  175. LeanTween.cancel(s.gameObject);
  176. foundEmpty = s;
  177. }
  178. else if (s.isPlaying)
  179. {
  180. timeSamples = s.timeSamples;
  181. LeanTween.cancel(s.gameObject);
  182. LeanTween.value(s.gameObject, s.volume, 0f, 1f).setOnUpdate((float val)=>{
  183. s.volume = val;
  184. }).setOnComplete(()=>{
  185. s.Stop();
  186. s.clip = null;
  187. }).setIgnoreTimeScale(true);
  188. }
  189. else if (foundEmpty == null)
  190. {
  191. LeanTween.cancel(s.gameObject);
  192. foundEmpty = s;
  193. }
  194. }
  195. if (foundEmpty != null && !foundEmpty.isPlaying)
  196. {
  197. LeanTween.cancel(foundEmpty.gameObject);
  198. foundEmpty.clip = clip;
  199. if (timeSamples < foundEmpty.clip.samples)
  200. foundEmpty.timeSamples = timeSamples;
  201. else
  202. foundEmpty.timeSamples = 0;
  203. foundEmpty.Play();
  204. LeanTween.value(foundEmpty.gameObject, foundEmpty.volume, volume, 1f).setOnUpdate((float val)=>{
  205. foundEmpty.volume = val;
  206. }).setIgnoreTimeScale(true);
  207. }
  208. else if (foundEmpty != null && foundEmpty.isPlaying)
  209. {
  210. LeanTween.cancel(foundEmpty.gameObject);
  211. LeanTween.value(foundEmpty.gameObject, foundEmpty.volume, volume, 1f).setOnUpdate((float val)=>{
  212. foundEmpty.volume = val;
  213. }).setIgnoreTimeScale(true);
  214. }
  215. }
  216. public void fadeBgm()
  217. {
  218. foreach (AudioSource s in bgmSources)
  219. {
  220. if (s.isPlaying)
  221. {
  222. LeanTween.cancel(s.gameObject);
  223. LeanTween.value(s.gameObject, s.volume, 0f, 1f).setOnUpdate((float val)=>{
  224. s.volume = val;
  225. }).setOnComplete(()=>{
  226. s.Stop();
  227. s.clip = null;
  228. }).setIgnoreTimeScale(true);
  229. }
  230. }
  231. }
  232. public void fadeAmb()
  233. {
  234. foreach (AudioSource s in ambSources)
  235. {
  236. if (s.isPlaying)
  237. {
  238. LeanTween.cancel(s.gameObject);
  239. LeanTween.value(s.gameObject, s.volume, 0f, 1f).setOnUpdate((float val)=>{
  240. s.volume = val;
  241. }).setOnComplete(()=>{
  242. s.Stop();
  243. s.clip = null;
  244. }).setIgnoreTimeScale(true);
  245. }
  246. }
  247. }
  248. }