StarItem.js 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. const GameModule = require("../utils/GameModule");
  2. const DWTool = require("../utils/DWTool");
  3. const StarApi = require('../net/StarApi');
  4. const GameNotificationKey = require('../utils/GameEnum').GameNotificationKey;
  5. const TapTapTool = require("../utils/TapTapTool");
  6. const ArtistManager = require('../utils/ArtistManager');
  7. cc.Class({
  8. extends: cc.Component,
  9. properties: {
  10. starIcon: cc.Sprite,
  11. starNameLabel: cc.Label,
  12. subTitleRichText: cc.RichText,
  13. subTitleRichText1: cc.RichText,
  14. starUseTitleRichText: cc.RichText,
  15. starUseBtn: cc.Button,
  16. starUserSprite: cc.Sprite,
  17. countLabel: cc.Label,
  18. countNode: cc.Node,
  19. starIconBlackBg: cc.Node,
  20. lightSpriteFrame: cc.SpriteFrame,
  21. graySpriteFrame: cc.SpriteFrame,
  22. lockedSpriteFrame: cc.SpriteFrame,
  23. },
  24. // LIFE-CYCLE CALLBACKS:
  25. onLoad () {
  26. GameEvent.on('StarItem_updateUI', this, () => {
  27. this._buyGold = this._listViewAdapter.buyGold;
  28. this._starTypeCount = this._listViewAdapter.starTypeCount;
  29. this.updateData();
  30. });
  31. },
  32. start () {
  33. this._isAcion = false;
  34. },
  35. onDestroy() {
  36. GameEvent.off('StarItem_updateUI', this);
  37. },
  38. update (dt) {
  39. /// 如果是锁住的 那就什么都不做嘛 这里就是更新金币
  40. if (this._isRoomLocked == false && this._isStarLocked == false && this._isAcion == false) {
  41. /// 如果是没反应的 说明金币不够
  42. if (this.starUseBtn.interactable == false) {
  43. if (TapTapTool.compare(GameModule.userInfo.gold, this._buyGold)) {
  44. this.setUpUseBtnBg(true);
  45. }
  46. }
  47. }
  48. },
  49. setListViewAdapter (listViewAdapter) {
  50. this._listViewAdapter = listViewAdapter;
  51. this._buyGold = listViewAdapter.buyGold;
  52. this._starTypeCount = listViewAdapter.starTypeCount;
  53. },
  54. updateItem(userInfo, itemId) {
  55. this._itemId = itemId;
  56. this._starData = userInfo;
  57. // console.log(this._starData.itemId);
  58. this._starInfo = Global.BuildingManager.getStarInfo(this._starData.starId);
  59. this.updateData();
  60. this.starNameLabel.string = this._starInfo.name;
  61. let imageId = 50000 + this._starData.starId;
  62. ArtistManager.loadStarAvatarSpriteFrame(imageId, this.starIcon);
  63. },
  64. deleteItem() {
  65. this._listViewAdapter.removeItem(this);
  66. },
  67. /// 实时更新数据
  68. updateData() {
  69. this.setUpIsCanSign();
  70. this.undateStarCount();
  71. },
  72. undateStarCount() {
  73. if (this._starData.starCount > 0) {
  74. this.countNode.active = true;
  75. this.countLabel.string = this._starData.starRoomCount + '/' + this._starData.starCount;
  76. } else {
  77. this.countNode.active = false;
  78. }
  79. },
  80. /// 设置是否能签约
  81. setUpIsCanSign() {
  82. if (this._starData.type === 3) {
  83. this._isRoomLocked = false;
  84. this._isStarLocked = false;
  85. this.subTitleRichText1.string = "";
  86. this.subTitleRichText.string = '';
  87. } else {
  88. let buyStarCount = GameModule.userInfo.buyStarCount;
  89. let roomId = this._starData.signRoomId;
  90. let isUnlocked = Global.BuildingManager.getRoomIsUnlocked(roomId);
  91. let roomName = Global.BuildingManager.getRoomName(roomId);
  92. this._isRoomLocked = false;
  93. this._isStarLocked = false;
  94. /// 需要的房间锁住的。
  95. if (isUnlocked == 0) {
  96. this.subTitleRichText.string = "<img src='star_false'/><color=#2E2E2E> 开启 " + roomName + "</c>";
  97. this._isRoomLocked = true;
  98. } else {
  99. this.subTitleRichText.string = "<img src='star_true'/><color=#2E2E2E> 开启 " + roomName + "</c>";
  100. }
  101. let needCount = this._starData.itemCount;
  102. if (needCount > 0) {
  103. /// 如要明星的个数为解锁条件
  104. if (this._starData.itemId == 101) {
  105. /// 如果明星数量足够
  106. if (buyStarCount >= needCount) {
  107. this.subTitleRichText1.string = "<img src='star_true'/><color=#2E2E2E>" + this._starInfo.desc + " (" + buyStarCount + "/" + needCount + ")" + "</c>";
  108. } else {
  109. this._isStarLocked = true;
  110. this.subTitleRichText1.string = "<img src='star_false'/><color=#2E2E2E>" + this._starInfo.desc + " (" + buyStarCount + "/" + needCount + ")" + "</c>";
  111. }
  112. } else if (this._starData.itemId == 102) {
  113. if (this._starTypeCount >= needCount) {
  114. this.subTitleRichText1.string = "<img src='star_true'/><color=#2E2E2E>" + this._starInfo.desc + " (" + this._starTypeCount + "/" + needCount + ")" + "</c>";
  115. } else {
  116. this._isStarLocked = true;
  117. this.subTitleRichText1.string = "<img src='star_fale'/><color=#2E2E2E>" + this._starInfo.desc + " (" + this._starTypeCount + "/" + needCount + ")" + "</c>";
  118. }
  119. } else {
  120. if (this._starData.areaStarCount >= needCount) {
  121. this.subTitleRichText1.string = "<img src='star_true'/><color=#2E2E2E>" + this._starInfo.desc + " (" + this._starData.areaStarCount + "/" + needCount + ")" + "</c>";
  122. } else {
  123. this._isStarLocked = true;
  124. this.subTitleRichText1.string = "<img src='star_false'/><color=#2E2E2E>" + this._starInfo.desc + " (" + this._starData.areaStarCount + "/" + needCount + ")" + "</c>";
  125. }
  126. }
  127. } else {
  128. this.subTitleRichText1.string = "";
  129. }
  130. }
  131. ///说明满足明星和房间的条件 已经解锁 那么只需要判断金币进行预约了
  132. if (this._isStarLocked == false && this._isRoomLocked == false) {
  133. /// 如果金币满足
  134. this.starUseTitleRichText.node.active = true;
  135. this.setUpUseBtnBg(TapTapTool.compare(GameModule.userInfo.gold, this._buyGold));
  136. this.starUseTitleRichText.string = `<color=#ffffff>签约</c><br/><img src='coin_small'/><color=#ffffff>${TapTapTool.parseToString(this._buyGold)}</c>`;
  137. this.starIconBlackBg.active = false;
  138. } else {
  139. this.starUseTitleRichText.node.active = false;
  140. this.starUseBtn.interactable = false;
  141. this.starUserSprite.spriteFrame = this.lockedSpriteFrame;
  142. this.starIconBlackBg.active = true;
  143. }
  144. },
  145. /// 设置签约的button是否能点击
  146. setUpUseBtnBg(isActive) {
  147. this.starUseBtn.interactable = isActive;
  148. this.starUserSprite.spriteFrame = isActive ? this.lightSpriteFrame : this.graySpriteFrame;
  149. },
  150. // 签约明星
  151. signUseBtnAction() {
  152. /// 只能同时买一个
  153. if (this._listViewAdapter.isBuying) {
  154. return;
  155. }
  156. this._listViewAdapter.isBuying = true;
  157. GameModule.audioMng.playClickButton();
  158. this.starUseBtn.interactable = false;
  159. this._isAcion = true;
  160. this.buyStar().then((respondData) => {
  161. this.starUseBtn.interactable = true;
  162. this._isAcion = false;
  163. GameModule.userInfo.gold = TapTapTool.sub(GameModule.userInfo.gold, this._buyGold);
  164. /// 本地购买的明星数量加1
  165. GameModule.userInfo.buyStarCount += 1;
  166. // this._starData.starRoomCount += 1;
  167. this._starData.starCount += 1;
  168. GameModule.userInfo.perpetualClickMt = respondData.clickMt;
  169. GameModule.userInfo.perpetualMt = respondData.mt;
  170. Global._buyStarGold = respondData.buyGold;
  171. this.undateStarCount();
  172. this._listViewAdapter.isBuying = false;
  173. GameEvent.fire(GameNotificationKey.StarEnterRoom, this._starData.starId, respondData.roomId);
  174. GameEvent.fire("Star_Buy_success", this._starInfo.areaType, respondData.buyGold, respondData.starTypeCount);
  175. GameEvent.fire('Star_Buy_success_Alert', this._starInfo.starId, this._starInfo.name);
  176. }).catch((error) => {
  177. this.starUseBtn.interactable = true;
  178. this._isAcion = false;
  179. Global.commonAlert.showCommonErrorAlert("签约明星失败");
  180. console.error(error);
  181. });
  182. },
  183. buyStar() {
  184. return new Promise((resolve, reject) => {
  185. //// 购买明星
  186. StarApi.buyStar(this._starData.starId, (respondData) => {
  187. resolve(respondData);
  188. }, (code, msg) => {
  189. reject({code, msg});
  190. });
  191. });
  192. },
  193. });