QuestMainItem.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  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. if (res.isCancel) {
  175. let isCancel = res.isCancel == 1 ? true : false;
  176. GameEvent.fire('quest_main_notice', isCancel);
  177. }
  178. },
  179. fail: (errCode, errMsg) => {
  180. // 领取失败
  181. Global.commonAlert.showCommonErrorAlert("领取奖励失败");
  182. }
  183. })
  184. },
  185. /**
  186. * 设置任务领取状态按钮
  187. * @param {number} status 任务状态[0 : 未完成, 1 : 完成可领取, 2 : 完成已领取]
  188. */
  189. _setGiftBtn (status) {
  190. // console.log(this.giftItem);
  191. this.questBtns.forEach(n => {
  192. n.active = false;
  193. });
  194. let btn
  195. // 根据状态选择按钮
  196. switch (status) {
  197. case QuestMainMissionType.NoFinished:
  198. btn = this.questBtns[0];
  199. break;
  200. case QuestMainMissionType.CanGain:
  201. btn = this.questBtns[1];
  202. break;
  203. case QuestMainMissionType.AlreadyGet:
  204. btn = this.questBtns[2];
  205. break;
  206. default:
  207. break;
  208. }
  209. btn.active = true;
  210. // 设置图标和数量
  211. if(status != QuestMainMissionType.AlreadyGet) {
  212. let countNode = cc.find('/awardNode/count_label', btn);
  213. let spriteNode = cc.find('/awardNode/sprite', btn);
  214. let awardLabelNode = cc.find('/awardLabel', btn);
  215. let awardNode = cc.find('/awardNode', btn);
  216. let otherNode = cc.find('/otherLabel', btn);
  217. var awardString = '奖励';
  218. if (status == QuestMainMissionType.CanGain) {
  219. awardString = '获得';
  220. }
  221. if (this.giftItem.id > 10002) {
  222. awardNode.active = false;
  223. otherNode.active = true;
  224. awardString = this.giftItem.wordInfo;
  225. otherNode.getComponent('cc.Label').string = `提升 ${DWTool.coinParseNoFixed(this.giftItem.itemCount)}倍`;
  226. } else {
  227. awardNode.active = true;
  228. otherNode.active = false;
  229. countNode.getComponent('cc.Label').string = `${DWTool.coinParseNoFixed(this.giftItem.itemCount)}`;
  230. if (this.giftItem.picId) {
  231. DWTool.loadResSpriteFrame(`./textures/taskItem/task_${this.giftItem.picId}`)
  232. .then((spriteFrame) => {
  233. spriteNode.getComponent('cc.Sprite').spriteFrame = spriteFrame;
  234. }).catch((err) => {
  235. console.log(err);
  236. });
  237. DWTool.loadResSpriteFrame(`./textures/item/${this.giftItem.id}`)
  238. .then((spriteFrame) => {
  239. this.giftItem.giftFrame = spriteFrame;
  240. }).catch((err) => {
  241. console.log(err);
  242. });
  243. }
  244. }
  245. awardLabelNode.getComponent('cc.Label').string = awardString;
  246. }
  247. DWTool.loadResSpriteFrame(`./textures/quest/altas/${this.task.picId}`)
  248. .then((spriteFrame) => {
  249. this.questNode.getComponent('cc.Sprite').spriteFrame = spriteFrame;
  250. }).catch((err) => {
  251. console.log(err);
  252. });
  253. },
  254. /**
  255. * 设置任务阶段进度条
  256. * @param {number} current 当前值
  257. * @param {number} total 阶段总值
  258. */
  259. _setProgress (current, total) {
  260. let _progress = 0;
  261. let newCurrent = TapTapTool.goldStrToClass(current + '');
  262. let newTotal = TapTapTool.goldStrToClass(total + '');
  263. /// 说明是正常的数字
  264. if (newCurrent.n == undefined && newTotal.n == undefined) {
  265. _progress = (newCurrent / newTotal).toFixed(1);
  266. this.progressBar.progress = _progress;
  267. this.progressLabel.string = `${current}/${total}`;
  268. } else {
  269. if (newCurrent == NaN) {
  270. newCurrent = {'n': 0, 'e': 0};
  271. }
  272. _progress = TapTapTool.divisionToRatio(newCurrent, newTotal).toFixed(1);
  273. this.progressBar.progress = _progress;
  274. this.progressLabel.string = `${TapTapTool.parseToString(newCurrent)}/${TapTapTool.parseToString(newTotal)}`;
  275. }
  276. },
  277. /**
  278. * 设置当前任务星级
  279. * @param {number} level 星级
  280. */
  281. _setStarLevel (level) {
  282. this.starNode.forEach((node, index) => {
  283. if(index < level) {
  284. node.getComponent('cc.Sprite').spriteFrame = this.starFrames[1];
  285. } else {
  286. node.getComponent('cc.Sprite').spriteFrame = this.starFrames[0];
  287. }
  288. });
  289. }
  290. });