ArtistTrainItem.js 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. const DWTool = require("../utils/DWTool");
  2. const AlertManager = require('../utils/AlertManager');
  3. const { GameNotificationKey, ArtistTrainItemSkillStyle } = require('../utils/GameEnum');
  4. const ArtistTrainApi = require('../net/ArtistTrainApi');
  5. var AristTrainState = cc.Enum({
  6. Invalid: 0,
  7. Stay: 1,
  8. Training: 2,
  9. Completion: 3
  10. });
  11. cc.Class({
  12. extends: cc.Component,
  13. properties: {
  14. trainBgSprite: cc.Sprite,
  15. trainSprite: cc.Sprite,
  16. contentLabel: cc.Label,
  17. trainCostNode: cc.Node,
  18. costLabel: cc.Label,
  19. recuperateLabel: cc.Label,
  20. sandclockLabel: cc.Label,
  21. trainNode: cc.Node,
  22. refreshBtn: cc.Button,
  23. confirmBtn: cc.Button,
  24. trainSlider: cc.Slider,
  25. trainSliderTimeLabel: cc.Label,
  26. trainProgress: cc.Sprite,
  27. // 技能
  28. skillNode: cc.Node,
  29. diamondRefreshLabel: cc.Label,
  30. countDown: {
  31. get: function() {
  32. if (!this._countDown) {
  33. this._countDown = 0;
  34. }
  35. return this._countDown;
  36. },
  37. set: function (value) {
  38. this._countDown = value;
  39. this.trainSliderTimeLabel.string = DWTool.calculateTime(this._countDown);
  40. this._preCountDown = this._countDown;
  41. }
  42. },
  43. },
  44. init(targetUid, data, levelZIndex) {
  45. if (arguments.length < 3) {
  46. throw new Error("init Missing parameter...");
  47. }
  48. this.targetUid = targetUid;
  49. this.data = data;
  50. this.levelZIndex = levelZIndex;
  51. this.contentLabel.string = data.msg;
  52. this.itemInfo = this.data.items[0];
  53. this.recuperateLabel.string = `${this.itemInfo.name}(${this.itemInfo.count}/${this.itemInfo.count})`;
  54. this.costLabel.string = DWTool.coinParse(data.coin);
  55. this.sandclockLabel.string = DWTool.calculateTime(data.cd / 1000);
  56. let timestamp = Date.parse(new Date());
  57. if (timestamp > data.cdEnd) {
  58. this.trainSlider.progress = 1.0;
  59. this.trainSliderTimeLabel.string = DWTool.calculateTime(0);
  60. } else {
  61. let startTime = data.cdEnd - data.cd;
  62. this._currentTime = (timestamp - startTime) / 1000;
  63. this.countDown = data.cd / 1000;
  64. }
  65. this.setSkillLayout();
  66. this.diamondRefreshLabel.string = `${data.diamond} 加速`;
  67. DWTool.loadResSpriteFrame(`./artistTrain/${data.quality}`)
  68. .then((result) => {
  69. this.trainBgSprite.spriteFrame = result;
  70. });
  71. DWTool.loadResSpriteFrame(`./artistTrain/${data.picId}`)
  72. .then((result) => {
  73. this.trainSprite.spriteFrame = result;
  74. });
  75. if (data.status == 0) {
  76. this.setState(AristTrainState.Stay);
  77. } else if (data.status == 1) {
  78. this.setState(AristTrainState.Training);
  79. } else if (data.status == 2) {
  80. this.setState(AristTrainState.Completion);
  81. } else {
  82. this.setState(AristTrainState.Invalid);
  83. }
  84. },
  85. // LIFE-CYCLE CALLBACKS:
  86. onLoad () {
  87. this._width = this.trainProgress.node.width;
  88. this.showRechargeEvent = _.debounce(() => {
  89. AlertManager.showRechargeAlert(this.levelZIndex);
  90. }, 1000, true);
  91. },
  92. startTrain() {
  93. if (this.state === AristTrainState.Stay) {
  94. Global.GameEvent.fire(GameNotificationKey.BeginArtistTrain, this.data.id);
  95. }
  96. },
  97. setSkillLayout() {
  98. DWTool.loadResPrefab("./prefabs/artist_train_item_skill")
  99. .then((prefab) => {
  100. if (this.data.addCharm != 0) {
  101. let item = cc.instantiate(prefab);
  102. this.skillNode.addChild(item);
  103. item.getComponent('ArtistTrainItemSkill').init(ArtistTrainItemSkillStyle.Charm);
  104. }
  105. if (this.data.addAbility != 0) {
  106. let item = cc.instantiate(prefab);
  107. this.skillNode.addChild(item);
  108. item.getComponent('ArtistTrainItemSkill').init(ArtistTrainItemSkillStyle.Ability);
  109. }
  110. if (this.data.addEffect != 0) {
  111. let item = cc.instantiate(prefab);
  112. this.skillNode.addChild(item);
  113. item.getComponent('ArtistTrainItemSkill').init(ArtistTrainItemSkillStyle.Effect);
  114. }
  115. });
  116. },
  117. refreshTrainTime() {
  118. ArtistTrainApi.missionSpeedUp(this.targetUid, this.data.id, () => {
  119. this.countDown = this._currentTime + 0.2;
  120. });
  121. },
  122. confirm() {
  123. Global.GameEvent.fire(GameNotificationKey.ArtistTrainCompletion, this.data.id);
  124. AlertManager.showArtistTrainCompletion(this.data, this.levelZIndex);
  125. },
  126. setState(state) {
  127. if (this.state === state) { return; }
  128. switch (state) {
  129. case AristTrainState.Stay:
  130. this.trainNode.active = true;
  131. this.refreshBtn.node.active = false;
  132. this.trainSlider.node.active = false;
  133. this.trainCostNode.active = true;
  134. this.confirmBtn.node.active = false;
  135. this.trainSliderTimeLabel.node.active = false;
  136. break;
  137. case AristTrainState.Training:
  138. this.trainNode.active = false;
  139. this.refreshBtn.node.active = true;
  140. this.trainSlider.node.active = true;
  141. this.trainCostNode.active = false;
  142. this.confirmBtn.node.active = false;
  143. this.trainSliderTimeLabel.node.active = true;
  144. break;
  145. case AristTrainState.Completion:
  146. this.trainNode.active = false;
  147. this.refreshBtn.node.active = false;
  148. this.trainCostNode.active = false;
  149. this.trainSlider.node.active = true;
  150. this.confirmBtn.node.active = true;
  151. this.trainSliderTimeLabel.node.active = false;
  152. break;
  153. case AristTrainState.Invalid:
  154. this.trainNode.active = false;
  155. this.refreshBtn.node.active = false;
  156. this.trainCostNode.active = false;
  157. this.trainSlider.node.active = false;
  158. this.confirmBtn.node.active = false;
  159. this.trainSliderTimeLabel.node.active = false;
  160. break;
  161. default:
  162. break;
  163. }
  164. this.state = state;
  165. },
  166. update(dt) {
  167. if (this.state === AristTrainState.Training) {
  168. // 进度条走完, 开始生产金币
  169. if (Math.floor(this.trainSlider.progress) != 1) {
  170. this._currentTime += dt;
  171. this.trainSlider.progress = this._currentTime / this.countDown;
  172. if (Math.floor(this._currentTime) > this.countDown) {
  173. this._currentTime = this.countDown;
  174. }
  175. let resultCountDown = this.countDown - Math.floor(this._currentTime);
  176. if (this._preCountDown !== resultCountDown) {
  177. this.trainSliderTimeLabel.string = DWTool.calculateTime(resultCountDown);
  178. this._preCountDown = resultCountDown;
  179. }
  180. this.trainProgress.node.width = this._width * this.trainSlider.progress;
  181. } else {
  182. this.setState(AristTrainState.Completion);
  183. }
  184. }
  185. },
  186. });