BookHeroItem.ts 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. import { _decorator, Component, Node, SystemEventType, Label, SpriteFrame, Sprite, ProgressBar, EventTouch, Vec2 } from 'cc';
  2. import { DataSystem } from '../../core/data/DataSystem';
  3. import { Http } from '../../core/net/Http';
  4. import { HttpSystem } from '../../core/net/HttpSystem';
  5. import { ResourceLoader } from '../../core/resourceManager/ResourceLoader';
  6. import { BitmapFont } from '../../core/ui/BitmapFont';
  7. import { OpenWindow } from '../../core/ui/window/OpenWindow';
  8. import { WindowOpenMode } from '../../core/ui/window/WindowOpenMode';
  9. import { WindowSystem } from '../../core/ui/window/WindowSystem';
  10. import { ConfigData } from '../../data/ConfigData';
  11. import { HttpErrorCode } from '../../data/HttpErrorCode';
  12. import { platform } from '../../data/jsb/platform';
  13. import { UserData } from '../../data/UserData';
  14. import { RedPoint } from '../../main/RedPoint';
  15. import { ReportThinking } from '../../ReportThinking';
  16. import { FormationData } from '../formation/FormationData';
  17. import { UserHero, UserHeroData } from '../UserHeroData'
  18. const { ccclass, property, requireComponent } = _decorator;
  19. /**
  20. * 图鉴英雄项
  21. * @author 袁浩
  22. */
  23. @ccclass('BookHeroItem')
  24. @requireComponent(ResourceLoader)
  25. export class BookHeroItem extends Component {
  26. @property({ tooltip: "即将解锁", type: Node })
  27. public lockChipNode: Node;
  28. @property({ tooltip: "已解锁并只有碎片", type: Node })
  29. public unlockNode: Node;
  30. @property({ tooltip: "已上阵显示的节点", type: Node })
  31. public haveNode: Node;
  32. @property({ tooltip: "名字", type: Label })
  33. public nameLabel: Label;
  34. @property({ tooltip: "名字背景", type: Sprite })
  35. public nameBg: Sprite;
  36. @property({ tooltip: "所有名字背景", type: [SpriteFrame] })
  37. public nameBgList: SpriteFrame[] = [];
  38. @property({ tooltip: "品质背景", type: Sprite })
  39. public campBg: Sprite;
  40. @property({ tooltip: "所有品质背景", type: [SpriteFrame] })
  41. public campBgList: SpriteFrame[] = [];
  42. @property({ tooltip: "阵营图标", type: Sprite })
  43. public campIcon: Sprite;
  44. @property({ tooltip: "所有阵营图标", type: [SpriteFrame] })
  45. public campIconList: SpriteFrame[] = [];
  46. @property({ tooltip: "品质框", type: Sprite })
  47. public qualityBorder: Sprite;
  48. @property({ tooltip: "所有品质框", type: [SpriteFrame] })
  49. public qualityBorderList: SpriteFrame[] = [];
  50. @property({ tooltip: "解锁文本提示", type: Label })
  51. public unlockLabel: Label;
  52. @property({ tooltip: "武将原画", type: Sprite })
  53. public body: Sprite;
  54. @property({ tooltip: "星级", type: BitmapFont })
  55. public starBitmapLabel: BitmapFont;
  56. @property({ tooltip: "操作文本", type: Label })
  57. public operateLabel: Label;
  58. @property({ tooltip: "碎片进度", type: Sprite })
  59. public chipProgress: Sprite;
  60. @property({ tooltip: "碎片进度文本", type: Label })
  61. public chipLabel: Label;
  62. @property({ tooltip: "用于打开武将信息的组件", type: OpenWindow })
  63. public openHeroInfo: OpenWindow;
  64. @property({ tooltip: "红点提示", type: Sprite })
  65. public redPoint: Sprite;
  66. public data: UserHero;
  67. public heroList: UserHero[];
  68. public index: number;
  69. start() {
  70. this.node.on(SystemEventType.TOUCH_END, this.onItemTouch, this);
  71. }
  72. update() {
  73. if (this.data && DataSystem.watch(UserHeroData, this.data.client.id + "")) {//如果英雄数据发生变化
  74. this.heroList[this.index].server = DataSystem.getData(UserHeroData).get(this.data.client.id).server;
  75. this.onDataChange(this.heroList, this.index);
  76. }
  77. }
  78. public async onDataChange(heroList: UserHero[], index: number) {
  79. this.data = heroList[index];
  80. this.heroList = heroList;
  81. this.index = index;
  82. this.lockChipNode.active = !this.data.client.isunlock;
  83. this.campIcon.enabled = !this.lockChipNode.active;
  84. this.unlockNode.active = !!this.data.client.isunlock && (!this.data.server || !this.data.server.active);
  85. this.haveNode.active = !!this.data.server && !!DataSystem.getData(FormationData).getIDByHero(this.data.client.id);
  86. this.nameLabel.string = this.data.client.name;
  87. this.campBg.spriteFrame = this.campBgList[this.data.client.color - 1];
  88. this.nameBg.spriteFrame = this.nameBgList[this.data.client.color - 1];
  89. this.campIcon.spriteFrame = this.campIconList[this.data.client.camp - 1];
  90. this.qualityBorder.spriteFrame = this.qualityBorderList[this.data.client.color - 1];
  91. this.unlockLabel.string = !!this.data.client.unlockLv ? `兵营${this.data.client.unlockLv}级解锁` : '';
  92. let starString = "";
  93. let star = !!this.data.server ? this.data.server.star : this.data.client.initStar;
  94. for (let i = 0; i < star; i++) {
  95. starString += "0";
  96. }
  97. this.starBitmapLabel.string = starString;
  98. let chip = !!this.data.server ? this.data.server.chip : 0;
  99. this.chipProgress.fillRange = Math.min(1, chip / this.data.client.needChip);
  100. this.chipLabel.string = `${chip}/${this.data.client.needChip}`;
  101. this.operateLabel.string = `${chip >= this.data.client.needChip ? '合成>' : '获取>'}`;
  102. if (this.data.server && !this.data.server.active && chip >= this.data.client.needChip) {
  103. this.redPoint.node.active = true;
  104. }
  105. else {
  106. if (this.data && this.data.server && this.data.server.active) {
  107. let starConfig = DataSystem.getData(ConfigData).get("star");
  108. if (starConfig[this.data.server.star] && this.data.server.chip >= starConfig[this.data.server.star].nextReq) {
  109. this.redPoint.node.active = true;
  110. }
  111. else {
  112. this.redPoint.node.active = false;
  113. }
  114. }
  115. else {
  116. this.redPoint.node.active = false;
  117. }
  118. }
  119. this.body.spriteFrame = await this.getComponent(ResourceLoader).load(`images/general/texture/hero_img/${this.data.client.hero_res}/spriteFrame`, SpriteFrame);
  120. }
  121. private async onItemTouch(event: EventTouch) {
  122. if (Vec2.distance(event.getUILocation(), event.getUIStartLocation()) > 20) {
  123. return;
  124. }
  125. if (this.lockChipNode.active) {//未解锁的不做处理
  126. return;
  127. }
  128. if (!!this.data.server && this.data.server.active) {//已经解锁且已经获得
  129. //打开武将信息界面
  130. this.openHeroInfo.open(this.data);
  131. }
  132. else if (this.unlockNode.active) {//已解锁且只有碎片
  133. let chip = !!this.data.server ? this.data.server.chip : 0;
  134. if (chip >= this.data.client.needChip) {//合成
  135. let result = await this.getComponent(Http).send("/api/hero/compose", { id: this.data.client.id });
  136. let userHeroData = DataSystem.getData(UserHeroData);
  137. if (result.data) {
  138. let heroID = parseInt(result.data.id);
  139. let chip = result.data.chip;
  140. let hero = userHeroData.get(heroID);
  141. //更新碎片
  142. hero.server.chip = chip;
  143. //等级为兵营等级
  144. hero.server.lv = DataSystem.getData(UserData).campLv;
  145. //设置为已拥有
  146. hero.server.active = true;
  147. //更新英雄数据
  148. userHeroData.set(heroID, hero);
  149. userHeroData.haveHeros.push(heroID);
  150. let index = userHeroData.notHavetHeros.indexOf(heroID);
  151. index != -1 && userHeroData.notHavetHeros.splice(index, 1);
  152. ReportThinking.hero_activate(this.data.client.name, 'hero_compose');
  153. //打开升星界面
  154. WindowSystem.open("prefabs/ui/hero/compose", WindowOpenMode.NotCloseAndCover, [this.data]);
  155. }
  156. else if (result.code == HttpErrorCode.SendDataError) {
  157. userHeroData.pull(this.getComponent(Http));
  158. }
  159. }
  160. else {//打开招募界面
  161. WindowSystem.open("prefabs/ui/recruit/recruit", WindowOpenMode.NotCloseAndAdd);
  162. }
  163. }
  164. }
  165. }