ArtistTrainItem.js 9.1 KB

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