SoundController.ts 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532
  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. this.saveSettings();
  100. }
  101. }
  102. /**
  103. * 音乐滑动条变化事件
  104. */
  105. private onMusicSliderChange(slider: Slider) {
  106. if (this.musicEnabled) {
  107. this.savedMusicVolume = slider.progress;
  108. this.updateProgressBar(this.musicProgressBar, slider.progress);
  109. // TODO: 应用音乐音量到音频系统
  110. this.applyMusicVolume(slider.progress);
  111. // 立即保存设置,确保退出前已落盘
  112. this.saveSettings();
  113. }
  114. }
  115. /**
  116. * 音效开关按钮点击事件
  117. */
  118. private onSoundEffectCheckboxClick() {
  119. Audio.playUISound('data/弹球音效/ui play');
  120. this.soundEffectEnabled = !this.soundEffectEnabled;
  121. if (this.soundEffectEnabled) {
  122. // 开启音效:恢复之前保存的音量
  123. this.soundEffectSlider.progress = this.savedSoundEffectVolume;
  124. this.updateProgressBar(this.soundEffectProgressBar, this.savedSoundEffectVolume);
  125. this.applySoundEffectVolume(this.savedSoundEffectVolume);
  126. } else {
  127. // 关闭音效:保存当前音量并设置为0
  128. this.savedSoundEffectVolume = this.soundEffectSlider.progress;
  129. this.soundEffectSlider.progress = 0;
  130. this.updateProgressBar(this.soundEffectProgressBar, 0);
  131. this.applySoundEffectVolume(0);
  132. }
  133. // 更新勾选标记显示
  134. if (this.soundEffectCheck) {
  135. this.soundEffectCheck.active = this.soundEffectEnabled;
  136. }
  137. this.saveSettings();
  138. }
  139. /**
  140. * 音乐开关按钮点击事件
  141. */
  142. private onMusicCheckboxClick() {
  143. Audio.playUISound('data/弹球音效/ui play');
  144. this.musicEnabled = !this.musicEnabled;
  145. if (this.musicEnabled) {
  146. // 开启音乐:恢复之前保存的音量
  147. this.musicSlider.progress = this.savedMusicVolume;
  148. this.updateProgressBar(this.musicProgressBar, this.savedMusicVolume);
  149. this.applyMusicVolume(this.savedMusicVolume);
  150. } else {
  151. // 关闭音乐:保存当前音量并设置为0
  152. this.savedMusicVolume = this.musicSlider.progress;
  153. this.musicSlider.progress = 0;
  154. this.updateProgressBar(this.musicProgressBar, 0);
  155. this.applyMusicVolume(0);
  156. }
  157. // 更新勾选标记显示
  158. if (this.musicCheck) {
  159. this.musicCheck.active = this.musicEnabled;
  160. }
  161. this.saveSettings();
  162. }
  163. /**
  164. * 震动开启按钮点击事件
  165. */
  166. private onVibrationOnClick() {
  167. Audio.playUISound('data/弹球音效/ui play');
  168. this.vibrationEnabled = true;
  169. this.updateVibrationSlideButton();
  170. this.saveVibrationSetting();
  171. // 开启音效和音乐
  172. this.enableSoundEffectAndMusic();
  173. }
  174. /**
  175. * 震动关闭按钮点击事件
  176. */
  177. private onVibrationOffClick() {
  178. Audio.playUISound('data/弹球音效/ui play');
  179. this.vibrationEnabled = false;
  180. this.updateVibrationSlideButton();
  181. this.saveVibrationSetting();
  182. // 关闭音效和音乐
  183. this.disableSoundEffectAndMusic();
  184. }
  185. /**
  186. * 更新震动滑动按钮位置
  187. */
  188. private updateVibrationSlideButton() {
  189. if (!this.vibrationSlideNode || !this.vibrationOnButton || !this.vibrationOffButton) {
  190. return;
  191. }
  192. // 获取目标按钮的位置
  193. const targetButton = this.vibrationEnabled ? this.vibrationOnButton : this.vibrationOffButton;
  194. const targetPosition = targetButton.node.position.clone();
  195. // 使用缓动动画移动滑动节点
  196. tween(this.vibrationSlideNode)
  197. .to(0.3, { position: targetPosition }, { easing: 'sineInOut' })
  198. .start();
  199. }
  200. /**
  201. * 保存震动设置
  202. */
  203. private saveVibrationSetting() {
  204. const saveDataManager = SaveDataManager.getInstance();
  205. if (saveDataManager) {
  206. saveDataManager.updateSetting('vibrationEnabled', this.vibrationEnabled);
  207. }
  208. }
  209. /**
  210. * 立即更新震动滑动按钮位置(不使用动画)
  211. */
  212. private updateVibrationSlideButtonImmediate() {
  213. if (!this.vibrationSlideNode || !this.vibrationOnButton || !this.vibrationOffButton) {
  214. return;
  215. }
  216. // 获取目标按钮的位置
  217. const targetButton = this.vibrationEnabled ? this.vibrationOnButton : this.vibrationOffButton;
  218. const targetPosition = targetButton.node.position.clone();
  219. // 直接设置位置,不使用动画
  220. this.vibrationSlideNode.position = targetPosition;
  221. }
  222. /**
  223. * 更新进度条显示
  224. */
  225. private updateProgressBar(progressBar: ProgressBar, progress: number) {
  226. if (!progressBar) return;
  227. // 直接设置ProgressBar的进度值
  228. // ProgressBar会自动处理背景和前景的显示,保持黑色背景不变
  229. progressBar.progress = progress;
  230. }
  231. /**
  232. * 应用音效音量到音频系统
  233. */
  234. private applySoundEffectVolume(volume: number) {
  235. const audioManager = AudioManager.getInstance();
  236. if (audioManager) {
  237. audioManager.setAllSoundVolume(volume);
  238. }
  239. //console.log(`[SoundController] 设置音效音量: ${volume}`);
  240. }
  241. /**
  242. * 应用音乐音量到音频系统
  243. */
  244. private applyMusicVolume(volume: number) {
  245. const audioManager = AudioManager.getInstance();
  246. if (audioManager) {
  247. audioManager.setMusicVolume(volume);
  248. }
  249. //console.log(`[SoundController] 设置音乐音量: ${volume}`);
  250. }
  251. /**
  252. * 保存设置到本地存储
  253. */
  254. private saveSettings() {
  255. const settings = {
  256. soundEffectEnabled: this.soundEffectEnabled,
  257. musicEnabled: this.musicEnabled,
  258. soundEffectVolume: this.savedSoundEffectVolume,
  259. musicVolume: this.savedMusicVolume
  260. };
  261. // 兼容已有的本地键(微信端也可用)
  262. localStorage.setItem('audioSettings', JSON.stringify(settings));
  263. // 写入到 SaveDataManager 的玩家设置,保证随存档持久化
  264. const sdm = SaveDataManager.getInstance();
  265. if (sdm && typeof sdm.updateSetting === 'function') {
  266. sdm.updateSetting('soundEnabled', this.soundEffectEnabled);
  267. sdm.updateSetting('musicEnabled', this.musicEnabled);
  268. sdm.updateSetting('soundVolume', this.savedSoundEffectVolume);
  269. sdm.updateSetting('musicVolume', this.savedMusicVolume);
  270. // 设置变更需要立即强制保存,避免5秒节流导致未写盘
  271. if (typeof sdm.savePlayerData === 'function') {
  272. sdm.savePlayerData(true);
  273. }
  274. }
  275. }
  276. /**
  277. * 从本地存储加载设置
  278. */
  279. private loadSettings() {
  280. const sdm = SaveDataManager.getInstance();
  281. let loadedFromSDM = false;
  282. if (sdm && typeof sdm.getSetting === 'function' && sdm.getPlayerData()) {
  283. try {
  284. this.soundEffectEnabled = sdm.getSetting('soundEnabled') ?? true;
  285. this.musicEnabled = sdm.getSetting('musicEnabled') ?? true;
  286. this.savedSoundEffectVolume = sdm.getSetting('soundVolume') ?? 0.8;
  287. this.savedMusicVolume = sdm.getSetting('musicVolume') ?? 0.6;
  288. loadedFromSDM = true;
  289. } catch (e) {
  290. // 读取失败则走本地兜底
  291. }
  292. }
  293. if (!loadedFromSDM) {
  294. const savedSettings = localStorage.getItem('audioSettings');
  295. if (savedSettings) {
  296. try {
  297. const settings = JSON.parse(savedSettings);
  298. this.soundEffectEnabled = settings.soundEffectEnabled ?? true;
  299. this.musicEnabled = settings.musicEnabled ?? true;
  300. this.savedSoundEffectVolume = settings.soundEffectVolume ?? 0.8;
  301. this.savedMusicVolume = settings.musicVolume ?? 0.6;
  302. } catch (e) {
  303. console.warn('[SoundController] 加载音频设置失败:', e);
  304. }
  305. }
  306. }
  307. // 从SaveDataManager加载震动设置
  308. if (sdm) {
  309. this.vibrationEnabled = sdm.getSetting('vibrationEnabled');
  310. }
  311. // 更新UI显示
  312. this.updateUI();
  313. }
  314. /**
  315. * 更新UI显示
  316. */
  317. private updateUI() {
  318. // 更新勾选标记
  319. if (this.soundEffectCheck) {
  320. this.soundEffectCheck.active = this.soundEffectEnabled;
  321. }
  322. if (this.musicCheck) {
  323. this.musicCheck.active = this.musicEnabled;
  324. }
  325. // 更新滑动条和进度条
  326. if (this.soundEffectSlider) {
  327. this.soundEffectSlider.progress = this.soundEffectEnabled ? this.savedSoundEffectVolume : 0;
  328. this.updateProgressBar(this.soundEffectProgressBar, this.soundEffectSlider.progress);
  329. // 应用当前滑条音量到音频系统(初始化时也生效)
  330. this.applySoundEffectVolume(this.soundEffectSlider.progress);
  331. }
  332. if (this.musicSlider) {
  333. this.musicSlider.progress = this.musicEnabled ? this.savedMusicVolume : 0;
  334. this.updateProgressBar(this.musicProgressBar, this.musicSlider.progress);
  335. // 应用当前滑条音量到音频系统(初始化时也生效)
  336. this.applyMusicVolume(this.musicSlider.progress);
  337. }
  338. // 更新震动滑动按钮位置(不使用动画,直接设置位置)
  339. this.updateVibrationSlideButtonImmediate();
  340. }
  341. /**
  342. * 获取当前音效音量
  343. */
  344. public getSoundEffectVolume(): number {
  345. return this.soundEffectEnabled ? this.savedSoundEffectVolume : 0;
  346. }
  347. /**
  348. * 获取当前音乐音量
  349. */
  350. public getMusicVolume(): number {
  351. return this.musicEnabled ? this.savedMusicVolume : 0;
  352. }
  353. /**
  354. * 设置音效音量
  355. */
  356. public setSoundEffectVolume(volume: number) {
  357. this.savedSoundEffectVolume = Math.max(0, Math.min(1, volume));
  358. if (this.soundEffectEnabled && this.soundEffectSlider) {
  359. this.soundEffectSlider.progress = this.savedSoundEffectVolume;
  360. this.updateProgressBar(this.soundEffectProgressBar, this.savedSoundEffectVolume);
  361. this.applySoundEffectVolume(this.savedSoundEffectVolume);
  362. }
  363. this.saveSettings();
  364. }
  365. /**
  366. * 设置音乐音量
  367. */
  368. public setMusicVolume(volume: number) {
  369. this.savedMusicVolume = Math.max(0, Math.min(1, volume));
  370. if (this.musicEnabled && this.musicSlider) {
  371. this.musicSlider.progress = this.savedMusicVolume;
  372. this.updateProgressBar(this.musicProgressBar, this.savedMusicVolume);
  373. this.applyMusicVolume(this.savedMusicVolume);
  374. }
  375. this.saveSettings();
  376. }
  377. /**
  378. * 获取当前震动状态
  379. */
  380. public getVibrationEnabled(): boolean {
  381. return this.vibrationEnabled;
  382. }
  383. /**
  384. * 设置震动状态
  385. */
  386. public setVibrationEnabled(enabled: boolean) {
  387. this.vibrationEnabled = enabled;
  388. this.updateVibrationSlideButton();
  389. this.saveVibrationSetting();
  390. }
  391. /**
  392. * 开启音效和音乐
  393. */
  394. private enableSoundEffectAndMusic() {
  395. // 如果音效未开启,则开启音效
  396. if (!this.soundEffectEnabled) {
  397. this.soundEffectEnabled = true;
  398. // 恢复之前保存的音量或使用默认值0.5
  399. const volumeToRestore = this.savedSoundEffectVolume > 0 ? this.savedSoundEffectVolume : 0.5;
  400. this.soundEffectSlider.progress = volumeToRestore;
  401. this.updateProgressBar(this.soundEffectProgressBar, volumeToRestore);
  402. this.applySoundEffectVolume(volumeToRestore);
  403. // 更新勾选标记显示
  404. if (this.soundEffectCheck) {
  405. this.soundEffectCheck.active = true;
  406. }
  407. }
  408. // 如果音乐未开启,则开启音乐
  409. if (!this.musicEnabled) {
  410. this.musicEnabled = true;
  411. // 恢复之前保存的音量或使用默认值0.5
  412. const volumeToRestore = this.savedMusicVolume > 0 ? this.savedMusicVolume : 0.5;
  413. this.musicSlider.progress = volumeToRestore;
  414. this.updateProgressBar(this.musicProgressBar, volumeToRestore);
  415. this.applyMusicVolume(volumeToRestore);
  416. // 更新勾选标记显示
  417. if (this.musicCheck) {
  418. this.musicCheck.active = true;
  419. }
  420. }
  421. this.saveSettings();
  422. }
  423. /**
  424. * 关闭音效和音乐
  425. */
  426. private disableSoundEffectAndMusic() {
  427. // 关闭音效
  428. if (this.soundEffectEnabled) {
  429. this.soundEffectEnabled = false;
  430. // 保存当前音量并设置为0
  431. this.savedSoundEffectVolume = this.soundEffectSlider.progress;
  432. this.soundEffectSlider.progress = 0;
  433. this.updateProgressBar(this.soundEffectProgressBar, 0);
  434. this.applySoundEffectVolume(0);
  435. // 更新勾选标记显示
  436. if (this.soundEffectCheck) {
  437. this.soundEffectCheck.active = false;
  438. }
  439. }
  440. // 关闭音乐
  441. if (this.musicEnabled) {
  442. this.musicEnabled = false;
  443. // 保存当前音量并设置为0
  444. this.savedMusicVolume = this.musicSlider.progress;
  445. this.musicSlider.progress = 0;
  446. this.updateProgressBar(this.musicProgressBar, 0);
  447. this.applyMusicVolume(0);
  448. // 更新勾选标记显示
  449. if (this.musicCheck) {
  450. this.musicCheck.active = false;
  451. }
  452. }
  453. this.saveSettings();
  454. }
  455. }