DoubleRewardsView.ts 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. /*
  2. * @Author: mojunshou 1637302775@qq.com
  3. * @Date: 2025-03-21 11:57:43
  4. * @LastEditors: mojunshou 1637302775@qq.com
  5. * @LastEditTime: 2025-04-30 12:06:15
  6. * @Description: 惊喜翻倍弹窗
  7. */
  8. import { _decorator, instantiate, Label, Node, Prefab, UITransform } from 'cc';
  9. import { oops } from 'db://oops-framework/core/Oops';
  10. import VMParent from 'db://oops-framework/libs/model-view/VMParent';
  11. import { AD_TYPE } from '../common/config/GameDefine';
  12. import { UIID } from '../common/config/GameUIConfig';
  13. import { ADHandler } from '../common/manager/ADHandler';
  14. import { smc } from '../common/SingletonModuleComp';
  15. import { BricsItem } from './BricsItem';
  16. import { DCHandler } from '../common/manager/DCHandler';
  17. import { ServerHandler } from '../common/manager/ServerHandler';
  18. import { GameEvent } from '../common/config/GameEvent';
  19. const { ccclass, property } = _decorator;
  20. interface LevelInfo {
  21. level: number;
  22. eventType: string;
  23. withdraw: boolean;
  24. position?: number; //位置
  25. isFirstNotWithdraw?: boolean; //是否第一个没提现
  26. isAllWithdrawBeforeSecondLast?: boolean; //是否所有提现点都提现
  27. }
  28. /** 显示对象控制 */
  29. @ccclass('DoubleRewardsView')
  30. export class DoubleRewardsView extends VMParent {
  31. @property([Label])
  32. lab_list: Label[] = [];
  33. @property(Node)
  34. private redBagNode: Node = null!; //红包Node
  35. @property(Node)
  36. private iconNode: Node = null!; //角标Node
  37. //进度条Node
  38. @property(Node)
  39. private progressNode: Node = null!; //进度条Node
  40. //提示文字Node
  41. @property(Node)
  42. private tipsLabelNode: Node = null!; //提示文字Node
  43. @property(Node)
  44. private levelListNode: Node = null!; //等级Node
  45. @property(Prefab)
  46. private levelItemPrefab: Prefab = null!; //等级Item预制体
  47. data: any = {
  48. cd: 0,
  49. cdMax: 0,
  50. //总金额
  51. totalGoldNum: 0,
  52. //手续费
  53. handlingCharge: 0,
  54. //已经有的金额
  55. curGoldNum: 0,
  56. //差多少
  57. gapGoldNum: 0,
  58. //自动领取时间
  59. time: 0,
  60. goldNum: 0, //金砖数量
  61. }
  62. private callback: Function = null!;
  63. private isAuto: boolean = true;
  64. protected start() {
  65. DCHandler.inst.reportData(2000400);
  66. this.setButton();
  67. this.setData();
  68. this.setAutoReceive();
  69. oops.message.dispatchEvent(GameEvent.updateGameState, "paused");
  70. }
  71. btn_alittle() {
  72. oops.gui.remove(UIID.DoubleRewards);
  73. ServerHandler.inst.getLittleRewards();
  74. ADHandler.inst.showAd(AD_TYPE.Double_Close);
  75. DCHandler.inst.reportData(2000401);
  76. DCHandler.inst.reportData(2000403);
  77. oops.message.dispatchEvent(GameEvent.updateGameState, "playing");
  78. smc.game.GameModel.smallAdCount++;
  79. }
  80. btn_all() {
  81. this.isAuto = false;
  82. smc.game.GameModel.viewType = "double_reward";
  83. ADHandler.inst.showAd(AD_TYPE.Double_Receive);
  84. oops.gui.remove(UIID.DoubleRewards);
  85. oops.message.dispatchEvent(GameEvent.updateGameState, "playing");
  86. smc.game.GameModel.allAdCount++;
  87. }
  88. //设置3秒自动领取
  89. private setAutoReceive() {
  90. this.callback = () => {
  91. this.data.time--;
  92. if (this.data.time <= 0) {
  93. //
  94. if (this.isAuto) {
  95. this.isAuto = false;
  96. oops.gui.remove(UIID.DoubleRewards);
  97. ServerHandler.inst.getLittleRewards();
  98. ADHandler.inst.showAd(AD_TYPE.Double_Close);
  99. DCHandler.inst.reportData(2000402);
  100. DCHandler.inst.reportData(2000403);
  101. oops.message.dispatchEvent(GameEvent.updateGameState, "playing");
  102. smc.game.GameModel.smallAdCount++;
  103. }
  104. if (this.callback) {
  105. this.unschedule(this.callback);
  106. }
  107. }
  108. }
  109. this.schedule(this.callback, 1, this.data.time - 1);
  110. }
  111. //算出底部进度条位置
  112. //设置数据
  113. private setData() {
  114. this.data.time = 3;
  115. this.isAuto = true;
  116. const info = smc.game.GameModel.doubleRewardInfo;
  117. if (info) {
  118. if (info.handingChargeProgress) {
  119. this.data.totalGoldNum = info.handingChargeProgress.totalMoney;
  120. this.data.handlingCharge = info.handingChargeProgress.handingCharge;
  121. this.data.curGoldNum = info.handingChargeProgress.hasNum;
  122. this.data.gapGoldNum = info.handingChargeProgress.gapNum;
  123. const showNode = this.node.getChildByPath("bottonNode/tips_node1");
  124. if (showNode) {
  125. showNode.active = true;
  126. }
  127. //计算红包的位置,计算角标位置
  128. //获取进度条长度
  129. const transform = this.progressNode.getComponent(UITransform);
  130. if (transform) {
  131. const progressWidth = transform.width;
  132. //通过长度,然后加上原始位置加长度*进度,就等于红包和icon需要放置的X轴位置
  133. const progressX = this.progressNode.position.x;
  134. //计算红包的位置
  135. const redBagX = progressX + (this.data.curGoldNum / this.data.handlingCharge) * progressWidth;
  136. this.redBagNode.setPosition(redBagX, this.redBagNode.position.y);
  137. //计算角标的位置
  138. this.iconNode.setPosition(redBagX, this.iconNode.position.y);
  139. //可以通过除,如果大于0.5就右边一点小于就左边一点,中间就在中间
  140. //x轴位置,-60, 0,60;
  141. if (this.data.curGoldNum / this.data.handlingCharge > 0.5) {
  142. this.tipsLabelNode.setPosition(50, this.tipsLabelNode.position.y);
  143. } else if (this.data.curGoldNum / this.data.handlingCharge < 0.5) {
  144. this.tipsLabelNode.setPosition(-80, this.tipsLabelNode.position.y);
  145. } else {
  146. this.tipsLabelNode.setPosition(0, this.tipsLabelNode.position.y);
  147. }
  148. }
  149. } else {
  150. //隐藏节点
  151. const showNode = this.node.getChildByPath("bottonNode/tips_node1");
  152. if (showNode) {
  153. showNode.active = false;
  154. }
  155. }
  156. if (info.levelProgress) {
  157. //设置进度条
  158. //克隆,然后添加
  159. const showNode = this.node.getChildByPath("bottonNode/tips_node2");
  160. if (showNode) {
  161. showNode.active = true;
  162. }
  163. this.data.goldNum = info.levelProgress.nextProgress;
  164. const levelInfoList = this.processLevelInfo(info.levelProgress.levelInfoList);
  165. this.levelListNode.destroyAllChildren();
  166. levelInfoList.forEach((item, index) => {
  167. const levelItem = instantiate(this.levelItemPrefab);
  168. this.levelListNode.addChild(levelItem);
  169. if (item.position) {
  170. levelItem.setPosition(item.position, 0);
  171. //设置数据
  172. const script = levelItem.getComponent(BricsItem);
  173. if (script) {
  174. script.updateData(item);
  175. }
  176. }
  177. });
  178. //计算进度//如果当前关提现了
  179. //const 最大关
  180. const maxLevel = levelInfoList[levelInfoList.length - 1].level;
  181. //const 当前关
  182. const currentLevel = smc.account.AccountModel.curLevel;
  183. this.data.curGoldNum = currentLevel;
  184. this.data.handlingCharge = maxLevel;
  185. } else {
  186. //隐藏节点
  187. const showNode = this.node.getChildByPath("bottonNode/tips_node2");
  188. if (showNode) {
  189. showNode.active = false;
  190. }
  191. }
  192. if (info.showReward) {
  193. info.showReward.forEach((item, index) => {
  194. if (this.lab_list[index]) {
  195. this.lab_list[index].string = item.propNum + "";
  196. }
  197. })
  198. }
  199. }
  200. }
  201. //计算位置
  202. calculatePositions(levelInfoList: LevelInfo[], totalLength: number = 516): LevelInfo[] {
  203. if (levelInfoList.length === 0) return [];
  204. const firstLevel = levelInfoList[0].level;
  205. const lastLevel = levelInfoList[levelInfoList.length - 1].level;
  206. const levelRange = lastLevel - firstLevel || 1; // 避免除0
  207. return levelInfoList.map((item, index) => {
  208. if (index === 0) {
  209. item.position = this.progressNode.position.x;
  210. } else if (index === levelInfoList.length - 1) {
  211. item.position = this.progressNode.position.x + totalLength;
  212. } else {
  213. //这里还有是进度条的初始位置+进度条的长度*当前的进度
  214. item.position = this.progressNode.position.x + ((item.level - firstLevel) / levelRange) * totalLength;
  215. }
  216. return item;
  217. });
  218. }
  219. processLevelInfo(levelInfoList: LevelInfo[], totalLength: number = 516): LevelInfo[] {
  220. if (levelInfoList.length === 0) return [];
  221. const firstLevel = levelInfoList[0].level;
  222. const lastLevel = levelInfoList[levelInfoList.length - 1].level;
  223. const levelRange = lastLevel - firstLevel || 1;
  224. let foundFirstNotWithdraw = false;
  225. //现在要算平均长度,总长度除数组长度
  226. let singleLength = totalLength / (levelInfoList.length - 1);
  227. const secondLastIndex = levelInfoList.length - 2;
  228. const allBeforeSecondLastWithdrawed = levelInfoList
  229. .slice(0, secondLastIndex)
  230. .every(item => item.withdraw);
  231. return levelInfoList.map((item, index) => {
  232. // 设置 position
  233. if (index === 0) {
  234. item.position = this.progressNode.position.x;
  235. } else if (index === levelInfoList.length - 1) {
  236. item.position = this.progressNode.position.x + totalLength;
  237. } else {
  238. //因为产品和UI说只要算平均长度就好
  239. // item.position = this.progressNode.position.x + (((item.level - firstLevel) / levelRange) * totalLength);
  240. item.position = this.progressNode.position.x + index * singleLength;
  241. }
  242. // 标记第一个未提现
  243. if (!foundFirstNotWithdraw && !item.withdraw) {
  244. item.isFirstNotWithdraw = true;
  245. foundFirstNotWithdraw = true;
  246. } else {
  247. item.isFirstNotWithdraw = false;
  248. }
  249. // 标记是否倒数第二个之前的都提现
  250. item.isAllWithdrawBeforeSecondLast = allBeforeSecondLastWithdrawed;
  251. return item;
  252. });
  253. }
  254. }