RedBagItem.ts 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. // Learn TypeScript:
  2. // - https://docs.cocos.com/creator/manual/en/scripting/typescript.html
  3. // Learn Attribute:
  4. // - https://docs.cocos.com/creator/manual/en/scripting/reference/attributes.html
  5. // Learn life-cycle callbacks:
  6. // - https://docs.cocos.com/creator/manual/en/scripting/life-cycle-callbacks.html
  7. import { OpenActionType } from "../../../mk/system/UISystem";
  8. import TimeUtil from "../../../mk/utils/TimeUtil";
  9. export enum RedBagItemType {
  10. /** 开始界面 */
  11. StartUI = 0,
  12. /** 关卡界面 */
  13. LevelUI = 1
  14. }
  15. const { ccclass, property } = cc._decorator;
  16. /** [FC][V2.0.1]气泡红包新增脚本 */
  17. @ccclass
  18. export default class RedBagItem extends cc.Component {
  19. @property(cc.Label)
  20. public label_tip: cc.Label = null;
  21. @property(cc.Node)
  22. public node_qipaoBg: cc.Node = null;
  23. @property(cc.Node)
  24. public node_tip: cc.Node = null;
  25. @property({ type: cc.Enum(RedBagItemType), displayName: '气泡红包类型' })
  26. public type: number = 0;
  27. @property(cc.Integer)
  28. public index: number = 0;
  29. @property({ displayName: '点击回调', tooltip: "点击时触发", type: cc.Component.EventHandler })
  30. public onComplete: cc.Component.EventHandler[] = [];
  31. /** 存储的key */
  32. public stroageKey: string = "";
  33. // LIFE-CYCLE CALLBACKS:
  34. // onLoad () {}
  35. start() {
  36. this.node.on(cc.Node.EventType.TOUCH_START, this.onClick, this);
  37. this.init();
  38. }
  39. // update (dt) {}
  40. /** 初始化 */
  41. init() {
  42. //显示之后再初始化
  43. gData.redBagData.init();
  44. this.stroageKey = `RedBagItem${this.index}`
  45. let curDate = mk.time.getNowDayString(new Date().getTime());
  46. let lastDate = mk.time.getNowDayString(gData.gameData.gameData.lastTime * 1000);
  47. console.log("===[v2.0.1] curDate != lastDate", "curDate:" + curDate, "lastDate:" + lastDate, "curDate != lastDate:" + curDate != lastDate);
  48. if (curDate != lastDate) {
  49. mk.storage.setStorage(this.stroageKey, 0);
  50. }
  51. let ifClicked = mk.storage.getStorage(this.stroageKey);
  52. // console.log("===[v2.0.1] ifClicked && ifClicked == 1", ifClicked && ifClicked == 1, "=+++ " + ifClicked);
  53. /** 有值 且 为1(0 也会被判定为有值) */
  54. if (ifClicked && ifClicked == 1) {
  55. this.scheduleOnce(() => {
  56. this.hide();
  57. }, 0.01)
  58. }
  59. else {
  60. // console.log("===[v2.0.1]", "RedBagItem active" + this.node.active + "inxdex:" + this.index);
  61. gData.redBagData.addRedBagItem(this.type, this);
  62. }
  63. //开始界面或者游戏界面最后一个 则开始显示文案提示
  64. if (this.index == 2 || this.index == 6) {
  65. this.scheduleOnce(() => {
  66. gData.redBagData.showTip(this.type);
  67. }, 0.1);
  68. }
  69. }
  70. /** 重设 */
  71. reset() {
  72. mk.storage.setStorage(this.stroageKey, 0);
  73. }
  74. /** 被点击 */
  75. async onClick() {
  76. //如果未开启,则不操作
  77. if (!gData.redBagData.ifOpen) {
  78. await mk.ui.openPanel("module/reward/rewardLuck", OpenActionType.normal);
  79. this.callBack();
  80. }
  81. else {
  82. //倒计时未结束,则不操作
  83. if (this.type == RedBagItemType.StartUI) {
  84. if (gData.redBagData.curStartLeftTime >= 1) {
  85. mk.tip.pop("请等待倒计时结束之后领取红包");
  86. return;
  87. }
  88. }
  89. else if (this.type == RedBagItemType.LevelUI) {
  90. if (gData.redBagData.curLevelLeftTime >= 1) {
  91. mk.tip.pop("请等待倒计时结束之后领取红包");
  92. return;
  93. }
  94. }
  95. await mk.ui.openPanel("module/reward/rewardLuck", OpenActionType.normal);
  96. this.callBack();
  97. gData.redBagData.removeRedBagItem(this.type, this);
  98. gData.redBagData.startCountDown(this.type, gData.redBagData.countTotalTime);
  99. gData.redBagData.showTip(this.type, this.index);
  100. }
  101. }
  102. callBack() {
  103. const c_count = this.onComplete.length;
  104. for (let i = 0; i < c_count; i++) {
  105. if (this.onComplete[i].handler) this.onComplete[i].emit([])
  106. }
  107. }
  108. /** 倒计时 */
  109. countDown(time: string) {
  110. this.label_tip.string = time;
  111. this.node.color = cc.Color.GRAY;
  112. this.node_qipaoBg.color = cc.Color.GRAY;
  113. }
  114. /** 停止倒计时 */
  115. stopCountDown() {
  116. this.label_tip.string = "可领";
  117. this.node.color = cc.Color.WHITE;
  118. this.node_qipaoBg.color = cc.Color.WHITE;
  119. }
  120. /** 隐藏 */
  121. hide() {
  122. console.log("==[v2.0.1 index :" + this.index, "隐藏");
  123. this.node.active = false;
  124. if (this.node_tip.active) {
  125. this.node_tip.active = false;
  126. }
  127. }
  128. showTip() {
  129. this.node_tip.active = true;
  130. }
  131. }