RedBagData.ts 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  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, { RedBagItemType } from "../../module/redBagItem/RedBagItem";
  9. const { ccclass, property } = cc._decorator;
  10. /**[FC][V2.0.1]气泡红包数据 */
  11. @ccclass
  12. export default class RedBagData extends cc.Component {
  13. /** 是否初始化了 */
  14. public ifInit: boolean = false;
  15. /** 是否开启 */
  16. public ifOpen: boolean = false;
  17. /** 正在计时 */
  18. public isCounting: boolean = false;
  19. /** 总的倒计时时间 */
  20. public countTotalTime: number = 300;
  21. /** 开始界面 */
  22. private startRedBagItemArr: RedBagItem[] = [];
  23. /** 开始界面倒计时剩余时间 */
  24. public curStartLeftTime: number = 0;
  25. /** 开始界面倒计时的时间 */
  26. private storageKey_startCountEndTime: string = "startCountEndTime";
  27. /** 关卡界面 */
  28. private levelRedBagItemArr: RedBagItem[] = [];
  29. /** 关卡界面倒计时剩余时间 */
  30. public curLevelLeftTime: number = 0;
  31. /** 关卡界面倒计时的时间 */
  32. private storageKey_levelCountEndTime: string = "levelCountEndTime";
  33. /**初始化 */
  34. public init() {
  35. if (this.ifInit) {
  36. return;
  37. }
  38. this.ifInit = true;
  39. this.ifOpen = gData.gameData.configs.GlobalCfg.redBagLimitSwitch == 0 ? false : true;
  40. console.log("===[v2.0.1] 初始化 switch", gData.gameData.configs.GlobalCfg.redBagLimitSwitch, gData.gameData.configs.GlobalCfg.levelRedBagSwitch);
  41. if (this.ifOpen) {
  42. this.checkOfflineTime();
  43. }
  44. else {
  45. mk.storage.setStorage(this.storageKey_startCountEndTime, null);
  46. mk.storage.setStorage(this.storageKey_levelCountEndTime, null);
  47. }
  48. }
  49. //数组操作相关-----------------------------------------------------------------------------------
  50. /** 添加进数组
  51. * @param type 气泡红包类型
  52. * @param redBadItem 气泡红包脚本本身
  53. */
  54. public addRedBagItem(type: RedBagItemType, redBadItem: RedBagItem) {
  55. if (type == RedBagItemType.StartUI) {
  56. this.startRedBagItemArr.push(redBadItem);
  57. }
  58. else if (type == RedBagItemType.LevelUI) {
  59. this.levelRedBagItemArr.push(redBadItem);
  60. }
  61. }
  62. /** 从数组中移除
  63. * @param type 气泡红包类型
  64. * @param redBadItem 气泡红包脚本本身
  65. */
  66. public removeRedBagItem(type: RedBagItemType, redBagItem: RedBagItem) {
  67. let redBagItemArr: RedBagItem[] = [];
  68. if (type == RedBagItemType.StartUI) {
  69. redBagItemArr = this.startRedBagItemArr;
  70. }
  71. else if (type == RedBagItemType.LevelUI) {
  72. redBagItemArr = this.levelRedBagItemArr;
  73. }
  74. if (redBagItemArr.length > 1) {
  75. let index = redBagItemArr.indexOf(redBagItem);
  76. if (index != -1) {
  77. redBagItemArr.splice(index, 1);
  78. }
  79. redBagItem.hide();
  80. mk.storage.setStorage(redBagItem.stroageKey, 1);
  81. }
  82. }
  83. //倒计时相关---------------------------------------------------------------------------------------
  84. /** 检测离线时间 */
  85. public checkOfflineTime() {
  86. let curDate = new Date().getTime();
  87. let startCountEndDate = mk.storage.getStorage(this.storageKey_startCountEndTime);
  88. // console.log("===[v2.0.1] lastDate", lastDate);
  89. if (startCountEndDate) {
  90. let gapTime = Math.floor((startCountEndDate - curDate) * 0.001);
  91. if (gapTime >= 5) {
  92. this.startCountDown(RedBagItemType.StartUI, gapTime);
  93. }
  94. else {
  95. }
  96. }
  97. else {
  98. }
  99. let levelCountEndDate = mk.storage.getStorage(this.storageKey_levelCountEndTime);
  100. if (levelCountEndDate) {
  101. let gapTime = Math.floor((levelCountEndDate - curDate) * 0.001);
  102. if (gapTime >= 5) {
  103. this.startCountDown(RedBagItemType.LevelUI, gapTime);
  104. }
  105. else {
  106. }
  107. }
  108. else {
  109. }
  110. }
  111. /** 开始倒计时 */
  112. public startCountDown(type: RedBagItemType, leftTime: number) {
  113. console.log("===[v2.0.1 type", RedBagItemType[type], leftTime);
  114. if (type == RedBagItemType.StartUI) {
  115. this.curStartLeftTime = leftTime;
  116. let countEnddate = new Date().getTime() + leftTime * 1000;
  117. mk.storage.setStorage(this.storageKey_startCountEndTime, countEnddate);
  118. }
  119. else if (type == RedBagItemType.LevelUI) {
  120. this.curLevelLeftTime = leftTime;
  121. let countEnddate = new Date().getTime() + leftTime * 1000;
  122. mk.storage.setStorage(this.storageKey_levelCountEndTime, countEnddate);
  123. }
  124. if (!this.isCounting) {
  125. this.isCounting = true;
  126. this.schedule(this.countDown, 1);
  127. }
  128. }
  129. /** 倒计时 */
  130. countDown() {
  131. if (this.curStartLeftTime > 0) {
  132. let time = --this.curStartLeftTime;
  133. if (time <= 0) {
  134. this.curStartLeftTime = 0;
  135. this.stopCountDown(RedBagItemType.StartUI);
  136. return;
  137. }
  138. let timeStr = mk.time.format(time, "m:s");
  139. for (var i = 0; i < this.startRedBagItemArr.length; i++) {
  140. let redBagItem: RedBagItem = this.startRedBagItemArr[i];
  141. redBagItem.countDown(timeStr);
  142. }
  143. }
  144. if (this.curLevelLeftTime > 0) {
  145. let time = --this.curLevelLeftTime;
  146. if (time <= 0) {
  147. this.curLevelLeftTime = 0;
  148. this.stopCountDown(RedBagItemType.LevelUI);
  149. return;
  150. }
  151. let timeStr = mk.time.format(time, "m:s");
  152. for (var i = 0; i < this.levelRedBagItemArr.length; i++) {
  153. let redBagItem: RedBagItem = this.levelRedBagItemArr[i];
  154. redBagItem.countDown(timeStr);
  155. }
  156. }
  157. }
  158. /** 结束倒计时 */
  159. public stopCountDown(type: RedBagItemType) {
  160. if (type == RedBagItemType.StartUI) {
  161. for (var i = 0; i < this.startRedBagItemArr.length; i++) {
  162. let redBagItem: RedBagItem = this.startRedBagItemArr[i];
  163. redBagItem.stopCountDown();
  164. }
  165. mk.storage.setStorage(this.storageKey_startCountEndTime, null);
  166. }
  167. else if (type == RedBagItemType.LevelUI) {
  168. for (var i = 0; i < this.levelRedBagItemArr.length; i++) {
  169. let redBagItem: RedBagItem = this.levelRedBagItemArr[i];
  170. redBagItem.stopCountDown();
  171. }
  172. mk.storage.setStorage(this.storageKey_levelCountEndTime, null);
  173. }
  174. if (this.curStartLeftTime <= 0 && this.curLevelLeftTime <= 0) {
  175. this.isCounting = false;
  176. this.unschedule(this.countDown);
  177. }
  178. }
  179. reset() {
  180. for (var i = 0; i < this.startRedBagItemArr.length; i++) {
  181. let redBagItem: RedBagItem = this.startRedBagItemArr[i];
  182. redBagItem.reset();
  183. }
  184. for (var i = 0; i < this.levelRedBagItemArr.length; i++) {
  185. let redBagItem: RedBagItem = this.levelRedBagItemArr[i];
  186. redBagItem.reset();
  187. }
  188. }
  189. public curShowTipIndex: number[] = [];
  190. public showTip(type: RedBagItemType, redBagItemIndex: number = -1) {
  191. //如果未点中
  192. if (redBagItemIndex != -1) {
  193. if (this.curShowTipIndex.indexOf(redBagItemIndex) == -1) {
  194. return;
  195. }
  196. }
  197. let redBagItemArr: RedBagItem[] = [];
  198. if (type == RedBagItemType.StartUI) {
  199. redBagItemArr = this.startRedBagItemArr;
  200. }
  201. else if (type == RedBagItemType.LevelUI) {
  202. redBagItemArr = this.levelRedBagItemArr;
  203. }
  204. let length = redBagItemArr.length;
  205. let index = mk.math.random(0, length);
  206. let redBagItem: RedBagItem = redBagItemArr[index];
  207. if (redBagItem) {
  208. redBagItem.showTip();
  209. this.curShowTipIndex.push(redBagItem.index);
  210. }
  211. }
  212. public showNewTip(curIndex: number) {
  213. }
  214. }