ArtistTrain.js 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. const DWTool = require("../utils/DWTool");
  2. const AlertManager = require('../utils/AlertManager');
  3. const {GameNotificationKey} = require('../utils/GameEnum');
  4. const ArtistTrainApi = require('../net/ArtistTrainApi');
  5. const GameModule = require('../utils/GameModule');
  6. cc.Class({
  7. extends: cc.Component,
  8. properties: {
  9. content: cc.Node,
  10. scrollView: cc.ScrollView,
  11. countDownLabel: cc.Label,
  12. refreshLabel: cc.Label,
  13. tipLabel: cc.Label,
  14. refreshBtn: cc.Button,
  15. countDown: {
  16. get: function() {
  17. if (!this._countDown) {
  18. this._countDown = 0;
  19. }
  20. return this._countDown;
  21. },
  22. set: function (value) {
  23. this._countDown = value;
  24. this.countDownLabel.string = DWTool.calculateTime(this._countDown);
  25. this._preCountDown = this._countDown;
  26. }
  27. },
  28. isTraining: {
  29. get: function() {
  30. if (this._isTraining === undefined) {
  31. this._isTraining = false;
  32. }
  33. return this._isTraining;
  34. },
  35. set: function (value) {
  36. this._isTraining = value;
  37. if (this._isTraining) {
  38. this.refreshLabel.node.active = false;
  39. this.tipLabel.node.active = false;
  40. this.refreshBtn.node.active = false;
  41. } else {
  42. this.refreshLabel.node.active = true;
  43. this.tipLabel.node.active = true;
  44. this.refreshBtn.node.active = true;
  45. }
  46. }
  47. },
  48. },
  49. init(targetUid) {
  50. this.targetUid = targetUid;
  51. },
  52. // LIFE-CYCLE CALLBACKS:
  53. onLoad () {
  54. this.countDown = 600;
  55. this._currentTime = 0;
  56. this.getNetworkData();
  57. GameEvent.on(GameNotificationKey.BeginArtistTrain, this, (missionId) => {
  58. this.clickStartTrain(missionId);
  59. });
  60. GameEvent.on(GameNotificationKey.ArtistTrainCompletion, this, (missionId) => {
  61. ArtistTrainApi.missionGain(this.targetUid, missionId, (responseData) => {
  62. console.log("任务已完成!");
  63. GameEvent.fire(GameNotificationKey.RefreshUserInformation);
  64. }, (err, msg) => {
  65. console.log(msg);
  66. });
  67. this.close();
  68. });
  69. GameModule.homeGuide.on('Fire_state29', () => {
  70. if (this.missionData && this.missionData.length > 0) {
  71. let missionId = this.missionData[0].id;
  72. this.clickStartTrain(missionId);
  73. }
  74. }, this);
  75. },
  76. onDestroy() {
  77. GameEvent.off(GameNotificationKey.BeginArtistTrain, this);
  78. GameEvent.off(GameNotificationKey.ArtistTrainCompletion, this);
  79. },
  80. start() {
  81. this.content.y = -cc.view.getVisibleSize().height;
  82. let bouncesActionArray = [];
  83. let space = 50;
  84. let upAction = cc.moveTo(0.25, cc.v2(0, space));
  85. let downAction = cc.moveBy(0.1, cc.v2(0, -space));
  86. bouncesActionArray.push(upAction);
  87. bouncesActionArray.push(downAction);
  88. this.content.runAction(cc.sequence(bouncesActionArray));
  89. },
  90. getNetworkData() {
  91. ArtistTrainApi.missionGetTrainings(this.targetUid, (responseData) => {
  92. this.missionData = responseData.list || [];
  93. if (typeof responseData.list === 'array' && responseData.list.length > 0) {
  94. for (let item of responseData.list) {
  95. if (item.status === 1 || item.status === 2) {
  96. this.isTraining = true;
  97. } else {
  98. this.isTraining = false;
  99. }
  100. }
  101. } else {
  102. // 设置为true可以将倒计时隐藏起来
  103. this.isTraining = true;
  104. }
  105. this.layout();
  106. }, (err, msg) => {
  107. console.log("error: " + msg);
  108. });
  109. },
  110. clickStartTrain(missionId) {
  111. ArtistTrainApi.missionTrains(this.targetUid, missionId, (responseData) => {
  112. this.missionData = responseData.list || [];
  113. this.isTraining = true;
  114. this.layout();
  115. GameModule.homeGuide.getComponent('HomeGuide').handleState('state30');
  116. }, (err, msg) => {
  117. console.log("error: " + msg);
  118. });
  119. },
  120. layout() {
  121. for (let child of this.scrollView.content.children) {
  122. child.destroy();
  123. }
  124. DWTool.loadResPrefab("./prefabs/artist_train_item")
  125. .then((result) => {
  126. for (let i = 0; i < this.missionData.length; i++) {
  127. let item = cc.instantiate(result);
  128. item.getComponent('ArtistTrainItem').init(this.targetUid, this.missionData[i], this.node.zIndex);
  129. this.scrollView.content.addChild(item);
  130. }
  131. GameModule.homeGuide.getComponent('HomeGuide').handleState('state29');
  132. });
  133. },
  134. close() {
  135. this.node.destroy();
  136. },
  137. diamondRefresh() {
  138. AlertManager.showRechargeAlert(this.node.zIndex);
  139. },
  140. update(dt) {
  141. if (this.isTraining) { return; }
  142. if (Math.floor(this._currentTime) === this.countDown) {
  143. this._currentTime = 0;
  144. this.getNetworkData();
  145. } else {
  146. this._currentTime += dt;
  147. let resultCountDown = this.countDown - Math.floor(this._currentTime);
  148. if (this._preCountDown !== resultCountDown) {
  149. this.countDownLabel.string = DWTool.calculateTime(resultCountDown);
  150. this._preCountDown = resultCountDown;
  151. }
  152. }
  153. }
  154. });