const Api = require('../net/Api'); cc.Class({ extends: cc.Component, properties: { questNode: cc.Node, questFrames: { tooltip: '任务图标', default: [], type: [cc.SpriteFrame], }, questBtns: [cc.Node], starNode: { tooltip: '星星节点', default: [], type: [cc.Node] }, giftFrames: { tooltip: '奖励图片素材', default: [], type: [cc.SpriteFrame] }, 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.questNode.getComponent('cc.Sprite').spriteFrame = this.questFrames[this.questIndex] // 设置当前任务等级内容 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 == 5 && task.status == 2) { this._setMaxItem() } else { let level = sn == 0 ? 0 : sn - 1 let nameLevelMap = ['I', 'II', 'III', 'IV', 'V'] let giftType let giftCount this.giftMap.forEach(n => { if(task[n] > 0) { giftType = n giftCount = task[n] } }) // 设置当前任务标题 this.questTitle.string = `${task.name}(${nameLevelMap[level]})` // 设置当前任务说明内容 this.qusetMsg.string = task.msg.replace('${num}', task.totalAimValue) // 设置当前任务星级 this._setStarLevel(level) // 设置任务阶段进度条 let aimValue = task.aimValue > task.totalAimValue ? task.totalAimValue : task.aimValue this._setProgress(aimValue, task.totalAimValue) // 设置任务领取按钮 this._setGiftBtn(task.status, giftType, giftCount) } }, _setMaxItem () { let task = this.tasks[4] // 设置当前任务标题 this.questTitle.string = `${task.name}(V)` // 设置当前任务说明内容 this.qusetMsg.string = task.msg.replace('${num}', task.totalAimValue) // 设置当前任务星级 this._setStarLevel(5) // 设置任务阶段进度条 let aimValue = task.aimValue > task.totalAimValue ? task.totalAimValue : task.aimValue this._setProgress(aimValue, task.totalAimValue) // 设置任务领取按钮 this._setGiftBtn(2) }, /** * 领取按钮点击 */ handleGiftBtn () { Api.httpPost({ url: "/task/gain.do", data: { taskId: this.task.taskId, sn: this.task.sn }, success: (res) => { // 更新全局userInfo this.quest.updateUserInfo(this.task.coin, this.task.diamond, this.task.ticket) // 显示领取奖品动画 this.quest.showActGift({ ticket: this.task.ticket, diamond: this.task.diamond, coin: this.task.coin }) // 领取成功,更新当前按钮状态到下一阶段 if(this.sn < 5) { this.sn += 1 this._setTaskItem(this.sn) } else { this._setMaxItem() } }, fail: () => { // 领取失败 } }) }, /** * 设置任务领取状态按钮 * @param {number} status 任务状态[0 : 未完成, 1 : 完成可领取, 2 : 完成已领取] * @param {string} giftType 奖励类型[ticket: 艺人券, diamond: 钻石, coin: 金币] * @param {number} giftCount 奖励数量 */ _setGiftBtn (status, giftType, giftCount) { this.questBtns.forEach(n => { n.active = false }) let btn // 根据状态选择按钮 switch (status) { case 0: btn = this.questBtns[1] break; case 1: btn = this.questBtns[0] break; case 2: btn = this.questBtns[2] break; default: break; } btn.active = true // 设置图标和数量 if(status != 2) { let countNode = cc.find('/count_label', btn) let spriteNode = cc.find('/sprite', btn) let giftSprite this.giftMap.forEach((value, index) => { if(value == giftType) { giftSprite = this.giftFrames[index] } }) countNode.getComponent('cc.Label').string = `${giftCount}` spriteNode.getComponent('cc.Sprite').spriteFrame = giftSprite } }, /** * 设置任务阶段进度条 * @param {number} current 当前值 * @param {number} total 阶段总值 */ _setProgress (current, total) { let _progress = (current / total).toFixed(1) console.log(_progress); this.progressBar.progress = _progress this.progressLabel.string = `${current} / ${total}` }, /** * 设置当前任务星级 * @param {number} level 星级 */ _setStarLevel (level) { 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] } }) } });