RedBagData.ts 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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 { Timer } from "../../../mk/system/TimerSystem";
  8. import RedBagItem from "../../module/redBagItem/RedBagItem";
  9. const { ccclass, property } = cc._decorator;
  10. /**气泡数据 */
  11. @ccclass
  12. export default class RedBagData extends cc.Component {
  13. /** 是否开启 */
  14. public ifOpen: boolean = false;
  15. /** 倒计时时间 */
  16. public countTime: number = 300;
  17. private storageKey_startCountTime: string = "startCountTime";
  18. // LIFE-CYCLE CALLBACKS:
  19. /** 是否初始化了 */
  20. public ifInit: boolean = false;
  21. /** 气泡红包列表 */
  22. private redBagItemArr: RedBagItem[] = [];
  23. /** 当前倒计时剩余时间 */
  24. public curLeftTime: number = 0;
  25. /**
  26. *
  27. */
  28. constructor() {
  29. super();
  30. }
  31. /**初始化 */
  32. public init() {
  33. if (this.ifInit) {
  34. return;
  35. }
  36. console.log("===[v2.0.1] 初始化");
  37. this.ifInit = true;
  38. this.checkOfflineTime();
  39. }
  40. /** 检测离线时间 */
  41. public checkOfflineTime() {
  42. let curDate = new Date().getTime();
  43. let lastDate = mk.storage.getStorage(this.storageKey_startCountTime);
  44. console.log("===[v2.0.1] lastDat", lastDate);
  45. if (lastDate) {
  46. let gapTime = Math.floor((curDate - lastDate) * 0.001);
  47. console.log("===[v2.0.1] gapTime", gapTime);
  48. let leftTime = Math.floor(this.countTime - gapTime);
  49. console.log("===[v2.0.1] leftTime", leftTime);
  50. if (leftTime >= 5) {
  51. this.curLeftTime = leftTime;
  52. this.startCountDown(this.curLeftTime);
  53. }
  54. else {
  55. }
  56. }
  57. else {
  58. }
  59. }
  60. /** 添加进数组 */
  61. public addRedBagItem(redBadItem: RedBagItem) {
  62. this.redBagItemArr.push(redBadItem);
  63. }
  64. /** 从数组中移除 */
  65. public removeRedBagItem(redBagItem: RedBagItem) {
  66. if (this.redBagItemArr.length >= 1) {
  67. let index = this.redBagItemArr.indexOf(redBagItem);
  68. if (index != -1) {
  69. this.redBagItemArr.splice(index, 1);
  70. }
  71. }
  72. else {
  73. }
  74. }
  75. /** 开始倒计时 */
  76. public startCountDown(leftTime: number) {
  77. this.curLeftTime = leftTime;
  78. mk.tip.pop("气泡红包数据开始倒计时");
  79. let date = new Date().getTime();
  80. mk.storage.setStorage(this.storageKey_startCountTime, date);
  81. this.schedule(this.countDown, 1);
  82. }
  83. /** 倒计时 */
  84. countDown() {
  85. let time = --this.curLeftTime;
  86. if (time <= 0) {
  87. this.curLeftTime = 0;
  88. this.stopCountDown();
  89. return;
  90. }
  91. let timeStr = mk.time.format(time, "m:s");
  92. for (var i = 0; i < this.redBagItemArr.length; i++) {
  93. let redBagItem: RedBagItem = this.redBagItemArr[i];
  94. redBagItem.countDown(timeStr);
  95. }
  96. }
  97. /** 结束倒计时 */
  98. public stopCountDown() {
  99. this.unschedule(this.countDown);
  100. for (var i = 0; i < this.redBagItemArr.length; i++) {
  101. let redBagItem: RedBagItem = this.redBagItemArr[i];
  102. redBagItem.stopCountDown();
  103. }
  104. }
  105. public showTip() {
  106. }
  107. public showNewTip(curIndex: number) {
  108. }
  109. }