SettingModel.ts 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. import UIHelp from "../../../logic/ui/UIHelp"
  2. import { AudioConst, PlatformType } from "../../const/TypeConst"
  3. import GameDataCenter from "../../GameDataCenter"
  4. import IDataModel from "../../../frameWork/model/IDataModel"
  5. const SETTING = "SETTING"
  6. export class SettingModel extends IDataModel {
  7. setting: {
  8. musicOn: boolean,
  9. effectOn: boolean,
  10. musicVol: number, //0~1
  11. effectVol: number //0~1
  12. }
  13. constructor() {
  14. super('setting')
  15. let data = this.Query(SETTING, JSON.stringify({ musicOn: true, effectOn: true, talkOn: true, spineOn: true }))
  16. this.setting = JSON.parse(data)
  17. cc.audioEngine.setMusicVolume(this.setting.musicVol)
  18. if (cc.sys.platform != cc.sys.TAOBAO_MINIGAME) {
  19. // 非淘宝小游戏,淘宝小游戏不支持设置音量
  20. cc.audioEngine.setEffectsVolume(this.setting.effectVol)
  21. }
  22. }
  23. private saveSetting() {
  24. this.Set(SETTING, JSON.stringify(this.setting))
  25. this.Save()
  26. }
  27. get musicOn(): boolean {
  28. return this.setting.musicOn
  29. }
  30. set musicOn(on: boolean) {
  31. this.setting.musicOn = on
  32. this.saveSetting()
  33. if (this.musicOn) {
  34. GameDataCenter.audio.bgmName = ""
  35. // if (UIHelp.IsShowingUI(UIBattleFieldView)) {
  36. // GameDataCenter.audio.playMusic(AudioConst.bgm_fight)
  37. // } else {
  38. GameDataCenter.audio.playMusic(AudioConst.bgm_base)
  39. // }
  40. } else {
  41. GameDataCenter.audio.stopMusic()
  42. }
  43. }
  44. get effectOn(): boolean {
  45. return this.setting.effectOn
  46. }
  47. set effectOn(on: boolean) {
  48. this.setting.effectOn = on
  49. this.saveSetting()
  50. if (!this.effectOn) {
  51. GameDataCenter.audio.stopAllEffects()
  52. }
  53. }
  54. get musicVol(): number {
  55. return this.setting.musicVol
  56. }
  57. set musicVol(vol: number) {
  58. this.setting.musicVol = vol
  59. cc.audioEngine.setMusicVolume(this.setting.musicVol)
  60. this.saveSetting()
  61. }
  62. get effectVol(): number {
  63. return this.setting.effectVol
  64. }
  65. set effectVol(vol: number) {
  66. this.setting.effectVol = vol
  67. if (cc.sys.platform != cc.sys.TAOBAO_MINIGAME) {
  68. // 非淘宝小游戏,淘宝小游戏不支持设置音量
  69. cc.audioEngine.setEffectsVolume(this.setting.effectVol)
  70. }
  71. this.saveSetting()
  72. }
  73. // 切换音乐
  74. turnMusic() {
  75. this.musicOn = !this.setting.musicOn
  76. }
  77. // 切换音效
  78. turnEffect() {
  79. this.effectOn = !this.setting.effectOn
  80. }
  81. sendCdkey(id: string) {
  82. // let param: CS_User = { cdKey: { id: id } }
  83. // this.send({ user: param })
  84. }
  85. }