SoundController.ts 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524
  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.setAllSoundVolume(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. // 兼容已有的本地键(微信端也可用)
  258. localStorage.setItem('audioSettings', JSON.stringify(settings));
  259. // 写入到 SaveDataManager 的玩家设置,保证随存档持久化
  260. const sdm = SaveDataManager.getInstance();
  261. if (sdm && typeof sdm.updateSetting === 'function') {
  262. sdm.updateSetting('soundEnabled', this.soundEffectEnabled);
  263. sdm.updateSetting('musicEnabled', this.musicEnabled);
  264. sdm.updateSetting('soundVolume', this.savedSoundEffectVolume);
  265. sdm.updateSetting('musicVolume', this.savedMusicVolume);
  266. }
  267. }
  268. /**
  269. * 从本地存储加载设置
  270. */
  271. private loadSettings() {
  272. const sdm = SaveDataManager.getInstance();
  273. let loadedFromSDM = false;
  274. if (sdm && typeof sdm.getSetting === 'function' && sdm.getPlayerData()) {
  275. try {
  276. this.soundEffectEnabled = sdm.getSetting('soundEnabled') ?? true;
  277. this.musicEnabled = sdm.getSetting('musicEnabled') ?? true;
  278. this.savedSoundEffectVolume = sdm.getSetting('soundVolume') ?? 0.8;
  279. this.savedMusicVolume = sdm.getSetting('musicVolume') ?? 0.6;
  280. loadedFromSDM = true;
  281. } catch (e) {
  282. // 读取失败则走本地兜底
  283. }
  284. }
  285. if (!loadedFromSDM) {
  286. const savedSettings = localStorage.getItem('audioSettings');
  287. if (savedSettings) {
  288. try {
  289. const settings = JSON.parse(savedSettings);
  290. this.soundEffectEnabled = settings.soundEffectEnabled ?? true;
  291. this.musicEnabled = settings.musicEnabled ?? true;
  292. this.savedSoundEffectVolume = settings.soundEffectVolume ?? 0.8;
  293. this.savedMusicVolume = settings.musicVolume ?? 0.6;
  294. } catch (e) {
  295. console.warn('[SoundController] 加载音频设置失败:', e);
  296. }
  297. }
  298. }
  299. // 从SaveDataManager加载震动设置
  300. if (sdm) {
  301. this.vibrationEnabled = sdm.getSetting('vibrationEnabled');
  302. }
  303. // 更新UI显示
  304. this.updateUI();
  305. }
  306. /**
  307. * 更新UI显示
  308. */
  309. private updateUI() {
  310. // 更新勾选标记
  311. if (this.soundEffectCheck) {
  312. this.soundEffectCheck.active = this.soundEffectEnabled;
  313. }
  314. if (this.musicCheck) {
  315. this.musicCheck.active = this.musicEnabled;
  316. }
  317. // 更新滑动条和进度条
  318. if (this.soundEffectSlider) {
  319. this.soundEffectSlider.progress = this.soundEffectEnabled ? this.savedSoundEffectVolume : 0;
  320. this.updateProgressBar(this.soundEffectProgressBar, this.soundEffectSlider.progress);
  321. // 应用当前滑条音量到音频系统(初始化时也生效)
  322. this.applySoundEffectVolume(this.soundEffectSlider.progress);
  323. }
  324. if (this.musicSlider) {
  325. this.musicSlider.progress = this.musicEnabled ? this.savedMusicVolume : 0;
  326. this.updateProgressBar(this.musicProgressBar, this.musicSlider.progress);
  327. // 应用当前滑条音量到音频系统(初始化时也生效)
  328. this.applyMusicVolume(this.musicSlider.progress);
  329. }
  330. // 更新震动滑动按钮位置(不使用动画,直接设置位置)
  331. this.updateVibrationSlideButtonImmediate();
  332. }
  333. /**
  334. * 获取当前音效音量
  335. */
  336. public getSoundEffectVolume(): number {
  337. return this.soundEffectEnabled ? this.savedSoundEffectVolume : 0;
  338. }
  339. /**
  340. * 获取当前音乐音量
  341. */
  342. public getMusicVolume(): number {
  343. return this.musicEnabled ? this.savedMusicVolume : 0;
  344. }
  345. /**
  346. * 设置音效音量
  347. */
  348. public setSoundEffectVolume(volume: number) {
  349. this.savedSoundEffectVolume = Math.max(0, Math.min(1, volume));
  350. if (this.soundEffectEnabled && this.soundEffectSlider) {
  351. this.soundEffectSlider.progress = this.savedSoundEffectVolume;
  352. this.updateProgressBar(this.soundEffectProgressBar, this.savedSoundEffectVolume);
  353. this.applySoundEffectVolume(this.savedSoundEffectVolume);
  354. }
  355. this.saveSettings();
  356. }
  357. /**
  358. * 设置音乐音量
  359. */
  360. public setMusicVolume(volume: number) {
  361. this.savedMusicVolume = Math.max(0, Math.min(1, volume));
  362. if (this.musicEnabled && this.musicSlider) {
  363. this.musicSlider.progress = this.savedMusicVolume;
  364. this.updateProgressBar(this.musicProgressBar, this.savedMusicVolume);
  365. this.applyMusicVolume(this.savedMusicVolume);
  366. }
  367. this.saveSettings();
  368. }
  369. /**
  370. * 获取当前震动状态
  371. */
  372. public getVibrationEnabled(): boolean {
  373. return this.vibrationEnabled;
  374. }
  375. /**
  376. * 设置震动状态
  377. */
  378. public setVibrationEnabled(enabled: boolean) {
  379. this.vibrationEnabled = enabled;
  380. this.updateVibrationSlideButton();
  381. this.saveVibrationSetting();
  382. }
  383. /**
  384. * 开启音效和音乐
  385. */
  386. private enableSoundEffectAndMusic() {
  387. // 如果音效未开启,则开启音效
  388. if (!this.soundEffectEnabled) {
  389. this.soundEffectEnabled = true;
  390. // 恢复之前保存的音量或使用默认值0.5
  391. const volumeToRestore = this.savedSoundEffectVolume > 0 ? this.savedSoundEffectVolume : 0.5;
  392. this.soundEffectSlider.progress = volumeToRestore;
  393. this.updateProgressBar(this.soundEffectProgressBar, volumeToRestore);
  394. this.applySoundEffectVolume(volumeToRestore);
  395. // 更新勾选标记显示
  396. if (this.soundEffectCheck) {
  397. this.soundEffectCheck.active = true;
  398. }
  399. }
  400. // 如果音乐未开启,则开启音乐
  401. if (!this.musicEnabled) {
  402. this.musicEnabled = true;
  403. // 恢复之前保存的音量或使用默认值0.5
  404. const volumeToRestore = this.savedMusicVolume > 0 ? this.savedMusicVolume : 0.5;
  405. this.musicSlider.progress = volumeToRestore;
  406. this.updateProgressBar(this.musicProgressBar, volumeToRestore);
  407. this.applyMusicVolume(volumeToRestore);
  408. // 更新勾选标记显示
  409. if (this.musicCheck) {
  410. this.musicCheck.active = true;
  411. }
  412. }
  413. this.saveSettings();
  414. }
  415. /**
  416. * 关闭音效和音乐
  417. */
  418. private disableSoundEffectAndMusic() {
  419. // 关闭音效
  420. if (this.soundEffectEnabled) {
  421. this.soundEffectEnabled = false;
  422. // 保存当前音量并设置为0
  423. this.savedSoundEffectVolume = this.soundEffectSlider.progress;
  424. this.soundEffectSlider.progress = 0;
  425. this.updateProgressBar(this.soundEffectProgressBar, 0);
  426. this.applySoundEffectVolume(0);
  427. // 更新勾选标记显示
  428. if (this.soundEffectCheck) {
  429. this.soundEffectCheck.active = false;
  430. }
  431. }
  432. // 关闭音乐
  433. if (this.musicEnabled) {
  434. this.musicEnabled = false;
  435. // 保存当前音量并设置为0
  436. this.savedMusicVolume = this.musicSlider.progress;
  437. this.musicSlider.progress = 0;
  438. this.updateProgressBar(this.musicProgressBar, 0);
  439. this.applyMusicVolume(0);
  440. // 更新勾选标记显示
  441. if (this.musicCheck) {
  442. this.musicCheck.active = false;
  443. }
  444. }
  445. this.saveSettings();
  446. }
  447. }