LoadingViewComp.ts 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. /*
  2. * @Author: mojunshou 1637302775@qq.com
  3. * @Date: 2025-03-19 16:23:51
  4. * @LastEditors: mojunshou 1637302775@qq.com
  5. * @LastEditTime: 2025-04-08 15:04:41
  6. * @Description: loading界面
  7. */
  8. import { _decorator, Toggle } from "cc";
  9. import { DeviceUtil } from "db://oops-framework/core/utils/DeviceUtil";
  10. import { ModuleUtil } from "db://oops-framework/module/common/ModuleUtil";
  11. import { oops } from "../../../../../extensions/oops-plugin-framework/assets/core/Oops";
  12. import { ecs } from "../../../../../extensions/oops-plugin-framework/assets/libs/ecs/ECS";
  13. import { CCVMParentComp } from "../../../../../extensions/oops-plugin-framework/assets/module/common/CCVMParentComp";
  14. import { AndroidEvent } from "../../common/config/AndroidEvent";
  15. import { GameEvent } from "../../common/config/GameEvent";
  16. import { UIID } from "../../common/config/GameUIConfig";
  17. import { CocosHandler } from "../../common/manager/CocosHandler";
  18. import { smc } from "../../common/SingletonModuleComp";
  19. import { EliminateViewComp } from "../../eliminate/view/EliminateViewComp";
  20. const { ccclass, property } = _decorator;
  21. /** 游戏资源加载 */
  22. @ccclass('LoadingViewComp')
  23. @ecs.register('LoadingView', false)
  24. export class LoadingViewComp extends CCVMParentComp {
  25. /** VM 组件绑定数据 */
  26. data: any = {
  27. /** 加载资源当前进度 */
  28. finished: 0,
  29. /** 加载资源最大进度 */
  30. total: 0,
  31. /** 加载资源进度比例值 */
  32. progress: "0",
  33. /** 加载流程中提示文本 */
  34. prompt: "",
  35. /**btn_show*/
  36. btn_show: 0
  37. };
  38. private progress: number = 0;
  39. start() {
  40. this.enter();
  41. this.setButton();
  42. this.updateToggleState();
  43. }
  44. async enter() {
  45. this.addEvent();
  46. if (DeviceUtil.isNative && DeviceUtil.isAndroid) {
  47. const state = await CocosHandler.inst.getPrivacyStatus();
  48. console.log("隐私状态>>>>>>>>>>>>>>>>", state);
  49. if (state.granted) {
  50. const isWxLogin = oops.storage.getBoolean("isWxLogin");
  51. if (isWxLogin) {
  52. //直接登录
  53. let result = await CocosHandler.inst.wechat_login();
  54. if (result.code) {
  55. this.setWxLoginBtnState(false);
  56. this.loadRes();
  57. } else {
  58. oops.gui.toast("登录失败,请重试")
  59. }
  60. } else {
  61. console.log("缓存没有,需要显示登录按钮")
  62. this.setWxLoginBtnState(true);
  63. }
  64. } else {
  65. oops.gui.open(UIID.KindTips);
  66. }
  67. } else {
  68. //非原生,网页的
  69. this.setWxLoginBtnState(false);
  70. this.loadRes();
  71. }
  72. }
  73. private addEvent() {
  74. this.on(AndroidEvent.AgreePrivacy, this.onAgreePrivacy, this);
  75. this.on(GameEvent.WechatLoginSuss, this.loginSuss, this)
  76. }
  77. private loginSuss() {
  78. //关闭按钮
  79. this.setWxLoginBtnState(false);
  80. this.loadRes();
  81. //加载资源
  82. }
  83. private onAgreePrivacy() {
  84. console.log("同意隐私协议");
  85. oops.storage.set("agree", true);
  86. oops.gui.remove(UIID.KindTips);
  87. //如果是客户端就显示微信登录按钮
  88. if (DeviceUtil.isNative && DeviceUtil.isAndroid) {
  89. this.setWxLoginBtnState(true);
  90. } else {
  91. this.loadRes();
  92. }
  93. }
  94. /** 加载资源 */
  95. private async loadRes() {
  96. this.data.progress = 0;
  97. await this.loadCustom();
  98. this.loadGameRes();
  99. }
  100. /** 加载游戏本地JSON数据(自定义内容) */
  101. private loadCustom() {
  102. // 加载游戏本地JSON数据的多语言提示文本
  103. this.data.prompt = oops.language.getLangByID("loading_load_json");
  104. }
  105. /** 加载初始游戏内容资源 */
  106. private loadGameRes() {
  107. // 加载初始游戏内容资源的多语言提示文本
  108. this.data.prompt = oops.language.getLangByID("loading_load_game");
  109. oops.res.loadDir("game", this.onProgressCallback.bind(this), this.onCompleteCallback.bind(this));
  110. }
  111. /** 加载进度事件 */
  112. private onProgressCallback(finished: number, total: number, item: any) {
  113. this.data.finished = finished;
  114. this.data.total = total;
  115. var progress = finished / total;
  116. if (progress > this.progress) {
  117. this.progress = progress;
  118. this.data.progress = (progress * 100).toFixed(2);
  119. }
  120. }
  121. /** 加载完成事件 */
  122. private async onCompleteCallback() {
  123. // 获取用户信息的多语言提示文本
  124. this.data.prompt = oops.language.getLangByID("loading_load_player");
  125. await ModuleUtil.addViewUiAsync(smc.account, EliminateViewComp, UIID.Eliminate);
  126. ModuleUtil.removeViewUi(this.ent, LoadingViewComp, UIID.Loading);
  127. }
  128. /**
  129. * @description: 微信登录
  130. * @return {*}
  131. */
  132. private async btn_wxlogin() {
  133. //是否同意了我们的隐私政策
  134. const agree = oops.storage.getBoolean("agree");
  135. if (!agree) {
  136. // oops.gui.open(UIID.KindTips);
  137. oops.gui.toast("请同意隐私政策")
  138. return;
  139. }
  140. //登录完要隐藏微信按钮然后加载进度
  141. let result = await CocosHandler.inst.wechat_login();
  142. if (result.code) {
  143. this.setWxLoginBtnState(false);
  144. this.loadRes();
  145. } else {
  146. oops.gui.toast("登录失败,请重试")
  147. }
  148. }
  149. private btn_showAd() {
  150. CocosHandler.inst.ad_interstitial();
  151. }
  152. /**
  153. * @description: 显示微信登录
  154. * @return {*}
  155. */
  156. setWxLoginBtnState(state: boolean) {
  157. const wxNode = this.node.getChildByName("login_node");
  158. if (wxNode) {
  159. wxNode.active = state;
  160. }
  161. //load条相反
  162. const loadNode = this.node.getChildByName("pro_progress");
  163. if (loadNode) {
  164. loadNode.active = !state;
  165. }
  166. }
  167. /**
  168. * @description:隐私政策toggle
  169. * @return {*}
  170. */
  171. onToggleChick(toggle: Toggle) {
  172. if (toggle.isChecked) {
  173. oops.storage.set("agree", true);
  174. } else {
  175. oops.storage.set("agree", false);
  176. }
  177. }
  178. //更新toggle状态
  179. updateToggleState() {
  180. const agree = oops.storage.getBoolean("agree");
  181. const toggle = this.node.getChildByPath("login_node/login_bg/tog_agree")!.uiToggle;
  182. if (agree) {
  183. toggle.isChecked = true;
  184. } else {
  185. toggle.isChecked = false;
  186. }
  187. }
  188. reset(): void { }
  189. /**
  190. * 保存关卡,分数,目标分数
  191. *
  192. *
  193. */
  194. }