123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197 |
- 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("领取奖励失败");
- });
- }
- });
|