ArtistTrain.js 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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. GameEvent.on(GameNotificationKey.BeginArtistTrain, this, (missionId) => {
  57. this.clickStartTrain(missionId);
  58. });
  59. GameEvent.on(GameNotificationKey.ArtistTrainCompletion, this, (missionId) => {
  60. ArtistTrainApi.missionGain(this.targetUid, missionId, (responseData) => {
  61. console.log("任务已完成!");
  62. GameEvent.fire(GameNotificationKey.RefreshUserInformation);
  63. }, (err, msg) => {
  64. console.log(msg);
  65. });
  66. this.close();
  67. });
  68. },
  69. onDestroy() {
  70. GameEvent.off(GameNotificationKey.BeginArtistTrain, this);
  71. GameEvent.off(GameNotificationKey.ArtistTrainCompletion, this);
  72. },
  73. start() {
  74. this.content.y = -cc.view.getVisibleSize().height;
  75. let bouncesActionArray = [];
  76. let space = 50;
  77. let upAction = cc.moveTo(0.25, cc.v2(0, space));
  78. let downAction = cc.moveBy(0.1, cc.v2(0, -space));
  79. bouncesActionArray.push(upAction);
  80. bouncesActionArray.push(downAction);
  81. this.content.runAction(cc.sequence(bouncesActionArray));
  82. },
  83. getNetworkData() {
  84. ArtistTrainApi.missionGetTrainings(this.targetUid, (responseData) => {
  85. this.missionData = responseData.list || [];
  86. if (typeof responseData.list === 'array' && responseData.list.length > 0) {
  87. for (let item of responseData.list) {
  88. if (item.status === 1 || item.status === 2) {
  89. this.isTraining = true;
  90. } else {
  91. this.isTraining = false;
  92. }
  93. }
  94. } else {
  95. // 设置为true可以将倒计时隐藏起来
  96. this.isTraining = true;
  97. }
  98. this.layout();
  99. }, (err, msg) => {
  100. console.log("error: " + msg);
  101. });
  102. },
  103. clickStartTrain(missionId) {
  104. ArtistTrainApi.missionTrains(this.targetUid, missionId, (responseData) => {
  105. this.missionData = responseData.list || [];
  106. this.isTraining = true;
  107. this.layout();
  108. }, (err, msg) => {
  109. console.log("error: " + msg);
  110. });
  111. },
  112. layout() {
  113. for (let child of this.scrollView.content.children) {
  114. child.destroy();
  115. }
  116. DWTool.loadResPrefab("./prefabs/artist_train_item")
  117. .then((result) => {
  118. for (let i = 0; i < this.missionData.length; i++) {
  119. let item = cc.instantiate(result);
  120. item.getComponent('ArtistTrainItem').init(this.targetUid, this.missionData[i], this.node.zIndex);
  121. this.scrollView.content.addChild(item);
  122. }
  123. });
  124. },
  125. close() {
  126. this.node.destroy();
  127. },
  128. diamondRefresh() {
  129. AlertManager.showRechargeAlert(this.node.zIndex);
  130. },
  131. update(dt) {
  132. if (this.isTraining) { return; }
  133. if (Math.floor(this._currentTime) === this.countDown) {
  134. this._currentTime = 0;
  135. this.getNetworkData();
  136. } else {
  137. this._currentTime += dt;
  138. let resultCountDown = this.countDown - Math.floor(this._currentTime);
  139. if (this._preCountDown !== resultCountDown) {
  140. this.countDownLabel.string = DWTool.calculateTime(resultCountDown);
  141. this._preCountDown = resultCountDown;
  142. }
  143. }
  144. }
  145. });