CheckpointMainViewComp.ts 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. /*
  2. * @Author: dgflash
  3. * @Date: 2022-06-02 09:38:48
  4. * @LastEditors: dgflash
  5. * @LastEditTime: 2022-09-20 17:18:29
  6. */
  7. import { EventTouch, Node, Widget, _decorator, sys, view } from "cc";
  8. import { oops } from "../../../../../extensions/oops-plugin-framework/assets/core/Oops";
  9. import { ecs } from "../../../../../extensions/oops-plugin-framework/assets/libs/ecs/ECS";
  10. import { CCComp } from "../../../../../extensions/oops-plugin-framework/assets/module/common/CCComp";
  11. import { UIID } from "../../common/config/GameUIConfig";
  12. import { SingletonModuleComp } from "../../common/SingletonModuleComp";
  13. import { CheckpointLevelItemViewComp } from "./CheckpointLevelItem";
  14. import { AdManager } from "../../../platform/ad/AdManager";
  15. import { ClearCellOperationComp } from "../bll/ClearCellOperation";
  16. import { AddCellOperationComp } from "../bll/AddCellOperation";
  17. import { FullVehicleOperationComp } from "../bll/FullVehicleOperation";
  18. const { ccclass, property } = _decorator;
  19. /** 主界面 */
  20. @ccclass('CheckpointMainViewComp')
  21. @ecs.register('CheckpointMainView', false)
  22. export class CheckpointMainViewComp extends CCComp {
  23. @property({ type: [CheckpointLevelItemViewComp], visible: true, tooltip: '关卡item' })
  24. itemArr: Array<CheckpointLevelItemViewComp> = []
  25. onAdded(args: any) {
  26. console.log(args);
  27. }
  28. onLoad() {
  29. const btnSkin = this.node.getChildByName("btn_skin")
  30. const btnAddCell = this.node.getChildByPath("bottom/btn_add_cell")
  31. const btnClear = this.node.getChildByPath("bottom/btn_clear")
  32. const btnLeave = this.node.getChildByPath("bottom/btn_leave")
  33. const btnReward = this.node.getChildByPath("btn_reward")
  34. const btnSetting = this.node.getChildByPath("btn_setting")
  35. const levelGroup = this.node.getChildByPath("level")
  36. // console.log('当前平台',sys.os)
  37. // if (sys.os === sys.OS.IOS || sys.os === sys.OS.ANDROID) {
  38. // }
  39. const safeArea = sys.getSafeAreaRect();
  40. const winSize = view.getVisibleSize();
  41. // 调整其 Widget 组件来适配刘海
  42. const widget = levelGroup.getComponent(Widget);
  43. if (widget) {
  44. widget.top = winSize.height - safeArea.height - safeArea.y;
  45. // console.log('偏移高度',widget.top)
  46. widget.updateAlignment(); // 立即更新 widget 对齐
  47. }
  48. btnSkin.on(Node.EventType.TOUCH_END, this.onTouchEnd, this);
  49. btnAddCell.on(Node.EventType.TOUCH_END, this.onTouchEnd, this);
  50. btnClear.on(Node.EventType.TOUCH_END, this.onTouchEnd, this);
  51. btnLeave.on(Node.EventType.TOUCH_END, this.onTouchEnd, this);
  52. btnSetting.on(Node.EventType.TOUCH_END, this.onTouchEnd, this);
  53. btnReward.on(Node.EventType.TOUCH_END, this.onTouchEnd, this);
  54. this.initLevel()
  55. if (sys.platform === sys.Platform.BYTEDANCE_MINI_GAME) {
  56. //判断用户是否支持侧边栏进入功能,有些旧版的抖音没有侧边栏,这种情况就把入口有礼那个按钮给隐藏掉
  57. tt.checkScene({
  58. scene: "sidebar",
  59. success: (res) => {
  60. btnReward.active = true
  61. },
  62. fail: (res) => {
  63. btnReward.active = false
  64. }
  65. });
  66. } else {
  67. btnReward.active = false
  68. }
  69. // btnReward.active = true
  70. }
  71. initLevel() {
  72. const lv = ecs.getSingleton(SingletonModuleComp).account.AccountModel.lv
  73. const arr = this.generateRange(lv)
  74. for (let index = 0; index < arr.length; index++) {
  75. const element = arr[index];
  76. this.itemArr[index].lv = element
  77. }
  78. }
  79. generateRange(n: number) {
  80. // // 计算起始数字
  81. // let start = n - ((n - 1) % 5);
  82. // // 创建一个数组,包含从起始数字开始的5个连续数字
  83. // let range = Array.from({ length: 5 }, (v, i) => start + i);
  84. // return range;
  85. // 直接处理1-5的特殊情况
  86. if (n >= 1 && n <= 5) {
  87. return [1, 2, 3, 4, 5];
  88. } else {
  89. // 计算中间值的位置,确保n位于中间
  90. let start = n - 2; // 使n成为中间值
  91. // 确保开始值不小于1
  92. start = Math.max(start, 1);
  93. let range = Array.from({ length: 5 }, (v, i) => start + i);
  94. // 调整以确保数组不超过n的下一个完整5的倍数段
  95. if (range[4] - range[0] !== 4) { // 如果计算的范围超出了预期(比如遇到边界情况)
  96. range = range.map((_, i) => n - 2 + i); // 重新计算以保持n在中间
  97. }
  98. return range;
  99. }
  100. }
  101. private onTouchEnd(event: EventTouch) {
  102. // console.log('点到我了',event.target.name)
  103. const checkpoint = ecs.getSingleton(SingletonModuleComp).account.checkpoint
  104. switch (event.target.name) {
  105. case "btn_setting":
  106. oops.gui.open(UIID.Setting);
  107. break;
  108. case "btn_skin":
  109. oops.gui.open(UIID.Skin);
  110. break;
  111. case "btn_clear":
  112. if (checkpoint.CheckpointModelBase.vm.clearCount >= 3) {
  113. oops.gui.toast('该关卡使用次数已满')
  114. return
  115. }
  116. oops.gui.open(UIID.ClearCell);
  117. // if (sys.platform === sys.Platform.WECHAT_GAME) {
  118. // oops.gui.open(UIID.ClearCell);
  119. // } else {
  120. // AdManager.getInstance().showRewardVideoAd(() => {
  121. // console.log('成功看完广告')
  122. // ecs.getSingleton(SingletonModuleComp).account.checkpoint.add(ClearCellOperationComp)
  123. // ecs.getSingleton(SingletonModuleComp).account.checkpoint.CheckpointModelBase.vm.clearCount += 1
  124. // })
  125. // }
  126. break;
  127. case "btn_add_cell":
  128. if (checkpoint.CheckpointModelBase.vm.addCellCount >= 3) {
  129. oops.gui.toast('该关卡使用次数已满')
  130. return
  131. }
  132. oops.gui.open(UIID.AddCell);
  133. // if (sys.platform === sys.Platform.WECHAT_GAME) {
  134. // oops.gui.open(UIID.AddCell);
  135. // } else {
  136. // AdManager.getInstance().showRewardVideoAd(() => {
  137. // console.log('成功看完广告')
  138. // ecs.getSingleton(SingletonModuleComp).account.checkpoint.add(AddCellOperationComp)
  139. // ecs.getSingleton(SingletonModuleComp).account.checkpoint.CheckpointModelBase.vm.addCellCount += 1
  140. // })
  141. // }
  142. break;
  143. case "btn_leave":
  144. // ecs.getSingleton(SingletonModuleComp).account.checkpoint.add(FullVehicleOperationComp)
  145. // ecs.getSingleton(SingletonModuleComp).account.checkpoint.CheckpointModelBase.vm.leaveCount += 1
  146. // return
  147. if (checkpoint.CheckpointModelBase.vm.leaveCount >= 3) {
  148. oops.gui.toast('该关卡使用次数已满')
  149. return
  150. }
  151. oops.gui.open(UIID.VehicleLeave);
  152. // if (sys.platform === sys.Platform.WECHAT_GAME) {
  153. // oops.gui.open(UIID.VehicleLeave);
  154. // } else {
  155. // AdManager.getInstance().showRewardVideoAd(() => {
  156. // console.log('成功看完广告')
  157. // ecs.getSingleton(SingletonModuleComp).account.checkpoint.add(FullVehicleOperationComp)
  158. // ecs.getSingleton(SingletonModuleComp).account.checkpoint.CheckpointModelBase.vm.leaveCount += 1
  159. // })
  160. // }
  161. break;
  162. case "btn_reward":
  163. oops.gui.open(UIID.SideReward);
  164. break;
  165. }
  166. event.propagationStopped = true;
  167. }
  168. reset() {
  169. this.node.destroy();
  170. }
  171. }