ArtistTrain.js 5.5 KB

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