StarItem.js 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  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. if (this._starData.starRoomCount <= this._starData.starCount) {
  77. this.countLabel.string = this._starData.starRoomCount + '/' + this._starData.starCount;
  78. } else {
  79. this.countLabel.string = this._starData.starCount + '/' + this._starData.starCount;
  80. }
  81. } else {
  82. this.countNode.active = false;
  83. }
  84. },
  85. /// 设置是否能签约
  86. setUpIsCanSign() {
  87. if (this._starData.type === 3) {
  88. this._isRoomLocked = false;
  89. this._isStarLocked = false;
  90. this.subTitleRichText1.string = "";
  91. this.subTitleRichText.string = '';
  92. } else {
  93. let buyStarCount = GameModule.userInfo.buyStarCount;
  94. let roomId = this._starData.signRoomId;
  95. let isUnlocked = GameGlobal.BuildingManager.getRoomIsUnlocked(roomId);
  96. let roomName = GameGlobal.BuildingManager.getRoomName(roomId);
  97. this._isRoomLocked = false;
  98. this._isStarLocked = false;
  99. /// 需要的房间锁住的。
  100. if (isUnlocked == 0) {
  101. this.subTitleRichText.string = "<img src='star_false'/><color=#2E2E2E> 开启 " + roomName + "</c>";
  102. this._isRoomLocked = true;
  103. } else {
  104. this.subTitleRichText.string = "<img src='star_true'/><color=#2E2E2E> 开启 " + roomName + "</c>";
  105. }
  106. let needCount = this._starData.itemCount;
  107. if (needCount > 0) {
  108. /// 如要明星的个数为解锁条件
  109. if (this._starData.itemId == 101) {
  110. /// 如果明星数量足够
  111. let newBuyStarCount = buyStarCount <= needCount ? buyStarCount : needCount;
  112. if (buyStarCount >= needCount) {
  113. this.subTitleRichText1.string = "<img src='star_true'/><color=#2E2E2E>" + this._starInfo.desc + " (" + newBuyStarCount + "/" + needCount + ")" + "</c>";
  114. } else {
  115. this._isStarLocked = true;
  116. this.subTitleRichText1.string = "<img src='star_false'/><color=#2E2E2E>" + this._starInfo.desc + " (" + newBuyStarCount + "/" + needCount + ")" + "</c>";
  117. }
  118. } else if (this._starData.itemId == 102) {
  119. let newStarTypeCount = this._starTypeCount <= needCount ? this._starTypeCount : needCount;
  120. if (this._starTypeCount >= needCount) {
  121. this.subTitleRichText1.string = "<img src='star_true'/><color=#2E2E2E>" + this._starInfo.desc + " (" + newStarTypeCount + "/" + needCount + ")" + "</c>";
  122. } else {
  123. this._isStarLocked = true;
  124. this.subTitleRichText1.string = "<img src='star_fale'/><color=#2E2E2E>" + this._starInfo.desc + " (" + newStarTypeCount + "/" + needCount + ")" + "</c>";
  125. }
  126. } else {
  127. let newAreaStarCount = this._starData.areaStarCount <= needCount ? this._starData.areaStarCount : needCount;
  128. if (this._starData.areaStarCount >= needCount) {
  129. this.subTitleRichText1.string = "<img src='star_true'/><color=#2E2E2E>" + this._starInfo.desc + " (" + newAreaStarCount + "/" + needCount + ")" + "</c>";
  130. } else {
  131. this._isStarLocked = true;
  132. this.subTitleRichText1.string = "<img src='star_false'/><color=#2E2E2E>" + this._starInfo.desc + " (" + newAreaStarCount + "/" + needCount + ")" + "</c>";
  133. }
  134. }
  135. } else {
  136. this.subTitleRichText1.string = "";
  137. }
  138. }
  139. ///说明满足明星和房间的条件 已经解锁 那么只需要判断金币进行预约了
  140. if (this._isStarLocked == false && this._isRoomLocked == false) {
  141. /// 如果金币满足
  142. this.starUseTitleRichText.node.active = true;
  143. this.setUpUseBtnBg(TapTapTool.compare(GameModule.userInfo.gold, this._buyGold));
  144. this.starUseTitleRichText.string = `<color=#ffffff>签约</c><br/><img src='coin_small'/><color=#ffffff>${TapTapTool.parseToString(this._buyGold)}</c>`;
  145. this.starIconBlackBg.active = false;
  146. } else {
  147. this.starUseTitleRichText.node.active = false;
  148. this.starUseBtn.interactable = false;
  149. this.starUserSprite.spriteFrame = this.lockedSpriteFrame;
  150. this.starIconBlackBg.active = true;
  151. }
  152. },
  153. /// 设置签约的button是否能点击
  154. setUpUseBtnBg(isActive) {
  155. this.starUseBtn.interactable = isActive;
  156. this.starUserSprite.spriteFrame = isActive ? this.lightSpriteFrame : this.graySpriteFrame;
  157. },
  158. // 签约明星
  159. signUseBtnAction() {
  160. /// 只能同时买一个
  161. if (this._listViewAdapter.isBuying) {
  162. return;
  163. }
  164. this._listViewAdapter.isBuying = true;
  165. GameModule.audioMng.playClickButton();
  166. this.starUseBtn.interactable = false;
  167. this._isAcion = true;
  168. this.buyStar().then((respondData) => {
  169. this.starUseBtn.interactable = true;
  170. this._isAcion = false;
  171. GameModule.userInfo.gold = TapTapTool.sub(GameModule.userInfo.gold, this._buyGold);
  172. /// 本地购买的明星数量加1
  173. GameModule.userInfo.buyStarCount += 1;
  174. // this._starData.starRoomCount += 1;
  175. this._starData.starCount += 1;
  176. GameModule.userInfo.perpetualClickMt = TapTapTool.multiple(GameModule.userInfo.perpetualClickMt, {'e': 0, 'n': 2});
  177. GameModule.userInfo.perpetualMt = TapTapTool.multiple(GameModule.userInfo.perpetualMt, {'e': 0, 'n': 2});
  178. GameGlobal._buyStarGold = respondData.buyGold;
  179. this.undateStarCount();
  180. this._listViewAdapter.isBuying = false;
  181. GameEvent.fire(GameNotificationKey.StarEnterRoom, this._starData.starId, respondData.roomId);
  182. GameEvent.fire("Star_Buy_success", this._starInfo.areaType, respondData.buyGold, respondData.starTypeCount);
  183. GameEvent.fire('Star_Buy_success_Alert', this._starInfo.starId, this._starInfo.name);
  184. }).catch((error) => {
  185. this.starUseBtn.interactable = true;
  186. this._isAcion = false;
  187. GameGlobal.commonAlert.showCommonErrorAlert("签约明星失败");
  188. console.error(error);
  189. });
  190. },
  191. buyStar() {
  192. return new Promise((resolve, reject) => {
  193. //// 购买明星
  194. StarApi.buyStar(this._starData.starId, (respondData) => {
  195. resolve(respondData);
  196. }, (code, msg) => {
  197. reject({code, msg});
  198. });
  199. });
  200. },
  201. });