1
0

ArtistTrain.js 5.2 KB

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