|
|
@@ -0,0 +1,131 @@
|
|
|
+// Learn TypeScript:
|
|
|
+// - https://docs.cocos.com/creator/manual/en/scripting/typescript.html
|
|
|
+// Learn Attribute:
|
|
|
+// - https://docs.cocos.com/creator/manual/en/scripting/reference/attributes.html
|
|
|
+// Learn life-cycle callbacks:
|
|
|
+// - https://docs.cocos.com/creator/manual/en/scripting/life-cycle-callbacks.html
|
|
|
+
|
|
|
+import { Timer } from "../../../mk/system/TimerSystem";
|
|
|
+import RedBagItem from "../../module/redBagItem/RedBagItem";
|
|
|
+
|
|
|
+const { ccclass, property } = cc._decorator;
|
|
|
+
|
|
|
+/**气泡数据 */
|
|
|
+@ccclass
|
|
|
+export default class RedBagData extends cc.Component {
|
|
|
+
|
|
|
+ /** 是否开启 */
|
|
|
+ public ifOpen: boolean = false;
|
|
|
+ /** 倒计时时间 */
|
|
|
+ public countTime: number = 300;
|
|
|
+ private storageKey_startCountTime: string = "startCountTime";
|
|
|
+ // LIFE-CYCLE CALLBACKS:
|
|
|
+
|
|
|
+ /** 是否初始化了 */
|
|
|
+ public ifInit: boolean = false;
|
|
|
+ /** 气泡红包列表 */
|
|
|
+ private redBagItemArr: RedBagItem[] = [];
|
|
|
+ /** 当前倒计时剩余时间 */
|
|
|
+ public curLeftTime: number = 0;
|
|
|
+
|
|
|
+ /**
|
|
|
+ *
|
|
|
+ */
|
|
|
+ constructor() {
|
|
|
+ super();
|
|
|
+ }
|
|
|
+
|
|
|
+ /**初始化 */
|
|
|
+ public init() {
|
|
|
+
|
|
|
+ if (this.ifInit) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ console.log("===[v2.0.1] 初始化");
|
|
|
+ this.ifInit = true;
|
|
|
+ this.checkOfflineTime();
|
|
|
+ }
|
|
|
+
|
|
|
+ /** 检测离线时间 */
|
|
|
+ public checkOfflineTime() {
|
|
|
+ let curDate = new Date().getTime();
|
|
|
+ let lastDate = mk.storage.getStorage(this.storageKey_startCountTime);
|
|
|
+ console.log("===[v2.0.1] lastDat", lastDate);
|
|
|
+ if (lastDate) {
|
|
|
+ let gapTime = Math.floor((curDate - lastDate) * 0.001);
|
|
|
+ console.log("===[v2.0.1] gapTime", gapTime);
|
|
|
+ let leftTime = Math.floor(this.countTime - gapTime);
|
|
|
+ console.log("===[v2.0.1] leftTime", leftTime);
|
|
|
+ if (leftTime >= 5) {
|
|
|
+ this.curLeftTime = leftTime;
|
|
|
+ this.startCountDown(this.curLeftTime);
|
|
|
+ }
|
|
|
+ else {
|
|
|
+
|
|
|
+ }
|
|
|
+ }
|
|
|
+ else {
|
|
|
+
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /** 添加进数组 */
|
|
|
+ public addRedBagItem(redBadItem: RedBagItem) {
|
|
|
+ this.redBagItemArr.push(redBadItem);
|
|
|
+ }
|
|
|
+
|
|
|
+ /** 从数组中移除 */
|
|
|
+ public removeRedBagItem(redBagItem: RedBagItem) {
|
|
|
+ if (this.redBagItemArr.length >= 1) {
|
|
|
+ let index = this.redBagItemArr.indexOf(redBagItem);
|
|
|
+ if (index != -1) {
|
|
|
+ this.redBagItemArr.splice(index, 1);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ else {
|
|
|
+
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /** 开始倒计时 */
|
|
|
+ public startCountDown(leftTime: number) {
|
|
|
+ this.curLeftTime = leftTime;
|
|
|
+ mk.tip.pop("气泡红包数据开始倒计时");
|
|
|
+ let date = new Date().getTime();
|
|
|
+ mk.storage.setStorage(this.storageKey_startCountTime, date);
|
|
|
+
|
|
|
+ this.schedule(this.countDown, 1);
|
|
|
+ }
|
|
|
+
|
|
|
+ /** 倒计时 */
|
|
|
+ countDown() {
|
|
|
+ let time = --this.curLeftTime;
|
|
|
+ if (time <= 0) {
|
|
|
+ this.curLeftTime = 0;
|
|
|
+ this.stopCountDown();
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ let timeStr = mk.time.format(time, "m:s");
|
|
|
+ for (var i = 0; i < this.redBagItemArr.length; i++) {
|
|
|
+ let redBagItem: RedBagItem = this.redBagItemArr[i];
|
|
|
+ redBagItem.countDown(timeStr);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /** 结束倒计时 */
|
|
|
+ public stopCountDown() {
|
|
|
+ this.unschedule(this.countDown);
|
|
|
+ for (var i = 0; i < this.redBagItemArr.length; i++) {
|
|
|
+ let redBagItem: RedBagItem = this.redBagItemArr[i];
|
|
|
+ redBagItem.stopCountDown();
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public showTip() {
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ public showNewTip(curIndex: number) {
|
|
|
+
|
|
|
+ }
|
|
|
+}
|