SettingViewComp.ts 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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-23 11:57:18
  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. const { ccclass, property } = _decorator;
  26. /** 视图层对象 */
  27. @ccclass('SettingViewComp')
  28. @ecs.register('SettingView', false)
  29. export class SettingViewComp extends CCVMParentComp {
  30. data: any = {
  31. head_url: "",
  32. uid: 100000,
  33. }
  34. @property(Button)
  35. private musicBtn: Button = null!;
  36. @property(Button)
  37. private effectBtn: Button = null!;
  38. private _musicState: boolean = true;
  39. private _effectState: boolean = true;
  40. /** 视图层逻辑代码分离演示 */
  41. start() {
  42. // const entity = this.ent as ecs.Entity; // ecs.Entity 可转为当前模块的具体实体对象
  43. this.setButton();
  44. this.updateBtnState();
  45. this.updateHead();
  46. }
  47. /** 视图对象通过 ecs.Entity.remove(SettingViewComp) 删除组件是触发组件处理自定义释放逻辑 */
  48. reset() {
  49. this.node.destroy();
  50. }
  51. private btn_about() {
  52. oops.gui.open(UIID.AboutUs);
  53. }
  54. private btn_close() {
  55. oops.gui.remove(UIID.Setting);
  56. oops.message.dispatchEvent(GameEvent.updateGameState, "playing");
  57. }
  58. //更新按钮状态
  59. private updateBtnState() {
  60. const music_state = oops.storage.getBoolean("music_state");
  61. const effect_state = oops.storage.getBoolean("effect_state");
  62. oops.log.logView(music_state, "music_state");
  63. console.log("effect_state", effect_state);
  64. if (music_state == null) {
  65. oops.storage.set("music_state", true);
  66. this._musicState = true;
  67. } else {
  68. // 将字符串转换为布尔值
  69. let music_state_bool = music_state == true ? true : false;
  70. this._musicState = music_state_bool;
  71. }
  72. if (effect_state == null) {
  73. oops.storage.set("effect_state", true);
  74. this._effectState = true;
  75. } else {
  76. // 将字符串转换为布尔值
  77. let effect_state_bool = effect_state == true ? true : false;
  78. this._effectState = effect_state_bool;
  79. }
  80. oops.audio.switchMusic = this._musicState;
  81. oops.audio.switchEffect = this._effectState;
  82. const music_on = this.musicBtn.node.getChildByPath("btn_on")!;
  83. const music_off = this.musicBtn.node.getChildByPath("btn_off")!;
  84. music_on.active = this._musicState;
  85. music_off.active = !this._musicState;
  86. const btn_on = this.effectBtn.node.getChildByPath("btn_on")!;
  87. const btn_off = this.effectBtn.node.getChildByPath("btn_off")!;
  88. btn_on.active = this._effectState;
  89. btn_off.active = !this._effectState;
  90. }
  91. //更新头像
  92. private updateHead() {
  93. this.data.uid = smc.account.AccountModel.uid;
  94. let url = smc.account.AccountModel.headUrl;
  95. let spriteNode = this.node.getChildByPath("Bg/btn_head/Mask/sp_head");
  96. if (spriteNode) {
  97. const uiTransform = spriteNode.getComponent(UITransform);
  98. uiTransform?.setContentSize(new Size(110, 110))
  99. let sprite = spriteNode.getComponent(Sprite);
  100. if (sprite) {
  101. console.log("更新头像啦")
  102. var opt: IRemoteOptions = { ext: ".png" };
  103. var onComplete = (err: Error | null, data: ImageAsset) => {
  104. const texture = new Texture2D();
  105. texture.image = data;
  106. const spriteFrame = new SpriteFrame();
  107. spriteFrame.texture = texture;
  108. sprite.spriteFrame = spriteFrame;
  109. }
  110. resLoader.loadRemote<ImageAsset>(url, opt, onComplete);
  111. }
  112. }
  113. }
  114. //打开音乐
  115. private btn_music() {
  116. this._musicState = !this._musicState;
  117. oops.storage.set("music_state", this._musicState);
  118. oops.audio.switchMusic = this._musicState;
  119. const btn_on = this.musicBtn.node.getChildByPath("btn_on")!;
  120. const btn_off = this.musicBtn.node.getChildByPath("btn_off")!;
  121. btn_on.active = this._musicState;
  122. btn_off.active = !this._musicState;
  123. }
  124. //打开音效
  125. private btn_effect() {
  126. this._effectState = !this._effectState;
  127. oops.storage.set("effect_state", this._effectState);
  128. oops.audio.switchEffect = this._effectState;
  129. const btn_on = this.effectBtn.node.getChildByPath("btn_on")!;
  130. const btn_off = this.effectBtn.node.getChildByPath("btn_off")!;
  131. btn_on.active = this._effectState;
  132. btn_off.active = !this._effectState;
  133. }
  134. //隐私协议
  135. private btn_privacy() {
  136. smc.game.GameModel.protocolType = 1;
  137. LoginHandler.inst.openAgreement();
  138. }
  139. //用户协议
  140. private btn_protocol() {
  141. smc.game.GameModel.protocolType = 2;
  142. LoginHandler.inst.openAgreement();
  143. }
  144. }