| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244 |
- // 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, { RedBagItemType } from "../../module/redBagItem/RedBagItem";
- const { ccclass, property } = cc._decorator;
- /**[FC][V2.0.1]气泡红包数据 */
- @ccclass
- export default class RedBagData extends cc.Component {
- /** 是否初始化了 */
- public ifInit: boolean = false;
- /** 是否开启 */
- public ifOpen: boolean = false;
- /** 正在计时 */
- public isCounting: boolean = false;
- /** 总的倒计时时间 */
- public countTotalTime: number = 300;
- /** 开始界面 */
- private startRedBagItemArr: RedBagItem[] = [];
- /** 开始界面倒计时剩余时间 */
- public curStartLeftTime: number = 0;
- /** 开始界面倒计时的时间 */
- private storageKey_startCountEndTime: string = "startCountEndTime";
- /** 关卡界面 */
- private levelRedBagItemArr: RedBagItem[] = [];
- /** 关卡界面倒计时剩余时间 */
- public curLevelLeftTime: number = 0;
- /** 关卡界面倒计时的时间 */
- private storageKey_levelCountEndTime: string = "levelCountEndTime";
- /**初始化 */
- public init() {
- if (this.ifInit) {
- return;
- }
- this.ifInit = true;
- this.ifOpen = gData.gameData.configs.GlobalCfg.redBagLimitSwitch == 0 ? false : true;
- console.log("===[v2.0.1] 初始化 switch", gData.gameData.configs.GlobalCfg.redBagLimitSwitch, gData.gameData.configs.GlobalCfg.levelRedBagSwitch);
- if (this.ifOpen) {
- this.checkOfflineTime();
- }
- else {
- mk.storage.setStorage(this.storageKey_startCountEndTime, null);
- mk.storage.setStorage(this.storageKey_levelCountEndTime, null);
- }
- }
- //数组操作相关-----------------------------------------------------------------------------------
- /** 添加进数组
- * @param type 气泡红包类型
- * @param redBadItem 气泡红包脚本本身
- */
- public addRedBagItem(type: RedBagItemType, redBadItem: RedBagItem) {
- if (type == RedBagItemType.StartUI) {
- this.startRedBagItemArr.push(redBadItem);
- }
- else if (type == RedBagItemType.LevelUI) {
- this.levelRedBagItemArr.push(redBadItem);
- }
- }
- /** 从数组中移除
- * @param type 气泡红包类型
- * @param redBadItem 气泡红包脚本本身
- */
- public removeRedBagItem(type: RedBagItemType, redBagItem: RedBagItem) {
- let redBagItemArr: RedBagItem[] = [];
- if (type == RedBagItemType.StartUI) {
- redBagItemArr = this.startRedBagItemArr;
- }
- else if (type == RedBagItemType.LevelUI) {
- redBagItemArr = this.levelRedBagItemArr;
- }
- if (redBagItemArr.length > 1) {
- let index = redBagItemArr.indexOf(redBagItem);
- if (index != -1) {
- redBagItemArr.splice(index, 1);
- }
- redBagItem.hide();
- mk.storage.setStorage(redBagItem.stroageKey, 1);
- }
- }
- //倒计时相关---------------------------------------------------------------------------------------
- /** 检测离线时间 */
- public checkOfflineTime() {
- let curDate = new Date().getTime();
- let startCountEndDate = mk.storage.getStorage(this.storageKey_startCountEndTime);
- // console.log("===[v2.0.1] lastDate", lastDate);
- if (startCountEndDate) {
- let gapTime = Math.floor((startCountEndDate - curDate) * 0.001);
- if (gapTime >= 5) {
- this.startCountDown(RedBagItemType.StartUI, gapTime);
- }
- else {
- }
- }
- else {
- }
- let levelCountEndDate = mk.storage.getStorage(this.storageKey_levelCountEndTime);
- if (levelCountEndDate) {
- let gapTime = Math.floor((levelCountEndDate - curDate) * 0.001);
- if (gapTime >= 5) {
- this.startCountDown(RedBagItemType.LevelUI, gapTime);
- }
- else {
- }
- }
- else {
- }
- }
- /** 开始倒计时 */
- public startCountDown(type: RedBagItemType, leftTime: number) {
- console.log("===[v2.0.1 type", RedBagItemType[type], leftTime);
- if (type == RedBagItemType.StartUI) {
- this.curStartLeftTime = leftTime;
- let countEnddate = new Date().getTime() + leftTime * 1000;
- mk.storage.setStorage(this.storageKey_startCountEndTime, countEnddate);
- }
- else if (type == RedBagItemType.LevelUI) {
- this.curLevelLeftTime = leftTime;
- let countEnddate = new Date().getTime() + leftTime * 1000;
- mk.storage.setStorage(this.storageKey_levelCountEndTime, countEnddate);
- }
- if (!this.isCounting) {
- this.isCounting = true;
- this.schedule(this.countDown, 1);
- }
- }
- /** 倒计时 */
- countDown() {
- if (this.curStartLeftTime > 0) {
- let time = --this.curStartLeftTime;
- if (time <= 0) {
- this.curStartLeftTime = 0;
- this.stopCountDown(RedBagItemType.StartUI);
- return;
- }
- let timeStr = mk.time.format(time, "m:s");
- for (var i = 0; i < this.startRedBagItemArr.length; i++) {
- let redBagItem: RedBagItem = this.startRedBagItemArr[i];
- redBagItem.countDown(timeStr);
- }
- }
- if (this.curLevelLeftTime > 0) {
- let time = --this.curLevelLeftTime;
- if (time <= 0) {
- this.curLevelLeftTime = 0;
- this.stopCountDown(RedBagItemType.LevelUI);
- return;
- }
- let timeStr = mk.time.format(time, "m:s");
- for (var i = 0; i < this.levelRedBagItemArr.length; i++) {
- let redBagItem: RedBagItem = this.levelRedBagItemArr[i];
- redBagItem.countDown(timeStr);
- }
- }
- }
- /** 结束倒计时 */
- public stopCountDown(type: RedBagItemType) {
- if (type == RedBagItemType.StartUI) {
- for (var i = 0; i < this.startRedBagItemArr.length; i++) {
- let redBagItem: RedBagItem = this.startRedBagItemArr[i];
- redBagItem.stopCountDown();
- }
- mk.storage.setStorage(this.storageKey_startCountEndTime, null);
- }
- else if (type == RedBagItemType.LevelUI) {
- for (var i = 0; i < this.levelRedBagItemArr.length; i++) {
- let redBagItem: RedBagItem = this.levelRedBagItemArr[i];
- redBagItem.stopCountDown();
- }
- mk.storage.setStorage(this.storageKey_levelCountEndTime, null);
- }
- if (this.curStartLeftTime <= 0 && this.curLevelLeftTime <= 0) {
- this.isCounting = false;
- this.unschedule(this.countDown);
- }
- }
- reset() {
- for (var i = 0; i < this.startRedBagItemArr.length; i++) {
- let redBagItem: RedBagItem = this.startRedBagItemArr[i];
- redBagItem.reset();
- }
- for (var i = 0; i < this.levelRedBagItemArr.length; i++) {
- let redBagItem: RedBagItem = this.levelRedBagItemArr[i];
- redBagItem.reset();
- }
- }
- public curShowTipIndex: number[] = [];
- public showTip(type: RedBagItemType, redBagItemIndex: number = -1) {
- //如果未点中
- if (redBagItemIndex != -1) {
- if (this.curShowTipIndex.indexOf(redBagItemIndex) == -1) {
- return;
- }
- }
- let redBagItemArr: RedBagItem[] = [];
- if (type == RedBagItemType.StartUI) {
- redBagItemArr = this.startRedBagItemArr;
- }
- else if (type == RedBagItemType.LevelUI) {
- redBagItemArr = this.levelRedBagItemArr;
- }
- let length = redBagItemArr.length;
- let index = mk.math.random(0, length);
- let redBagItem: RedBagItem = redBagItemArr[index];
- if (redBagItem) {
- redBagItem.showTip();
- this.curShowTipIndex.push(redBagItem.index);
- }
- }
- public showNewTip(curIndex: number) {
- }
- }
|