const QuestApi = require('../net/QuestApi'); const DWTool = require("../utils/DWTool"); const QuestDailyMissionType = require("../utils/GameEnum").QuestDailyMissionType; const GameModule = require('../utils/GameModule'); cc.Class({ extends: cc.Component, properties: { questSprite: cc.Sprite, questBtns: [cc.Node], giftFrames: { tooltip: '奖励图片素材', default: [], type: [cc.SpriteFrame] }, questTitle: { tooltip: '任务标题', default: null, type: cc.Label }, questDetail: { tooltip: '任务内容', default: null, type: cc.Label }, progressLabel: { tooltip: '奖励数量', default: null, type: cc.Label }, progressBar: cc.ProgressBar, }, onLoad () { }, start () { }, init (parent, task, questIndex) { this.giftMap = ['diamond', 'coin', 'ticket']; this.parent = parent; this.quest = parent.quest; this.task = task; this.questIndex = questIndex; this.questBtns[task.state].active = true; DWTool.loadResSpriteFrame(`./textures/quest/altas/${task.picId}`) .then((spriteFrame) => { this.questSprite.spriteFrame = spriteFrame; }).catch((err) => { console.log(err); }); // 设置任务标题 this.questTitle.string = task.title; // 设置任务说明内容 this.questDetail.string = task.desc; // 设置任务奖励类型及数量 this._setGiftBtn(); //设置当前进度 this._setProgress(); }, /** * 设置任务奖励类型及数量 */ _setGiftBtn () { this.questBtns.forEach(n => { n.active = false; }); let btn; // 根据状态选择按钮 switch (this.task.state) { case QuestDailyMissionType.NoFinished: btn = this.questBtns[1]; break; case QuestDailyMissionType.CanGain: btn = this.questBtns[0]; break; case QuestDailyMissionType.AlreadyGet: btn = this.questBtns[2]; break; default: break; } btn.active = true; // 设置图标和数量 if(this.task.state != QuestDailyMissionType.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); // this.giftItem.giftFrame = spriteFrame; var awardString = '奖励'; if (this.task.state == QuestDailyMissionType.CanGain) { awardString = '获得'; } let _count = 0; let _type = ''; let _sprite = null; this.giftMap.forEach((value, index) => { if(this.task[value] > 0) { _count = this.task[value]; _type = value; _sprite = this.giftFrames[index]; } }); awardNode.active = true; otherNode.active = false; spriteNode.getComponent('cc.Sprite').spriteFrame = _sprite; countNode.getComponent('cc.Label').string = `${DWTool.coinParseNoFixed(_count)}`; awardLabelNode.getComponent('cc.Label').string = awardString; } }, /** * 设置任务阶段进度条 * @param {number} current 当前值 * @param {number} total 阶段总值 */ _setProgress () { let total = this.task.totalCount; let current = this.task.userCount; let _progress = (current / total).toFixed(1); this.progressBar.progress = _progress; if (current > total) { this.progressLabel.string = `${total}/${total}`; } else { this.progressLabel.string = `${current}/${total}`; } }, /** * 领取按钮点击 */ handleGiftBtn () { this.questBtns[0].interactable = false; QuestApi.postGetDailyTaskReward(this.task.id, (res) => { this.questBtns[0].interactable = true; GameModule.audioMng.playGetAward(); // 更新全局userInfo this.quest.updateUserInfo(this.task.coin, this.task.diamond, this.task.ticket); this.task.state = QuestDailyMissionType.AlreadyGet; // 领取成功,更新状态 this.questBtns[QuestDailyMissionType.CanGain].active = false; this.questBtns[QuestDailyMissionType.AlreadyGet].active = true; // 显示领取奖品动画 this.quest.showActGift({ ticket: this.task.ticket, diamond: this.task.diamond, coin: this.task.coin }); this.parent.updateUserDegree(); if (res.isCancel) { let isCancel = res.isCancel == 1 ? true : false; GameEvent.fire('quest_daily_notice', isCancel); } }, (errCode, errMsg) => { this.questBtns[0].interactable = true; // 领取失败 GameGlobal.commonAlert.showCommonErrorAlert("领取奖励失败"); }); } });