ChangeJob.js 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  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, this.actorInfo.gender);
  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(0, this.actorInfo.gender);
  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. GameEvent.fire(GameNotificationKey.RefreshFriendList);
  184. this.node.zIndex = 0;
  185. this.node.destroy();
  186. },
  187. nextChoose() {
  188. if (this.myActorList && this.myActorList.length > 0) {
  189. this.actorInfo = this.myActorList.pop();
  190. this.bindChooseJob();
  191. } else {
  192. this.close();
  193. GameEvent.fire(GameNotificationKey.RefreshUserInformation);
  194. }
  195. },
  196. bindChooseJob() {
  197. this.user.getComponent('Actor').bind(this.actorInfo);
  198. },
  199. onChangeJobSuccess(msg) {
  200. GameModule.userInfo.grossIncome -= this.spend;
  201. let self = this;
  202. let targetJob = this.pageViewScript.getSelectedJob();
  203. let lastJob = null;
  204. jobList.forEach(jobInfo => {
  205. if (jobInfo.id === this.actorInfo.jobId) {
  206. lastJob = jobInfo;
  207. }
  208. });
  209. this.changeJobSuccess = cc.instantiate(this.changeJobSuccessPrefab);
  210. this.changeJobSuccess = this.changeJobSuccess.getComponent('ChangeJobSuccess');
  211. this.changeJobSuccess.show(this.node, lastJob, targetJob, () => {
  212. self.close();
  213. });
  214. GameEvent.fire(GameNotificationKey.RefreshUserInformation);
  215. GameEvent.fire(GameNotificationKey.PlaySuccessAnimation);
  216. },
  217. onLevelUpSuccess() {
  218. GameModule.userInfo.grossIncome -= this.spend;
  219. let self = this;
  220. this.levelUpSuccess = cc.instantiate(this.levelUpSuccessPrefab);
  221. this.levelUpSuccess = this.levelUpSuccess.getComponent('JobLevelUpSuccess');
  222. this.levelUpSuccess.show(this.node, '进阶成功', '恭喜你的艺人进阶成为:', this.upgradeLevel, this.upgradeName, () => {
  223. self.close();
  224. });
  225. GameEvent.fire(GameNotificationKey.RefreshUserInformation);
  226. GameEvent.fire(GameNotificationKey.PlaySuccessAnimation);
  227. },
  228. onFail(msg, callback) {
  229. this.fail = cc.instantiate(this.failPrefab);
  230. this.fail = this.fail.getComponent('JobPageFail');
  231. this.fail.show(this.node, msg, callback);
  232. },
  233. setNeedChooseJobUsers(users) {
  234. this.myActorList = users;
  235. },
  236. pageviewChangeEvent() {
  237. if (this.type === JobPageType.ChangeJob) {
  238. this.bindChangeJobComsume(this.pageViewScript.getSelectedJob());
  239. } else if (this.type === JobPageType.ChooseJob) {
  240. this.pageViewScript.bindSelectedJobText();
  241. }
  242. },
  243. showTips(text) {
  244. this.tipsNode.active = true;
  245. this.tipsLabel.string = text;
  246. this.scheduleOnce(() => {
  247. this.tipsNode.active = false;
  248. }, 2);
  249. }
  250. // update (dt) {},
  251. });