SettingViewComp.ts 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. /*
  2. * @Author: mojunshou 1637302775@qq.com
  3. * @Date: 2025-03-20 15:40:20
  4. * @LastEditors: mojunshou 1637302775@qq.com
  5. * @LastEditTime: 2025-04-29 16:48:45
  6. * @Description:设置界面
  7. */
  8. import { _decorator } from "cc";
  9. import { oops } from "db://oops-framework/core/Oops";
  10. import { ecs } from "db://oops-framework/libs/ecs/ECS";
  11. import { CCComp } from "db://oops-framework/module/common/CCComp";
  12. import { UIID } from "../config/GameUIConfig";
  13. import { CCVMParentComp } from "db://oops-framework/module/common/CCVMParentComp";
  14. import { Toggle } from "cc";
  15. import { Account } from "../../account/Account";
  16. import { ImageAsset, Texture2D, SpriteFrame, Sprite } from "cc";
  17. import { IRemoteOptions, resLoader } from "db://oops-framework/core/common/loader/ResLoader";
  18. import { Button } from "cc";
  19. import { smc } from "../SingletonModuleComp";
  20. import { CocosHandler } from "../manager/CocosHandler";
  21. import { LoginHandler } from "../manager/LoginHandler";
  22. import { UITransform } from "cc";
  23. import { Size } from "cc";
  24. import { GameEvent } from "../config/GameEvent";
  25. import { DCHandler } from "../manager/DCHandler";
  26. const { ccclass, property } = _decorator;
  27. /** 视图层对象 */
  28. @ccclass('SettingViewComp')
  29. @ecs.register('SettingView', false)
  30. export class SettingViewComp extends CCVMParentComp {
  31. data: any = {
  32. head_url: "",
  33. uid: 100000,
  34. }
  35. @property(Button)
  36. private musicBtn: Button = null!;
  37. @property(Button)
  38. private effectBtn: Button = null!;
  39. private _musicState: boolean = true;
  40. private _effectState: boolean = true;
  41. /** 视图层逻辑代码分离演示 */
  42. start() {
  43. // const entity = this.ent as ecs.Entity; // ecs.Entity 可转为当前模块的具体实体对象
  44. this.setButton();
  45. this.updateBtnState();
  46. this.updateHead();
  47. DCHandler.inst.reportData(3001000);
  48. }
  49. /** 视图对象通过 ecs.Entity.remove(SettingViewComp) 删除组件是触发组件处理自定义释放逻辑 */
  50. reset() {
  51. this.node.destroy();
  52. }
  53. private btn_about() {
  54. oops.gui.open(UIID.AboutUs);
  55. DCHandler.inst.reportData(3001004);
  56. }
  57. private btn_close() {
  58. oops.gui.remove(UIID.Setting);
  59. oops.message.dispatchEvent(GameEvent.updateGameState, "playing");
  60. DCHandler.inst.reportData(3001001);
  61. }
  62. //更新按钮状态
  63. private updateBtnState() {
  64. const music_state = oops.storage.get("music_state");
  65. const effect_state = oops.storage.get("effect_state");
  66. if (!music_state) {
  67. // 将字符串转换为布尔值
  68. let music_state_bool = true;
  69. this._musicState = music_state_bool;
  70. oops.storage.set("music_state", true);
  71. } else {
  72. let music_state_bool = music_state == "true" ? true : false
  73. this._musicState = music_state_bool;
  74. oops.storage.set("music_state", music_state_bool);
  75. }
  76. if (!effect_state) {
  77. oops.storage.set("effect_state", true);
  78. let effect_state_bool = true;
  79. this._effectState = effect_state_bool;
  80. } else {
  81. //存在
  82. let effect_state_bool = effect_state == "true" ? true : false;
  83. this._effectState = effect_state_bool;
  84. oops.storage.set("effect_state", effect_state_bool);
  85. }
  86. oops.audio.switchMusic = this._musicState;
  87. oops.audio.switchEffect = this._effectState;
  88. const music_on = this.musicBtn.node.getChildByPath("btn_on")!;
  89. const music_off = this.musicBtn.node.getChildByPath("btn_off")!;
  90. music_on.active = this._musicState;
  91. music_off.active = !this._musicState;
  92. const btn_on = this.effectBtn.node.getChildByPath("btn_on")!;
  93. const btn_off = this.effectBtn.node.getChildByPath("btn_off")!;
  94. btn_on.active = this._effectState;
  95. btn_off.active = !this._effectState;
  96. }
  97. //更新头像
  98. private updateHead() {
  99. this.data.uid = smc.account.AccountModel.uid;
  100. let url = smc.account.AccountModel.headUrl;
  101. let spriteNode = this.node.getChildByPath("Bg/btn_head/Mask/sp_head");
  102. if (spriteNode) {
  103. const uiTransform = spriteNode.getComponent(UITransform);
  104. uiTransform?.setContentSize(new Size(110, 110))
  105. let sprite = spriteNode.getComponent(Sprite);
  106. if (sprite) {
  107. console.log("更新头像啦")
  108. var opt: IRemoteOptions = { ext: ".png" };
  109. var onComplete = (err: Error | null, data: ImageAsset) => {
  110. const texture = new Texture2D();
  111. texture.image = data;
  112. const spriteFrame = new SpriteFrame();
  113. spriteFrame.texture = texture;
  114. sprite.spriteFrame = spriteFrame;
  115. }
  116. resLoader.loadRemote<ImageAsset>(url, opt, onComplete);
  117. }
  118. }
  119. }
  120. //打开音乐
  121. private btn_music() {
  122. this._musicState = !this._musicState;
  123. oops.storage.set("music_state", this._musicState);
  124. oops.audio.switchMusic = this._musicState;
  125. if (this._musicState) {
  126. DCHandler.inst.reportData(3001002);
  127. oops.audio.playMusicLoop("common/audios/bgm");
  128. } else {
  129. DCHandler.inst.reportData(3001003);
  130. }
  131. const btn_on = this.musicBtn.node.getChildByPath("btn_on")!;
  132. const btn_off = this.musicBtn.node.getChildByPath("btn_off")!;
  133. btn_on.active = this._musicState;
  134. btn_off.active = !this._musicState;
  135. }
  136. //打开音效
  137. private btn_effect() {
  138. this._effectState = !this._effectState;
  139. oops.storage.set("effect_state", this._effectState);
  140. oops.audio.switchEffect = this._effectState;
  141. const btn_on = this.effectBtn.node.getChildByPath("btn_on")!;
  142. const btn_off = this.effectBtn.node.getChildByPath("btn_off")!;
  143. btn_on.active = this._effectState;
  144. btn_off.active = !this._effectState;
  145. }
  146. //隐私协议
  147. private btn_privacy() {
  148. smc.game.GameModel.protocolType = 1;
  149. LoginHandler.inst.openAgreement();
  150. }
  151. //用户协议
  152. private btn_protocol() {
  153. smc.game.GameModel.protocolType = 2;
  154. LoginHandler.inst.openAgreement();
  155. }
  156. }