ChangeJob.js 9.9 KB

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