SoundController.ts 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495
  1. import { _decorator, Component, Node, Slider, Button, Sprite, SpriteFrame, ProgressBar, resources, tween, Vec3 } from 'cc';
  2. import { AudioManager } from '../../AudioManager/AudioManager';
  3. import { SaveDataManager } from '../../LevelSystem/SaveDataManager';
  4. import { Audio } from '../../AudioManager/AudioManager';
  5. const { ccclass, property } = _decorator;
  6. /**
  7. * 音频控制器
  8. * 管理音效和音乐的音量控制以及开关状态
  9. */
  10. @ccclass('SoundController')
  11. export class SoundController extends Component {
  12. // 音效控制组件
  13. @property({ type: Slider, tooltip: '音效音量滑动条' })
  14. public soundEffectSlider: Slider = null;
  15. @property({ type: Button, tooltip: '音效开关按钮' })
  16. public soundEffectCheckbox: Button = null;
  17. @property({ type: Node, tooltip: '音效勾选标记节点' })
  18. public soundEffectCheck: Node = null;
  19. @property({ type: ProgressBar, tooltip: '音效进度条' })
  20. public soundEffectProgressBar: ProgressBar = null;
  21. // 音乐控制组件
  22. @property({ type: Slider, tooltip: '音乐音量滑动条' })
  23. public musicSlider: Slider = null;
  24. @property({ type: Button, tooltip: '音乐开关按钮' })
  25. public musicCheckbox: Button = null;
  26. @property({ type: Node, tooltip: '音乐勾选标记节点' })
  27. public musicCheck: Node = null;
  28. @property({ type: ProgressBar, tooltip: '音乐进度条' })
  29. public musicProgressBar: ProgressBar = null;
  30. // 震动控制组件
  31. @property({ type: Button, tooltip: '震动开关按钮' })
  32. public vibrationOnButton: Button = null;
  33. @property({ type: Button, tooltip: '震动关闭按钮' })
  34. public vibrationOffButton: Button = null;
  35. @property({ type: Node, tooltip: '震动滑动节点' })
  36. public vibrationSlideNode: Node = null;
  37. // 音量状态
  38. private soundEffectEnabled: boolean = true;
  39. private musicEnabled: boolean = true;
  40. private savedSoundEffectVolume: number = 0.5;
  41. private savedMusicVolume: number = 0.5;
  42. private vibrationEnabled: boolean = true;
  43. onLoad() {
  44. this.initializeSliders();
  45. this.bindEvents();
  46. this.loadSettings();
  47. }
  48. /**
  49. * 初始化滑动条
  50. */
  51. private initializeSliders() {
  52. if (this.soundEffectSlider) {
  53. this.soundEffectSlider.progress = this.savedSoundEffectVolume;
  54. this.updateProgressBar(this.soundEffectProgressBar, this.soundEffectSlider.progress);
  55. }
  56. if (this.musicSlider) {
  57. this.musicSlider.progress = this.savedMusicVolume;
  58. this.updateProgressBar(this.musicProgressBar, this.musicSlider.progress);
  59. }
  60. }
  61. /**
  62. * 绑定事件
  63. */
  64. private bindEvents() {
  65. // 音效滑动条事件
  66. if (this.soundEffectSlider) {
  67. this.soundEffectSlider.node.on('slide', this.onSoundEffectSliderChange, this);
  68. }
  69. // 音乐滑动条事件
  70. if (this.musicSlider) {
  71. this.musicSlider.node.on('slide', this.onMusicSliderChange, this);
  72. }
  73. // 音效开关按钮事件
  74. if (this.soundEffectCheckbox) {
  75. this.soundEffectCheckbox.node.on(Button.EventType.CLICK, this.onSoundEffectCheckboxClick, this);
  76. }
  77. // 音乐开关按钮事件
  78. if (this.musicCheckbox) {
  79. this.musicCheckbox.node.on(Button.EventType.CLICK, this.onMusicCheckboxClick, this);
  80. }
  81. // 震动开关按钮事件
  82. if (this.vibrationOnButton) {
  83. this.vibrationOnButton.node.on(Button.EventType.CLICK, this.onVibrationOnClick, this);
  84. }
  85. if (this.vibrationOffButton) {
  86. this.vibrationOffButton.node.on(Button.EventType.CLICK, this.onVibrationOffClick, this);
  87. }
  88. }
  89. /**
  90. * 音效滑动条变化事件
  91. */
  92. private onSoundEffectSliderChange(slider: Slider) {
  93. if (this.soundEffectEnabled) {
  94. this.savedSoundEffectVolume = slider.progress;
  95. this.updateProgressBar(this.soundEffectProgressBar, slider.progress);
  96. // TODO: 应用音效音量到音频系统
  97. this.applySoundEffectVolume(slider.progress);
  98. }
  99. }
  100. /**
  101. * 音乐滑动条变化事件
  102. */
  103. private onMusicSliderChange(slider: Slider) {
  104. if (this.musicEnabled) {
  105. this.savedMusicVolume = slider.progress;
  106. this.updateProgressBar(this.musicProgressBar, slider.progress);
  107. // TODO: 应用音乐音量到音频系统
  108. this.applyMusicVolume(slider.progress);
  109. }
  110. }
  111. /**
  112. * 音效开关按钮点击事件
  113. */
  114. private onSoundEffectCheckboxClick() {
  115. Audio.playUISound('data/弹球音效/ui play');
  116. this.soundEffectEnabled = !this.soundEffectEnabled;
  117. if (this.soundEffectEnabled) {
  118. // 开启音效:恢复之前保存的音量
  119. this.soundEffectSlider.progress = this.savedSoundEffectVolume;
  120. this.updateProgressBar(this.soundEffectProgressBar, this.savedSoundEffectVolume);
  121. this.applySoundEffectVolume(this.savedSoundEffectVolume);
  122. } else {
  123. // 关闭音效:保存当前音量并设置为0
  124. this.savedSoundEffectVolume = this.soundEffectSlider.progress;
  125. this.soundEffectSlider.progress = 0;
  126. this.updateProgressBar(this.soundEffectProgressBar, 0);
  127. this.applySoundEffectVolume(0);
  128. }
  129. // 更新勾选标记显示
  130. if (this.soundEffectCheck) {
  131. this.soundEffectCheck.active = this.soundEffectEnabled;
  132. }
  133. this.saveSettings();
  134. }
  135. /**
  136. * 音乐开关按钮点击事件
  137. */
  138. private onMusicCheckboxClick() {
  139. Audio.playUISound('data/弹球音效/ui play');
  140. this.musicEnabled = !this.musicEnabled;
  141. if (this.musicEnabled) {
  142. // 开启音乐:恢复之前保存的音量
  143. this.musicSlider.progress = this.savedMusicVolume;
  144. this.updateProgressBar(this.musicProgressBar, this.savedMusicVolume);
  145. this.applyMusicVolume(this.savedMusicVolume);
  146. } else {
  147. // 关闭音乐:保存当前音量并设置为0
  148. this.savedMusicVolume = this.musicSlider.progress;
  149. this.musicSlider.progress = 0;
  150. this.updateProgressBar(this.musicProgressBar, 0);
  151. this.applyMusicVolume(0);
  152. }
  153. // 更新勾选标记显示
  154. if (this.musicCheck) {
  155. this.musicCheck.active = this.musicEnabled;
  156. }
  157. this.saveSettings();
  158. }
  159. /**
  160. * 震动开启按钮点击事件
  161. */
  162. private onVibrationOnClick() {
  163. Audio.playUISound('data/弹球音效/ui play');
  164. this.vibrationEnabled = true;
  165. this.updateVibrationSlideButton();
  166. this.saveVibrationSetting();
  167. // 开启音效和音乐
  168. this.enableSoundEffectAndMusic();
  169. }
  170. /**
  171. * 震动关闭按钮点击事件
  172. */
  173. private onVibrationOffClick() {
  174. Audio.playUISound('data/弹球音效/ui play');
  175. this.vibrationEnabled = false;
  176. this.updateVibrationSlideButton();
  177. this.saveVibrationSetting();
  178. // 关闭音效和音乐
  179. this.disableSoundEffectAndMusic();
  180. }
  181. /**
  182. * 更新震动滑动按钮位置
  183. */
  184. private updateVibrationSlideButton() {
  185. if (!this.vibrationSlideNode || !this.vibrationOnButton || !this.vibrationOffButton) {
  186. return;
  187. }
  188. // 获取目标按钮的位置
  189. const targetButton = this.vibrationEnabled ? this.vibrationOnButton : this.vibrationOffButton;
  190. const targetPosition = targetButton.node.position.clone();
  191. // 使用缓动动画移动滑动节点
  192. tween(this.vibrationSlideNode)
  193. .to(0.3, { position: targetPosition }, { easing: 'sineInOut' })
  194. .start();
  195. }
  196. /**
  197. * 保存震动设置
  198. */
  199. private saveVibrationSetting() {
  200. const saveDataManager = SaveDataManager.getInstance();
  201. if (saveDataManager) {
  202. saveDataManager.updateSetting('vibrationEnabled', this.vibrationEnabled);
  203. }
  204. }
  205. /**
  206. * 立即更新震动滑动按钮位置(不使用动画)
  207. */
  208. private updateVibrationSlideButtonImmediate() {
  209. if (!this.vibrationSlideNode || !this.vibrationOnButton || !this.vibrationOffButton) {
  210. return;
  211. }
  212. // 获取目标按钮的位置
  213. const targetButton = this.vibrationEnabled ? this.vibrationOnButton : this.vibrationOffButton;
  214. const targetPosition = targetButton.node.position.clone();
  215. // 直接设置位置,不使用动画
  216. this.vibrationSlideNode.position = targetPosition;
  217. }
  218. /**
  219. * 更新进度条显示
  220. */
  221. private updateProgressBar(progressBar: ProgressBar, progress: number) {
  222. if (!progressBar) return;
  223. // 直接设置ProgressBar的进度值
  224. // ProgressBar会自动处理背景和前景的显示,保持黑色背景不变
  225. progressBar.progress = progress;
  226. }
  227. /**
  228. * 应用音效音量到音频系统
  229. */
  230. private applySoundEffectVolume(volume: number) {
  231. const audioManager = AudioManager.getInstance();
  232. if (audioManager) {
  233. audioManager.setSoundEffectVolume(volume);
  234. }
  235. //console.log(`[SoundController] 设置音效音量: ${volume}`);
  236. }
  237. /**
  238. * 应用音乐音量到音频系统
  239. */
  240. private applyMusicVolume(volume: number) {
  241. const audioManager = AudioManager.getInstance();
  242. if (audioManager) {
  243. audioManager.setMusicVolume(volume);
  244. }
  245. //console.log(`[SoundController] 设置音乐音量: ${volume}`);
  246. }
  247. /**
  248. * 保存设置到本地存储
  249. */
  250. private saveSettings() {
  251. const settings = {
  252. soundEffectEnabled: this.soundEffectEnabled,
  253. musicEnabled: this.musicEnabled,
  254. soundEffectVolume: this.savedSoundEffectVolume,
  255. musicVolume: this.savedMusicVolume
  256. };
  257. localStorage.setItem('audioSettings', JSON.stringify(settings));
  258. }
  259. /**
  260. * 从本地存储加载设置
  261. */
  262. private loadSettings() {
  263. const savedSettings = localStorage.getItem('audioSettings');
  264. if (savedSettings) {
  265. try {
  266. const settings = JSON.parse(savedSettings);
  267. this.soundEffectEnabled = settings.soundEffectEnabled ?? true;
  268. this.musicEnabled = settings.musicEnabled ?? true;
  269. this.savedSoundEffectVolume = settings.soundEffectVolume ?? 0.8;
  270. this.savedMusicVolume = settings.musicVolume ?? 0.8;
  271. // 更新UI显示
  272. this.updateUI();
  273. } catch (e) {
  274. console.warn('[SoundController] 加载音频设置失败:', e);
  275. }
  276. }
  277. // 从SaveDataManager加载震动设置
  278. const saveDataManager = SaveDataManager.getInstance();
  279. if (saveDataManager) {
  280. this.vibrationEnabled = saveDataManager.getSetting('vibrationEnabled');
  281. }
  282. }
  283. /**
  284. * 更新UI显示
  285. */
  286. private updateUI() {
  287. // 更新勾选标记
  288. if (this.soundEffectCheck) {
  289. this.soundEffectCheck.active = this.soundEffectEnabled;
  290. }
  291. if (this.musicCheck) {
  292. this.musicCheck.active = this.musicEnabled;
  293. }
  294. // 更新滑动条和进度条
  295. if (this.soundEffectSlider) {
  296. this.soundEffectSlider.progress = this.soundEffectEnabled ? this.savedSoundEffectVolume : 0;
  297. this.updateProgressBar(this.soundEffectProgressBar, this.soundEffectSlider.progress);
  298. }
  299. if (this.musicSlider) {
  300. this.musicSlider.progress = this.musicEnabled ? this.savedMusicVolume : 0;
  301. this.updateProgressBar(this.musicProgressBar, this.musicSlider.progress);
  302. }
  303. // 更新震动滑动按钮位置(不使用动画,直接设置位置)
  304. this.updateVibrationSlideButtonImmediate();
  305. }
  306. /**
  307. * 获取当前音效音量
  308. */
  309. public getSoundEffectVolume(): number {
  310. return this.soundEffectEnabled ? this.savedSoundEffectVolume : 0;
  311. }
  312. /**
  313. * 获取当前音乐音量
  314. */
  315. public getMusicVolume(): number {
  316. return this.musicEnabled ? this.savedMusicVolume : 0;
  317. }
  318. /**
  319. * 设置音效音量
  320. */
  321. public setSoundEffectVolume(volume: number) {
  322. this.savedSoundEffectVolume = Math.max(0, Math.min(1, volume));
  323. if (this.soundEffectEnabled && this.soundEffectSlider) {
  324. this.soundEffectSlider.progress = this.savedSoundEffectVolume;
  325. this.updateProgressBar(this.soundEffectProgressBar, this.savedSoundEffectVolume);
  326. this.applySoundEffectVolume(this.savedSoundEffectVolume);
  327. }
  328. this.saveSettings();
  329. }
  330. /**
  331. * 设置音乐音量
  332. */
  333. public setMusicVolume(volume: number) {
  334. this.savedMusicVolume = Math.max(0, Math.min(1, volume));
  335. if (this.musicEnabled && this.musicSlider) {
  336. this.musicSlider.progress = this.savedMusicVolume;
  337. this.updateProgressBar(this.musicProgressBar, this.savedMusicVolume);
  338. this.applyMusicVolume(this.savedMusicVolume);
  339. }
  340. this.saveSettings();
  341. }
  342. /**
  343. * 获取当前震动状态
  344. */
  345. public getVibrationEnabled(): boolean {
  346. return this.vibrationEnabled;
  347. }
  348. /**
  349. * 设置震动状态
  350. */
  351. public setVibrationEnabled(enabled: boolean) {
  352. this.vibrationEnabled = enabled;
  353. this.updateVibrationSlideButton();
  354. this.saveVibrationSetting();
  355. }
  356. /**
  357. * 开启音效和音乐
  358. */
  359. private enableSoundEffectAndMusic() {
  360. // 如果音效未开启,则开启音效
  361. if (!this.soundEffectEnabled) {
  362. this.soundEffectEnabled = true;
  363. // 恢复之前保存的音量或使用默认值0.5
  364. const volumeToRestore = this.savedSoundEffectVolume > 0 ? this.savedSoundEffectVolume : 0.5;
  365. this.soundEffectSlider.progress = volumeToRestore;
  366. this.updateProgressBar(this.soundEffectProgressBar, volumeToRestore);
  367. this.applySoundEffectVolume(volumeToRestore);
  368. // 更新勾选标记显示
  369. if (this.soundEffectCheck) {
  370. this.soundEffectCheck.active = true;
  371. }
  372. }
  373. // 如果音乐未开启,则开启音乐
  374. if (!this.musicEnabled) {
  375. this.musicEnabled = true;
  376. // 恢复之前保存的音量或使用默认值0.5
  377. const volumeToRestore = this.savedMusicVolume > 0 ? this.savedMusicVolume : 0.5;
  378. this.musicSlider.progress = volumeToRestore;
  379. this.updateProgressBar(this.musicProgressBar, volumeToRestore);
  380. this.applyMusicVolume(volumeToRestore);
  381. // 更新勾选标记显示
  382. if (this.musicCheck) {
  383. this.musicCheck.active = true;
  384. }
  385. }
  386. this.saveSettings();
  387. }
  388. /**
  389. * 关闭音效和音乐
  390. */
  391. private disableSoundEffectAndMusic() {
  392. // 关闭音效
  393. if (this.soundEffectEnabled) {
  394. this.soundEffectEnabled = false;
  395. // 保存当前音量并设置为0
  396. this.savedSoundEffectVolume = this.soundEffectSlider.progress;
  397. this.soundEffectSlider.progress = 0;
  398. this.updateProgressBar(this.soundEffectProgressBar, 0);
  399. this.applySoundEffectVolume(0);
  400. // 更新勾选标记显示
  401. if (this.soundEffectCheck) {
  402. this.soundEffectCheck.active = false;
  403. }
  404. }
  405. // 关闭音乐
  406. if (this.musicEnabled) {
  407. this.musicEnabled = false;
  408. // 保存当前音量并设置为0
  409. this.savedMusicVolume = this.musicSlider.progress;
  410. this.musicSlider.progress = 0;
  411. this.updateProgressBar(this.musicProgressBar, 0);
  412. this.applyMusicVolume(0);
  413. // 更新勾选标记显示
  414. if (this.musicCheck) {
  415. this.musicCheck.active = false;
  416. }
  417. }
  418. this.saveSettings();
  419. }
  420. }