ArtistTrainItem.js 8.2 KB

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