RedBagItem.ts 5.0 KB

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