SkillBuildingAlert.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. const SkillApi = require('../net/SkillApi');
  2. const DWTool = require("../utils/DWTool");
  3. const GameModule = require("../utils/GameModule");
  4. const buildingLevel = require('../data/buildingLevel');
  5. const GameNotificationKey = require('../utils/GameEnum').GameNotificationKey;
  6. const TapTapTool = require("../utils/TapTapTool");
  7. var Promise = require('../lib/es6-promise').Promise;
  8. cc.Class({
  9. extends: cc.Component,
  10. properties: {
  11. button100: cc.Button,
  12. sprite100: cc.Sprite,
  13. richText100: cc.RichText,
  14. button10: cc.Button,
  15. sprite10: cc.Sprite,
  16. richText10: cc.RichText,
  17. lightSprite: cc.SpriteFrame,
  18. graySprite: cc.SpriteFrame,
  19. },
  20. // LIFE-CYCLE CALLBACKS:
  21. onLoad () {},
  22. start () {
  23. },
  24. updateData(gold10, gold100, item) {
  25. this._item = item;
  26. this._gold10 = gold10;
  27. this._gold100 = gold100;
  28. this.button10.interactable = false;
  29. this.button100.interactable = false;
  30. this.richText10.string = '<color=#ffffff>升级 x10</c>' + `<br/><img src='coin_small'/><color=#ffffff>${TapTapTool.parseToString(this._gold10)}</c>`;
  31. this.richText100.string = '<color=#ffffff>升级 x100</c>' + `<br/><img src='coin_small'/><color=#ffffff>${TapTapTool.parseToString(this._gold100)}</c>`;
  32. },
  33. /// 是否是100
  34. setupGrayBg(isActive, is100) {
  35. let spriteFrame = isActive ? this.lightSprite : this.graySprite;
  36. if (is100) {
  37. this.button100.interactable = isActive;
  38. if (this.sprite100 != spriteFrame) {
  39. this.sprite100.spriteFrame = spriteFrame;
  40. }
  41. } else {
  42. if (this.sprite10 != spriteFrame) {
  43. this.sprite10.spriteFrame = spriteFrame;
  44. }
  45. this.button10.interactable = isActive;
  46. }
  47. },
  48. update(dt) {
  49. let gold = GameModule.userInfo.gold;
  50. let is10 = TapTapTool.compare(gold, this._gold10);
  51. let is100 = TapTapTool.compare(gold, this._gold100);
  52. this.setupGrayBg(is10, false);
  53. this.setupGrayBg(is100, true);
  54. },
  55. buttonAction(eventTarget, eventData) {
  56. GameModule.audioMng.playClickButton();
  57. if (eventData == 10) {
  58. this.button10.interactable = false;
  59. } else {
  60. this.button100.interactable = false;
  61. }
  62. this.upBuildingLevel(eventData).then((respondData) => {
  63. let upGold = {};
  64. if (eventData == 10) {
  65. this.button10.interactable = true;
  66. upGold = this._gold10;
  67. } else {
  68. this.button100.interactable = true;
  69. upGold = this._gold100;
  70. }
  71. GameModule.userInfo.buildingLevel = respondData.level;
  72. GameModule.userInfo.gold = TapTapTool.sub(GameModule.userInfo.gold, upGold);
  73. this._item.hiddenSkillBuidingAlert(respondData.gold10, respondData.gold100);
  74. GameEvent.fire(GameNotificationKey.UpBuildingLevel);
  75. }).catch(({code, msg}) => {
  76. console.log(code, msg);
  77. if (eventData == 10) {
  78. this.button10.interactable = true;
  79. } else {
  80. this.button100.interactable = true;
  81. }
  82. });
  83. },
  84. /// 升级建筑
  85. upBuildingLevel(upLevel) {
  86. return new Promise((resolve, reject) => {
  87. // 获取目标用户的建筑
  88. SkillApi.upBuildingLevel(upLevel, (respondData) => {
  89. resolve(respondData);
  90. }, (code, msg) => {
  91. reject({code, msg});
  92. });
  93. });
  94. },
  95. // update (dt) {},
  96. });