RecruitWishUI.ts 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. import { _decorator, Component, Node, Label, Sprite, Prefab, instantiate, SpriteFrame, EventHandler } from 'cc';
  2. import { RewardVideoSystem } from '../../ad/RewardVideoSystem';
  3. import { DataSystem } from '../../core/data/DataSystem';
  4. import { Http, HttpResponseCode } from '../../core/net/Http';
  5. import { HttpSystem } from '../../core/net/HttpSystem';
  6. import { ResourceLoader } from '../../core/resourceManager/ResourceLoader';
  7. import { Window } from '../../core/ui/window/Window';
  8. import { WindowSystem } from '../../core/ui/window/WindowSystem';
  9. import { ConfigData } from '../../data/ConfigData';
  10. import { platform } from '../../data/jsb/platform';
  11. import { UserData } from '../../data/UserData';
  12. import { NoviceGuideData } from '../../guide/NoviceGuideData';
  13. import { FormationData } from '../../hero/formation/FormationData';
  14. import { Hero_Client, UserHeroData } from '../../hero/UserHeroData';
  15. import { ReportThinking } from '../../ReportThinking';
  16. import { TurntableUI } from '../../turntable/TurntableUI';
  17. import { RecruitData } from '../RecruitData';
  18. import { WishItem } from './WishItem';
  19. const { ccclass, property } = _decorator;
  20. /**
  21. * 心愿武将页
  22. * @author 郑聂华
  23. */
  24. @ccclass('RecruitWishUI')
  25. export class RecruitWishUI extends Component {
  26. @property({ type: ResourceLoader, displayName: "资源加载组件", tooltip: "资源加载组件" }) res: ResourceLoader = null;
  27. @property({ type: Node, displayName: '心愿背景框', tooltip: "心愿背景框" }) wishRect: Node = null;
  28. @property({ type: Label, displayName: '文本_倍率', tooltip: "文本_倍率" }) txtRate: Label = null;
  29. @property({ type: Sprite, displayName: '图_心愿武将', tooltip: "图_心愿武将" }) imgWish: Sprite = null;
  30. @property({ type: Label, displayName: '文本_心愿武将名', tooltip: "文本_心愿武将名" }) txtName: Label = null;
  31. @property({ type: Sprite, displayName: '图_心愿武将阵营', tooltip: "图_心愿武将阵营" }) imgWishCamp: Sprite = null;
  32. @property({ type: Node, displayName: "Content", tooltip: "Content" }) content: Node = null;
  33. @property({ type: Prefab, displayName: "已拥有武将Item", tooltip: "已拥有武将Item" }) wishItemPrefab: Prefab = null;
  34. @property({ type: Node, displayName: "广告图标_按钮", tooltip: "广告图标_按钮" }) iconAd: Node = null;
  35. @property({ type: Node, displayName: "广告按钮", tooltip: "广告按钮" }) btnAd: Node = null;
  36. @property({ type: Node, displayName: "广告按钮置灰", tooltip: "广告按钮置灰" }) btnAdGray: Node = null;
  37. @property({ type: EventHandler, displayName: "心愿武将回调", tooltip: "心愿武将回调" }) private wishItemBack: EventHandler;
  38. private selectIndex: number = -1; //选中的index
  39. private tempWishId: number = -1; //选中的武将id
  40. private wishItemAry: WishItem[] = []; //武将item
  41. async start() {
  42. DataSystem.getData(NoviceGuideData).openRecuitWish = true;
  43. //请求心愿武将列表 目前没数据
  44. let quality: any[] = DataSystem.getData(ConfigData).get("serverConfig").needQuality.split(",");
  45. let heroData = DataSystem.getData(UserHeroData);
  46. let formationData = DataSystem.getData(FormationData);
  47. await heroData.pull(this.getComponent(Http));
  48. let tempGenerals: Hero_Client[] = [];
  49. for (let i = 0; i < heroData.haveHeros.length; i++) {
  50. let grl = heroData.get(heroData.haveHeros[i]).client;// .notHavetHeros[i].client;
  51. if (quality.indexOf(grl.color) != -1)
  52. tempGenerals.push(grl);
  53. }
  54. let self = this;
  55. tempGenerals.sort(function (a, b) {
  56. let aFormat = self.isHasFormation(formationData, a.id);// formationData.get(a.id) && formationData.get(a.id) > 0;
  57. let bFormat = self.isHasFormation(formationData, b.id);//formationData.get(b.id) && formationData.get(b.id) > 0;
  58. if (aFormat == bFormat == true) {
  59. if (a.color == b.color) {
  60. if (a.initStar == b.initStar) {
  61. if (a.camp == b.camp) {
  62. return b.id - a.id;
  63. } else {
  64. return a.camp - b.camp;//正营 魏蜀吴群 1-4
  65. }
  66. } else {
  67. return b.initStar - a.initStar;
  68. }
  69. } else {
  70. return (Number)(b.color) - (Number)(a.color);
  71. }
  72. } else {
  73. return aFormat ? -1 : 1;
  74. }
  75. });
  76. console.log("WishID: " + DataSystem.getData(RecruitData).curWishId);
  77. for (let i = 0; i < tempGenerals.length; i++) {
  78. let grl = tempGenerals[i];
  79. let tempscr = instantiate(this.wishItemPrefab).getComponent(WishItem);
  80. tempscr.selectBack = this.wishItemBack;
  81. let hasFormat = this.isHasFormation(formationData, grl.id);
  82. tempscr.init(grl, i, hasFormat);
  83. tempscr.node.active = true;
  84. this.content.addChild(tempscr.node);
  85. this.wishItemAry.push(tempscr);
  86. }
  87. }
  88. update() {
  89. DataSystem.watch(NoviceGuideData, ' isRecuitWish') && this.onClickAd();
  90. }
  91. /**阵位上是否有指定武将*/
  92. private isHasFormation(formationData: FormationData, heroId: number) {
  93. for (let i = 0; i < 5; i++) {
  94. if (formationData.get(i + 1) && formationData.get(i + 1) == heroId) return true;
  95. }
  96. return false;
  97. }
  98. private async seletWishItemBack(wishId: number, index: number, rate: string = "") {
  99. if (this.tempWishId == wishId) {
  100. this.tempWishId = this.selectIndex = -1;
  101. index >= 0 && (this.wishItemAry[index].select.active = false);
  102. } else {
  103. wishId > 0 && (this.tempWishId = wishId);
  104. this.selectIndex >= 0 && (this.wishItemAry[this.selectIndex].select.active = false);
  105. index >= 0 && (this.wishItemAry[index].select.active = true);
  106. this.selectIndex = index;
  107. }
  108. let heroDataOne: Hero_Client;
  109. console.log("tempwishid: " + this.tempWishId + " index: " + index);
  110. if (this.tempWishId > 0) {
  111. DataSystem.getData(NoviceGuideData).changeHero = true;
  112. heroDataOne = DataSystem.getData(ConfigData).get("general")[this.tempWishId] as Hero_Client;
  113. this.imgWish.spriteFrame = await this.res.load<SpriteFrame>("images/general/texture/head_img/" + heroDataOne.hero_res + "/spriteFrame", SpriteFrame);
  114. this.imgWishCamp.spriteFrame = await this.res.load<SpriteFrame>("images/general/texture/icon_camp_" + heroDataOne.camp + "/spriteFrame", SpriteFrame);
  115. }
  116. this.imgWish.node.active = this.tempWishId > 0;
  117. this.wishRect.active = this.tempWishId < 0;
  118. this.txtName.string = this.tempWishId > 0 ? heroDataOne.name + "" : "指定武将";
  119. this.txtRate.string = this.tempWishId > 0 ? rate : "";
  120. this.iconAd.active = this.tempWishId > 0 && this.tempWishId != DataSystem.getData(RecruitData).curWishId;
  121. this.btnAdGray.active = this.tempWishId == DataSystem.getData(RecruitData).curWishId;
  122. this.btnAd.active = this.tempWishId != DataSystem.getData(RecruitData).curWishId;
  123. }
  124. private async changeWishAd() {
  125. let resultAd = await this.getComponent(Http).send("/api/ad/watchVideoAD");
  126. if (resultAd && resultAd.code == HttpResponseCode.Success) {
  127. ReportThinking.ad_init('change_wish');
  128. let adData = await RewardVideoSystem.show();
  129. if (adData) {
  130. let result = await this.getComponent(Http).send("/api/recruit/changeWish", { id: this.tempWishId, adData: adData.obj });
  131. console.log("Change: ", result);
  132. if (result && result.code == 0) {
  133. ReportThinking.ad_end('change_wish');
  134. DataSystem.getData(RecruitData).curWishId = result.data.heroID;// DataSystem.getData(RecruitData).curWishIdTemp;
  135. DataSystem.getData(RecruitData).wishHeroRate = result.data.up;
  136. this.txtRate.string = result.data.up + "";
  137. WindowSystem.close(this.getComponent(Window));
  138. }
  139. }
  140. }
  141. }
  142. /**清空心愿武将*/
  143. private async changeWishClear() {
  144. let result = await this.getComponent(Http).send("/api/recruit/changeWish", { id: null, adData: null });
  145. if (result && result.code == 0) {
  146. DataSystem.getData(RecruitData).curWishId = -1;
  147. WindowSystem.close(this.getComponent(Window));
  148. }
  149. }
  150. private onClickAd() {
  151. if (this.tempWishId > 0) {
  152. this.tempWishId != DataSystem.getData(RecruitData).curWishId
  153. && this.changeWishAd(); //选中心愿武将
  154. } else {
  155. DataSystem.getData(RecruitData).curWishId > 0
  156. && this.changeWishClear(); //清空心愿武将
  157. }
  158. }
  159. private onClickAdGray() {
  160. console.log("you click ad gray btn");
  161. }
  162. }