ChangeJob.js 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  1. const JobPageType = require('../utils/GameEnum').JobPageType;
  2. const JobApi = require('../net/JobApi');
  3. const GameNotificationKey = require('../utils/GameEnum').GameNotificationKey;
  4. const jobLevelList = require('../data/jobLevel');
  5. const jobList = require('../data/job');
  6. const cardList = require('../data/item');
  7. const GameModule = require('../utils/GameModule');
  8. cc.Class({
  9. extends: cc.Component,
  10. properties: {
  11. titleText: cc.RichText,
  12. confirmButton: cc.Button,
  13. confirmText: cc.RichText,
  14. pageView: cc.PageView,
  15. levelUpNode: cc.Node,
  16. bgNode: cc.Node,
  17. top: cc.Node,
  18. consume: cc.Node,
  19. user: cc.Node,
  20. myActorList: [],
  21. changeJobSuccessPrefab: cc.Prefab,
  22. levelUpSuccessPrefab: cc.Prefab,
  23. failPrefab: cc.Prefab,
  24. tipsNode: cc.Node,
  25. tipsLabel: cc.Label,
  26. },
  27. // LIFE-CYCLE CALLBACKS:
  28. onLoad() {
  29. this.node.height = cc.view.getVisibleSize().height;
  30. if (cc.view.getVisibleSize().height >= 1624) {
  31. this.top.getComponent(cc.Widget).top = 50;
  32. this.bgNode.getComponent(cc.Widget).top = -50;
  33. // this.informationNode.getComponent(cc.Widget).top = 185;
  34. // this.infoTop = 185;
  35. }
  36. this.consume.getComponent('Consume').setConfirmButton(this.confirmButton);
  37. },
  38. start() {
  39. },
  40. showFromTalent(type, actors) {
  41. this.myActorList = actors;
  42. this.show(type, this.myActorList.pop(), 0);
  43. },
  44. show(type, actorInfo, zIndex) {
  45. this.type = type;
  46. this.actorInfo = actorInfo;
  47. this.node.parent = cc.find('Canvas');
  48. this.node.active = true;
  49. this.node.zIndex = zIndex + 1;
  50. switch (type) {
  51. case JobPageType.ChangeJob:
  52. this.changeJob();
  53. break;
  54. case JobPageType.ChooseJob:
  55. this.chooseJob(actorInfo);
  56. break;
  57. case JobPageType.LevelUp:
  58. this.levelUp();
  59. break;
  60. }
  61. },
  62. changeJob() {
  63. this.consume.active = true;
  64. this.user.active = false;
  65. this.pageView.node.active = true;
  66. this.levelUpNode.active = false;
  67. this.pageView.getComponent('PVCtrl').bind(this.actorInfo.jobId);
  68. this.titleText.string = '<b>职业转换</b>';
  69. this.confirmText.string = '<outline color=#690802 width=1><b>转职</b></outline>';
  70. this.pageViewScript = this.pageView.getComponent('PVCtrl');
  71. JobApi.changeJobList(this.actorInfo.uid,
  72. (response) => {
  73. this.userPack = response.userPack;
  74. this.bindChangeJobComsume(this.pageViewScript.getSelectedJob());
  75. },
  76. (code, msg) => {
  77. this.showTips('网络错误');
  78. }
  79. );
  80. },
  81. bindChangeJobComsume(selectedJob) {
  82. let needCard = '';
  83. let itemKey = Object.keys(selectedJob.itemInfo)[0];
  84. cardList.forEach((cardInfo) => {
  85. if (cardInfo.id == itemKey) {
  86. needCard = cardInfo;
  87. needCard.number = selectedJob.itemInfo[itemKey];
  88. }
  89. });
  90. this.pageViewScript.bindSelectedJobText();
  91. this.consume.getComponent('Consume').bindChangeJob(selectedJob.gold, needCard, this.userPack);
  92. },
  93. chooseJob() {
  94. this.consume.active = false;
  95. this.user.active = true;
  96. this.pageView.node.active = true;
  97. this.levelUpNode.active = false;
  98. this.pageView.getComponent('PVCtrl').bind();
  99. this.titleText.string = '<b>选择职业</b>';
  100. this.confirmText.string = '<outline color=#690802 width=1><b>确定</b></outline>';
  101. this.pageViewScript = this.pageView.getComponent('PVCtrl');
  102. this.pageViewScript.bindSelectedJobText();
  103. this.bindChooseJob(this.actorInfo);
  104. },
  105. levelUp() {
  106. this.consume.active = true;
  107. this.user.active = false;
  108. this.pageView.node.active = false;
  109. this.levelUpNode.active = true;
  110. this.titleText.string = '<b>进阶</b>';
  111. this.confirmText.string = '<outline color=#690802 width=1><b>进阶</b></outline>';
  112. this.currentJobLevel = null;
  113. this.nextJobLevel = null;
  114. for (var i = 0; i < jobLevelList.length; i++) {
  115. let jobLevelInfo = jobLevelList[i];
  116. if (this.actorInfo.jobId === jobLevelInfo.jobId && this.actorInfo.jobLevel === jobLevelInfo.level) {
  117. this.currentJobLevel = jobLevelInfo;
  118. if (i >= jobLevelList.length - 1) {
  119. return;
  120. }
  121. this.nextJobLevel = jobLevelList[i + 1];
  122. this.spend = this.nextJobLevel.salary;
  123. }
  124. }
  125. let needCard = '';
  126. let itemKey = Object.keys(this.nextJobLevel.itemInfo)[0];
  127. cardList.forEach((cardInfo) => {
  128. if (cardInfo.id == itemKey) {
  129. needCard = cardInfo;
  130. needCard.number = this.nextJobLevel.itemInfo[itemKey];
  131. }
  132. });
  133. this.upgradeName = this.nextJobLevel.name;
  134. this.upgradeLevel = this.nextJobLevel.level;
  135. let self = this;
  136. JobApi.levelUpInfo(this.actorInfo.uid,
  137. (response) => {
  138. this.consume.getComponent('Consume').bindDataLevelUp(self.nextJobLevel.salary, needCard, response.userPack);
  139. this.levelUpNode.getComponent('LevelUp').bind(self.actorInfo, self.nextJobLevel, response.wish);
  140. },
  141. (code, msg) => {
  142. this.showTips('网络错误');
  143. }
  144. );
  145. },
  146. confirmClick() {
  147. switch (this.type) {
  148. case JobPageType.ChangeJob:
  149. var job = this.pageView.getComponent('PVCtrl').getSelectedJob();
  150. this.spend = job.gold;
  151. JobApi.changeJob(this.actorInfo.uid, job.id,
  152. (response, msg) => {
  153. this.onChangeJobSuccess(msg);
  154. },
  155. (code, msg) => {
  156. this.onFail(msg);
  157. });
  158. break;
  159. case JobPageType.ChooseJob:
  160. var job = this.pageView.getComponent('PVCtrl').getSelectedJob();
  161. JobApi.chooseJob(this.actorInfo.uid, job.id,
  162. (response) => {
  163. this.onFail('选择成功', () => {
  164. this.nextChoose();
  165. });
  166. },
  167. (code, msg) => {
  168. this.onFail(msg);
  169. });
  170. break;
  171. case JobPageType.LevelUp:
  172. JobApi.levelUp(this.actorInfo.uid,
  173. (response) => {
  174. this.onLevelUpSuccess();
  175. },
  176. (code, msg) => {
  177. this.onFail(msg);
  178. });
  179. break;
  180. }
  181. },
  182. close() {
  183. this.node.destroy();
  184. this.node.zIndex = 0;
  185. },
  186. nextChoose() {
  187. if (this.myActorList && this.myActorList.length > 0) {
  188. this.actorInfo = this.myActorList.pop();
  189. this.bindChooseJob();
  190. } else {
  191. this.close();
  192. GameEvent.fire(GameNotificationKey.RefreshUserInformation);
  193. }
  194. },
  195. bindChooseJob() {
  196. this.user.getComponent('Actor').bind(this.actorInfo);
  197. },
  198. onChangeJobSuccess(msg) {
  199. GameModule.userInfo.grossIncome -= this.spend;
  200. let self = this;
  201. let targetJob = this.pageViewScript.getSelectedJob();
  202. let lastJob = null;
  203. jobList.forEach(jobInfo => {
  204. if (jobInfo.id === this.actorInfo.jobId) {
  205. lastJob = jobInfo;
  206. }
  207. });
  208. this.changeJobSuccess = cc.instantiate(this.changeJobSuccessPrefab);
  209. this.changeJobSuccess = this.changeJobSuccess.getComponent('ChangeJobSuccess');
  210. this.changeJobSuccess.show(this.node, lastJob, targetJob, () => {
  211. self.close();
  212. });
  213. GameEvent.fire(GameNotificationKey.RefreshUserInformation);
  214. },
  215. onLevelUpSuccess() {
  216. GameModule.userInfo.grossIncome -= this.spend;
  217. let self = this;
  218. this.levelUpSuccess = cc.instantiate(this.levelUpSuccessPrefab);
  219. this.levelUpSuccess = this.levelUpSuccess.getComponent('JobLevelUpSuccess');
  220. this.levelUpSuccess.show(this.node, '进阶成功', '恭喜你的艺人进阶成为:', this.upgradeLevel, this.upgradeName, () => {
  221. self.close();
  222. });
  223. GameEvent.fire(GameNotificationKey.RefreshUserInformation);
  224. },
  225. onFail(msg, callback) {
  226. this.fail = cc.instantiate(this.failPrefab);
  227. this.fail = this.fail.getComponent('JobPageFail');
  228. this.fail.show(this.node, msg, callback);
  229. },
  230. setNeedChooseJobUsers(users) {
  231. this.myActorList = users;
  232. },
  233. pageviewChangeEvent() {
  234. if (this.type === JobPageType.ChangeJob) {
  235. this.bindChangeJobComsume(this.pageViewScript.getSelectedJob());
  236. } else if (this.type === JobPageType.ChooseJob) {
  237. this.pageViewScript.bindSelectedJobText();
  238. }
  239. },
  240. showTips(text) {
  241. this.tipsNode.active = true;
  242. this.tipsLabel.string = text;
  243. this.scheduleOnce(() => {
  244. this.tipsNode.active = false;
  245. }, 2);
  246. }
  247. // update (dt) {},
  248. });