StarItem.js 10 KB

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