StarItem.js 8.8 KB

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