| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131 |
- // 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) {
- }
- }
|