QuestMainItem.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  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 == this.tasks.length && 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(this.tasks.length);
  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. this.questBtns[1].interactable = false;
  122. Api.httpPost({
  123. url: "/task/gain.do",
  124. data: {
  125. taskId: this.task.taskId,
  126. sn: this.task.sn
  127. },
  128. success: (res) => {
  129. this.questBtns[1].interactable = true;
  130. GameModule.audioMng.playGetAward();
  131. if (res.clickMt) {
  132. let clickMt = TapTapTool.goldStrToClass(res.clickMt);
  133. if (GameModule.userInfo.perpetualClickMt.n != clickMt.n || GameModule.userInfo.perpetualClickMt.e != clickMt.e) {
  134. GameModule.userInfo.perpetualClickMt = clickMt;
  135. GameEvent.fire(GameNotificationKey.UpBuildingLevel);
  136. }
  137. }
  138. if (res.mt) {
  139. let mt = TapTapTool.goldStrToClass(res.mt);
  140. if (GameModule.userInfo.perpetualMt.n != mt.n || GameModule.userInfo.perpetualMt.e != mt.e) {
  141. GameModule.userInfo.perpetualMt = mt;
  142. GameModule.userInfo.refreshSecondText();
  143. }
  144. }
  145. let updatRes = {
  146. coin: '0',
  147. diamond: 0,
  148. ticket: 0
  149. }
  150. if(this.giftItem.id == 10001) {
  151. updatRes.coin = this.giftItem.itemCount;
  152. }
  153. if(this.giftItem.id == 10002) {
  154. updatRes.diamond = this.giftItem.itemCount;
  155. }
  156. if(this.giftItem.id == 10003) {
  157. updatRes.ticket = this.giftItem.itemCount;
  158. }
  159. // 更新全局userInfo
  160. this.quest.updateUserInfo(updatRes.coin, updatRes.diamond, updatRes.ticket);
  161. // 显示领取奖品动画
  162. // let showFrame = this.giftItem.giftFrame;
  163. let itemCount = TapTapTool.goldStrToClass(this.giftItem.itemCount);
  164. let showText = `${this.giftItem.name} x ${itemCount}`;
  165. if (this.giftItem.giftFrame) {
  166. let showFrame = this.giftItem.giftFrame;
  167. this.quest.showMainGift(showFrame, showText);
  168. }
  169. // 领取成功,更新当前按钮状态到下一阶段
  170. if(this.sn < this.tasks.length) {
  171. this.sn += 1;
  172. this._setTaskItem(this.sn);
  173. } else {
  174. this._setMaxItem();
  175. }
  176. if (res.isCancel) {
  177. let isCancel = res.isCancel == 1 ? true : false;
  178. GameEvent.fire('quest_main_notice', isCancel);
  179. }
  180. },
  181. fail: (errCode, errMsg) => {
  182. this.questBtns[1].interactable = true;
  183. // 领取失败
  184. Global.commonAlert.showCommonErrorAlert("领取奖励失败");
  185. }
  186. })
  187. },
  188. /**
  189. * 设置任务领取状态按钮
  190. * @param {number} status 任务状态[0 : 未完成, 1 : 完成可领取, 2 : 完成已领取]
  191. */
  192. _setGiftBtn (status) {
  193. // console.log(this.giftItem);
  194. this.questBtns.forEach(n => {
  195. n.active = false;
  196. });
  197. let btn
  198. // 根据状态选择按钮
  199. switch (status) {
  200. case QuestMainMissionType.NoFinished:
  201. btn = this.questBtns[0];
  202. break;
  203. case QuestMainMissionType.CanGain:
  204. btn = this.questBtns[1];
  205. break;
  206. case QuestMainMissionType.AlreadyGet:
  207. btn = this.questBtns[2];
  208. break;
  209. default:
  210. break;
  211. }
  212. btn.active = true;
  213. // 设置图标和数量
  214. if(status != QuestMainMissionType.AlreadyGet) {
  215. let countNode = cc.find('/awardNode/count_label', btn);
  216. let spriteNode = cc.find('/awardNode/sprite', btn);
  217. let awardLabelNode = cc.find('/awardLabel', btn);
  218. let awardNode = cc.find('/awardNode', btn);
  219. let otherNode = cc.find('/otherLabel', btn);
  220. var awardString = '奖励';
  221. if (status == QuestMainMissionType.CanGain) {
  222. awardString = '获得';
  223. }
  224. if (this.giftItem.id > 10002) {
  225. awardNode.active = false;
  226. otherNode.active = true;
  227. awardString = this.giftItem.wordInfo;
  228. otherNode.getComponent('cc.Label').string = `提升 ${DWTool.coinParseNoFixed(this.giftItem.itemCount)}倍`;
  229. } else {
  230. awardNode.active = true;
  231. otherNode.active = false;
  232. countNode.getComponent('cc.Label').string = `${DWTool.coinParseNoFixed(this.giftItem.itemCount)}`;
  233. if (this.giftItem.picId) {
  234. DWTool.loadResSpriteFrame(`./textures/taskItem/task_${this.giftItem.picId}`)
  235. .then((spriteFrame) => {
  236. spriteNode.getComponent('cc.Sprite').spriteFrame = spriteFrame;
  237. }).catch((err) => {
  238. console.log(err);
  239. });
  240. DWTool.loadResSpriteFrame(`./textures/item/${this.giftItem.id}`)
  241. .then((spriteFrame) => {
  242. this.giftItem.giftFrame = spriteFrame;
  243. }).catch((err) => {
  244. console.log(err);
  245. });
  246. }
  247. }
  248. awardLabelNode.getComponent('cc.Label').string = awardString;
  249. }
  250. DWTool.loadResSpriteFrame(`./textures/quest/altas/${this.task.picId}`)
  251. .then((spriteFrame) => {
  252. this.questNode.getComponent('cc.Sprite').spriteFrame = spriteFrame;
  253. }).catch((err) => {
  254. console.log(err);
  255. });
  256. },
  257. /**
  258. * 设置任务阶段进度条
  259. * @param {number} current 当前值
  260. * @param {number} total 阶段总值
  261. */
  262. _setProgress (current, total) {
  263. let _progress = 0;
  264. let newCurrent = TapTapTool.goldStrToClass(current + '');
  265. let newTotal = TapTapTool.goldStrToClass(total + '');
  266. /// 说明是正常的数字
  267. if (newCurrent.n == undefined && newTotal.n == undefined) {
  268. _progress = (newCurrent / newTotal).toFixed(1);
  269. this.progressBar.progress = _progress;
  270. this.progressLabel.string = `${current}/${total}`;
  271. } else {
  272. if (newCurrent == NaN) {
  273. newCurrent = {'n': 0, 'e': 0};
  274. }
  275. _progress = TapTapTool.divisionToRatio(newCurrent, newTotal).toFixed(1);
  276. this.progressBar.progress = _progress;
  277. this.progressLabel.string = `${TapTapTool.parseToString(newCurrent)}/${TapTapTool.parseToString(newTotal)}`;
  278. }
  279. },
  280. /**
  281. * 设置当前任务星级
  282. * @param {number} level 星级
  283. */
  284. _setStarLevel (level) {
  285. if (level >= 5) {
  286. this.starNode.forEach((node, index) => {
  287. if((index + 5) < level) {
  288. node.getComponent('cc.Sprite').spriteFrame = this.starFrames[3];
  289. } else {
  290. node.getComponent('cc.Sprite').spriteFrame = this.starFrames[2];
  291. }
  292. });
  293. } else {
  294. this.starNode.forEach((node, index) => {
  295. if(index < level) {
  296. node.getComponent('cc.Sprite').spriteFrame = this.starFrames[1];
  297. } else {
  298. node.getComponent('cc.Sprite').spriteFrame = this.starFrames[0];
  299. }
  300. });
  301. }
  302. }
  303. });