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