const Api = require('../net/Api'); const DWTool = require("../utils/DWTool"); const TapTapTool = require("../utils/TapTapTool"); const itemList = require('../data/item'); const GameModule = require("../utils/GameModule"); const GameNotificationKey = require('../utils/GameEnum').GameNotificationKey; const QuestMainMissionType = require("../utils/GameEnum").QuestMainMissionType; cc.Class({ extends: cc.Component, properties: { questNode: cc.Node, questBtns: [cc.Node], starNode: { tooltip: '星星节点', default: [], type: [cc.Node] }, starFrames: { tooltip: '星星图片素材', default: [], type: [cc.SpriteFrame] }, progressLabel: { tooltip: '奖励数量', default: null, type: cc.Label }, progressBar: cc.ProgressBar, questTitle: { tooltip: '任务标题', default: null, type: cc.Label }, qusetMsg: { tooltip: '任务说明', default: null, type: cc.Label } }, onLoad () { this.giftMap = ['diamond', 'coin', 'ticket']; }, start () { }, init (parent, tasks, sn, questIndex) { this.parent = parent; this.quest = parent.quest; this.tasks = tasks; this.questIndex = questIndex; sn = this.sn = parseInt(sn); // 设置当前任务等级内容 this._setTaskItem(sn); }, _setTaskItem (sn) { let task = this.task = this.tasks.find(n => { if(sn == 0) { return n.sn == 1; } else { return n.sn == sn; } }) if(sn == this.tasks.length && task.status == 2) { this._setMaxItem(); } else { let level = sn == 0 ? 0 : sn - 1; let nameLevelMap = ['I', 'II', 'III', 'IV', 'V','Ⅵ','Ⅶ','Ⅷ','Ⅸ','Ⅹ']; // 设置当前任务标题 this.questTitle.string = `${task.name}(${nameLevelMap[level]})`; // 设置当前任务说明内容 let aimValue = TapTapTool.goldStrToClass(task.aimValue); let totalAimValue = TapTapTool.goldStrToClass(task.totalAimValue); if (aimValue.n == undefined && totalAimValue.n == undefined) { this.qusetMsg.string = task.msg.replace('${num}', totalAimValue); // 设置任务阶段进度条 let taskAimValue = aimValue > totalAimValue ? totalAimValue : aimValue; this._setProgress(taskAimValue, task.totalAimValue); } else { this.qusetMsg.string = task.msg.replace('${num}', TapTapTool.parseToString(totalAimValue)); // 设置任务阶段进度条 let taskAimValue = TapTapTool.compare(aimValue, totalAimValue) ? task.totalAimValue : task.aimValue; this._setProgress(taskAimValue, task.totalAimValue); } // 设置当前任务星级 this._setStarLevel(level); this.giftItem = Object.assign({ itemCount: task.itemCount }, itemList.find(n => { return n.id == task.itemId })); // 设置任务领取按钮 this._setGiftBtn(task.status); } }, _setMaxItem () { let task = this.tasks[4]; // 设置当前任务标题 this.questTitle.string = `${task.name}(V)`; // 设置当前任务星级 this._setStarLevel(this.tasks.length); // 设置当前任务说明内容 let aimValue = TapTapTool.goldStrToClass(task.aimValue); let totalAimValue = TapTapTool.goldStrToClass(task.totalAimValue); if (aimValue.n == undefined && totalAimValue.n == undefined) { this.qusetMsg.string = task.msg.replace('${num}', totalAimValue); // 设置任务阶段进度条 let taskAimValue = aimValue > totalAimValue ? totalAimValue : aimValue; this._setProgress(taskAimValue, task.totalAimValue); } else { this.qusetMsg.string = task.msg.replace('${num}', TapTapTool.parseToString(totalAimValue)); // 设置任务阶段进度条 let taskAimValue = TapTapTool.compare(aimValue, totalAimValue) ? task.totalAimValue : task.aimValue; this._setProgress(taskAimValue, task.totalAimValue); } // 设置任务领取按钮 this._setGiftBtn(QuestMainMissionType.AlreadyGet); }, /** * 领取按钮点击 */ handleGiftBtn () { this.questBtns[1].interactable = false; Api.httpPost({ url: "/task/gain.do", data: { taskId: this.task.taskId, sn: this.task.sn }, success: (res) => { this.questBtns[1].interactable = true; GameModule.audioMng.playGetAward(); if (res.clickMt) { let clickMt = TapTapTool.goldStrToClass(res.clickMt); if (GameModule.userInfo.perpetualClickMt.n != clickMt.n || GameModule.userInfo.perpetualClickMt.e != clickMt.e) { GameModule.userInfo.perpetualClickMt = clickMt; GameEvent.fire(GameNotificationKey.UpBuildingLevel); } } if (res.mt) { let mt = TapTapTool.goldStrToClass(res.mt); if (GameModule.userInfo.perpetualMt.n != mt.n || GameModule.userInfo.perpetualMt.e != mt.e) { GameModule.userInfo.perpetualMt = mt; GameModule.userInfo.refreshSecondText(); } } let updatRes = { coin: '0', diamond: 0, ticket: 0 } if(this.giftItem.id == 10001) { updatRes.coin = this.giftItem.itemCount; } if(this.giftItem.id == 10002) { updatRes.diamond = this.giftItem.itemCount; } if(this.giftItem.id == 10003) { updatRes.ticket = this.giftItem.itemCount; } // 更新全局userInfo this.quest.updateUserInfo(updatRes.coin, updatRes.diamond, updatRes.ticket); // 显示领取奖品动画 // let showFrame = this.giftItem.giftFrame; let itemCount = TapTapTool.goldStrToClass(this.giftItem.itemCount); let showText = `${this.giftItem.name} x ${itemCount}`; if (this.giftItem.giftFrame) { let showFrame = this.giftItem.giftFrame; this.quest.showMainGift(showFrame, showText); } // 领取成功,更新当前按钮状态到下一阶段 if(this.sn < this.tasks.length) { this.sn += 1; this._setTaskItem(this.sn); } else { this._setMaxItem(); } if (res.isCancel) { let isCancel = res.isCancel == 1 ? true : false; GameEvent.fire('quest_main_notice', isCancel); } }, fail: (errCode, errMsg) => { this.questBtns[1].interactable = true; // 领取失败 Global.commonAlert.showCommonErrorAlert("领取奖励失败"); } }) }, /** * 设置任务领取状态按钮 * @param {number} status 任务状态[0 : 未完成, 1 : 完成可领取, 2 : 完成已领取] */ _setGiftBtn (status) { // console.log(this.giftItem); this.questBtns.forEach(n => { n.active = false; }); let btn // 根据状态选择按钮 switch (status) { case QuestMainMissionType.NoFinished: btn = this.questBtns[0]; break; case QuestMainMissionType.CanGain: btn = this.questBtns[1]; break; case QuestMainMissionType.AlreadyGet: btn = this.questBtns[2]; break; default: break; } btn.active = true; // 设置图标和数量 if(status != QuestMainMissionType.AlreadyGet) { let countNode = cc.find('/awardNode/count_label', btn); let spriteNode = cc.find('/awardNode/sprite', btn); let awardLabelNode = cc.find('/awardLabel', btn); let awardNode = cc.find('/awardNode', btn); let otherNode = cc.find('/otherLabel', btn); var awardString = '奖励'; if (status == QuestMainMissionType.CanGain) { awardString = '获得'; } if (this.giftItem.id > 10002) { awardNode.active = false; otherNode.active = true; awardString = this.giftItem.wordInfo; otherNode.getComponent('cc.Label').string = `提升 ${DWTool.coinParseNoFixed(this.giftItem.itemCount)}倍`; } else { awardNode.active = true; otherNode.active = false; countNode.getComponent('cc.Label').string = `${DWTool.coinParseNoFixed(this.giftItem.itemCount)}`; if (this.giftItem.picId) { DWTool.loadResSpriteFrame(`./textures/taskItem/task_${this.giftItem.picId}`) .then((spriteFrame) => { spriteNode.getComponent('cc.Sprite').spriteFrame = spriteFrame; }).catch((err) => { console.log(err); }); DWTool.loadResSpriteFrame(`./textures/item/${this.giftItem.id}`) .then((spriteFrame) => { this.giftItem.giftFrame = spriteFrame; }).catch((err) => { console.log(err); }); } } awardLabelNode.getComponent('cc.Label').string = awardString; } DWTool.loadResSpriteFrame(`./textures/quest/altas/${this.task.picId}`) .then((spriteFrame) => { this.questNode.getComponent('cc.Sprite').spriteFrame = spriteFrame; }).catch((err) => { console.log(err); }); }, /** * 设置任务阶段进度条 * @param {number} current 当前值 * @param {number} total 阶段总值 */ _setProgress (current, total) { let _progress = 0; let newCurrent = TapTapTool.goldStrToClass(current + ''); let newTotal = TapTapTool.goldStrToClass(total + ''); /// 说明是正常的数字 if (newCurrent.n == undefined && newTotal.n == undefined) { _progress = (newCurrent / newTotal).toFixed(1); this.progressBar.progress = _progress; this.progressLabel.string = `${current}/${total}`; } else { if (newCurrent == NaN) { newCurrent = {'n': 0, 'e': 0}; } _progress = TapTapTool.divisionToRatio(newCurrent, newTotal).toFixed(1); this.progressBar.progress = _progress; this.progressLabel.string = `${TapTapTool.parseToString(newCurrent)}/${TapTapTool.parseToString(newTotal)}`; } }, /** * 设置当前任务星级 * @param {number} level 星级 */ _setStarLevel (level) { if (level >= 5) { this.starNode.forEach((node, index) => { if((index + 5) < level) { node.getComponent('cc.Sprite').spriteFrame = this.starFrames[3]; } else { node.getComponent('cc.Sprite').spriteFrame = this.starFrames[2]; } }); } else { this.starNode.forEach((node, index) => { if(index < level) { node.getComponent('cc.Sprite').spriteFrame = this.starFrames[1]; } else { node.getComponent('cc.Sprite').spriteFrame = this.starFrames[0]; } }); } } });