QuestMainItem.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  1. const Api = require('../net/Api');
  2. const DWTool = require("../utils/DWTool");
  3. const TapTapTool = require("../utils/TapTapTool");
  4. const itemList = require('../data/item');
  5. const GameModule = require("../utils/GameModule");
  6. const GameNotificationKey = require('../utils/GameEnum').GameNotificationKey;
  7. const QuestMainMissionType = require("../utils/GameEnum").QuestMainMissionType;
  8. cc.Class({
  9. extends: cc.Component,
  10. properties: {
  11. questNode: cc.Node,
  12. questBtns: [cc.Node],
  13. starNode: {
  14. tooltip: '星星节点',
  15. default: [],
  16. type: [cc.Node]
  17. },
  18. starFrames: {
  19. tooltip: '星星图片素材',
  20. default: [],
  21. type: [cc.SpriteFrame]
  22. },
  23. progressLabel: {
  24. tooltip: '奖励数量',
  25. default: null,
  26. type: cc.Label
  27. },
  28. progressBar: cc.ProgressBar,
  29. questTitle: {
  30. tooltip: '任务标题',
  31. default: null,
  32. type: cc.Label
  33. },
  34. qusetMsg: {
  35. tooltip: '任务说明',
  36. default: null,
  37. type: cc.Label
  38. }
  39. },
  40. onLoad () {
  41. this.giftMap = ['diamond', 'coin', 'ticket'];
  42. },
  43. start () {
  44. },
  45. init (parent, tasks, sn, questIndex) {
  46. this.parent = parent;
  47. this.quest = parent.quest;
  48. this.tasks = tasks;
  49. this.questIndex = questIndex;
  50. sn = this.sn = parseInt(sn);
  51. // 设置当前任务等级内容
  52. this._setTaskItem(sn);
  53. },
  54. _setTaskItem (sn) {
  55. let task = this.task = this.tasks.find(n => {
  56. if(sn == 0) {
  57. return n.sn == 1;
  58. } else {
  59. return n.sn == sn;
  60. }
  61. })
  62. if(sn == 5 && task.status == 2) {
  63. this._setMaxItem();
  64. } else {
  65. let level = sn == 0 ? 0 : sn - 1;
  66. let nameLevelMap = ['I', 'II', 'III', 'IV', 'V'];
  67. // 设置当前任务标题
  68. this.questTitle.string = `${task.name}(${nameLevelMap[level]})`;
  69. // 设置当前任务说明内容
  70. let aimValue = TapTapTool.goldStrToClass(task.aimValue);
  71. let totalAimValue = TapTapTool.goldStrToClass(task.totalAimValue);
  72. if (aimValue.n == undefined && totalAimValue.n == undefined) {
  73. this.qusetMsg.string = task.msg.replace('${num}', totalAimValue);
  74. // 设置任务阶段进度条
  75. let taskAimValue = aimValue > totalAimValue ? totalAimValue : aimValue;
  76. this._setProgress(taskAimValue, task.totalAimValue);
  77. } else {
  78. this.qusetMsg.string = task.msg.replace('${num}', TapTapTool.parseToString(totalAimValue));
  79. // 设置任务阶段进度条
  80. let taskAimValue = TapTapTool.compare(aimValue, totalAimValue) ? task.totalAimValue : task.aimValue;
  81. this._setProgress(taskAimValue, task.totalAimValue);
  82. }
  83. // 设置当前任务星级
  84. this._setStarLevel(level);
  85. this.giftItem = Object.assign({
  86. itemCount: task.itemCount
  87. }, itemList.find(n => {
  88. return n.id == task.itemId
  89. }));
  90. // 设置任务领取按钮
  91. this._setGiftBtn(task.status);
  92. }
  93. },
  94. _setMaxItem () {
  95. let task = this.tasks[4];
  96. // 设置当前任务标题
  97. this.questTitle.string = `${task.name}(V)`;
  98. // 设置当前任务星级
  99. this._setStarLevel(5);
  100. // 设置当前任务说明内容
  101. let aimValue = TapTapTool.goldStrToClass(task.aimValue);
  102. let totalAimValue = TapTapTool.goldStrToClass(task.totalAimValue);
  103. if (aimValue.n == undefined && totalAimValue.n == undefined) {
  104. this.qusetMsg.string = task.msg.replace('${num}', totalAimValue);
  105. // 设置任务阶段进度条
  106. let taskAimValue = aimValue > totalAimValue ? totalAimValue : aimValue;
  107. this._setProgress(taskAimValue, task.totalAimValue);
  108. } else {
  109. this.qusetMsg.string = task.msg.replace('${num}', TapTapTool.parseToString(totalAimValue));
  110. // 设置任务阶段进度条
  111. let taskAimValue = TapTapTool.compare(aimValue, totalAimValue) ? task.totalAimValue : task.aimValue;
  112. this._setProgress(taskAimValue, task.totalAimValue);
  113. }
  114. // 设置任务领取按钮
  115. this._setGiftBtn(QuestMainMissionType.AlreadyGet);
  116. },
  117. /**
  118. * 领取按钮点击
  119. */
  120. handleGiftBtn () {
  121. Api.httpPost({
  122. url: "/task/gain.do",
  123. data: {
  124. taskId: this.task.taskId,
  125. sn: this.task.sn
  126. },
  127. success: (res) => {
  128. GameModule.audioMng.playGetAward();
  129. if (res.clickMt) {
  130. let clickMt = TapTapTool.goldStrToClass(res.clickMt);
  131. if (GameModule.userInfo.perpetualClickMt.n != clickMt.n || GameModule.userInfo.perpetualClickMt.e != clickMt.e) {
  132. GameModule.userInfo.perpetualClickMt = clickMt;
  133. GameEvent.fire(GameNotificationKey.UpBuildingLevel);
  134. }
  135. }
  136. if (res.mt) {
  137. let mt = TapTapTool.goldStrToClass(res.mt);
  138. if (GameModule.userInfo.perpetualMt.n != mt.n || GameModule.userInfo.perpetualMt.e != mt.e) {
  139. GameModule.userInfo.perpetualMt = mt;
  140. GameModule.userInfo.refreshSecondText();
  141. }
  142. }
  143. let updatRes = {
  144. coin: '0',
  145. diamond: 0,
  146. ticket: 0
  147. }
  148. if(this.giftItem.id == 10001) {
  149. updatRes.coin = this.giftItem.itemCount;
  150. }
  151. if(this.giftItem.id == 10002) {
  152. updatRes.diamond = this.giftItem.itemCount;
  153. }
  154. if(this.giftItem.id == 10003) {
  155. updatRes.ticket = this.giftItem.itemCount;
  156. }
  157. // 更新全局userInfo
  158. this.quest.updateUserInfo(updatRes.coin, updatRes.diamond, updatRes.ticket);
  159. // 显示领取奖品动画
  160. // let showFrame = this.giftItem.giftFrame;
  161. let itemCount = TapTapTool.goldStrToClass(this.giftItem.itemCount);
  162. let showText = `${this.giftItem.name} x ${itemCount}`;
  163. if (this.giftItem.giftFrame) {
  164. let showFrame = this.giftItem.giftFrame;
  165. this.quest.showMainGift(showFrame, showText);
  166. }
  167. // 领取成功,更新当前按钮状态到下一阶段
  168. if(this.sn < 5) {
  169. this.sn += 1;
  170. this._setTaskItem(this.sn);
  171. } else {
  172. this._setMaxItem();
  173. }
  174. },
  175. fail: (errCode, errMsg) => {
  176. // 领取失败
  177. Global.commonAlert.showCommonErrorAlert("领取奖励失败");
  178. }
  179. })
  180. },
  181. /**
  182. * 设置任务领取状态按钮
  183. * @param {number} status 任务状态[0 : 未完成, 1 : 完成可领取, 2 : 完成已领取]
  184. */
  185. _setGiftBtn (status) {
  186. // console.log(this.giftItem);
  187. this.questBtns.forEach(n => {
  188. n.active = false;
  189. });
  190. let btn
  191. // 根据状态选择按钮
  192. switch (status) {
  193. case QuestMainMissionType.NoFinished:
  194. btn = this.questBtns[0];
  195. break;
  196. case QuestMainMissionType.CanGain:
  197. btn = this.questBtns[1];
  198. break;
  199. case QuestMainMissionType.AlreadyGet:
  200. btn = this.questBtns[2];
  201. break;
  202. default:
  203. break;
  204. }
  205. btn.active = true;
  206. // 设置图标和数量
  207. if(status != QuestMainMissionType.AlreadyGet) {
  208. let countNode = cc.find('/awardNode/count_label', btn);
  209. let spriteNode = cc.find('/awardNode/sprite', btn);
  210. let awardLabelNode = cc.find('/awardLabel', btn);
  211. let awardNode = cc.find('/awardNode', btn);
  212. let otherNode = cc.find('/otherLabel', btn);
  213. var awardString = '奖励';
  214. if (status == QuestMainMissionType.CanGain) {
  215. awardString = '获得';
  216. }
  217. if (this.giftItem.id > 10002) {
  218. awardNode.active = false;
  219. otherNode.active = true;
  220. awardString = this.giftItem.wordInfo;
  221. otherNode.getComponent('cc.Label').string = `提升 ${DWTool.coinParseNoFixed(this.giftItem.itemCount)}倍`;
  222. } else {
  223. awardNode.active = true;
  224. otherNode.active = false;
  225. countNode.getComponent('cc.Label').string = `${DWTool.coinParseNoFixed(this.giftItem.itemCount)}`;
  226. if (this.giftItem.picId) {
  227. DWTool.loadResSpriteFrame(`./textures/taskItem/task_${this.giftItem.picId}`)
  228. .then((spriteFrame) => {
  229. spriteNode.getComponent('cc.Sprite').spriteFrame = spriteFrame;
  230. }).catch((err) => {
  231. console.log(err);
  232. });
  233. DWTool.loadResSpriteFrame(`./textures/item/${this.giftItem.id}`)
  234. .then((spriteFrame) => {
  235. this.giftItem.giftFrame = spriteFrame;
  236. }).catch((err) => {
  237. console.log(err);
  238. });
  239. }
  240. }
  241. awardLabelNode.getComponent('cc.Label').string = awardString;
  242. }
  243. DWTool.loadResSpriteFrame(`./textures/quest/altas/${this.task.picId}`)
  244. .then((spriteFrame) => {
  245. this.questNode.getComponent('cc.Sprite').spriteFrame = spriteFrame;
  246. }).catch((err) => {
  247. console.log(err);
  248. });
  249. },
  250. /**
  251. * 设置任务阶段进度条
  252. * @param {number} current 当前值
  253. * @param {number} total 阶段总值
  254. */
  255. _setProgress (current, total) {
  256. let _progress = 0;
  257. let newCurrent = TapTapTool.goldStrToClass(current + '');
  258. let newTotal = TapTapTool.goldStrToClass(total + '');
  259. /// 说明是正常的数字
  260. if (newCurrent.n == undefined && newTotal.n == undefined) {
  261. _progress = (newCurrent / newTotal).toFixed(1);
  262. this.progressBar.progress = _progress;
  263. this.progressLabel.string = `${current}/${total}`;
  264. } else {
  265. if (newCurrent == NaN) {
  266. newCurrent = {'n': 0, 'e': 0};
  267. }
  268. _progress = TapTapTool.divisionToRatio(newCurrent, newTotal).toFixed(1);
  269. this.progressBar.progress = _progress;
  270. this.progressLabel.string = `${TapTapTool.parseToString(newCurrent)}/${TapTapTool.parseToString(newTotal)}`;
  271. }
  272. },
  273. /**
  274. * 设置当前任务星级
  275. * @param {number} level 星级
  276. */
  277. _setStarLevel (level) {
  278. this.starNode.forEach((node, index) => {
  279. if(index < level) {
  280. node.getComponent('cc.Sprite').spriteFrame = this.starFrames[1];
  281. } else {
  282. node.getComponent('cc.Sprite').spriteFrame = this.starFrames[0];
  283. }
  284. });
  285. }
  286. });