HeroInfo.ts 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. import { _decorator, Component, sp, SpriteFrame, Sprite, Label, Node, RichText, CCString } from "cc";
  2. import { BattleData } from "../../battle/BattleData";
  3. import { DataSystem } from "../../core/data/DataSystem";
  4. import { Http, HttpResponseCode } from "../../core/net/Http";
  5. import { PageView } from "../../core/page/PageView";
  6. import { ResourceLoader } from "../../core/resourceManager/ResourceLoader";
  7. import { BitmapFont } from "../../core/ui/BitmapFont";
  8. import { Window } from "../../core/ui/window/Window";
  9. import { WindowOpenMode } from "../../core/ui/window/WindowOpenMode";
  10. import { WindowSystem } from "../../core/ui/window/WindowSystem";
  11. import { StringUtils } from "../../core/utils/StringUtils";
  12. import { ConfigData } from "../../data/ConfigData";
  13. import { HttpErrorCode } from "../../data/HttpErrorCode";
  14. import { UserData } from "../../data/UserData";
  15. import { ReportThinking } from "../../ReportThinking";
  16. import { Formation } from "../formation/Formation";
  17. import { FormationData } from "../formation/FormationData";
  18. import { UserHero, UserHeroData } from "../UserHeroData";
  19. const { ccclass, property, requireComponent } = _decorator;
  20. /**
  21. * 武将动画数据
  22. * @author 袁浩
  23. */
  24. @ccclass("HeroAniData")
  25. export class HeroAniData {
  26. @property({ tooltip: "是否循环播放" })
  27. public loop: boolean = true;
  28. @property({ tooltip: "动画名称" })
  29. public animation = "";
  30. @property({ tooltip: "动画切换周期(秒)" })
  31. public changeAniInterval: number = 5;
  32. }
  33. /**
  34. * 武将信息
  35. * @author 袁浩
  36. */
  37. @ccclass('HeroInfo')
  38. @requireComponent(ResourceLoader)
  39. export class HeroInfo extends Component {
  40. @property({ tooltip: "武将骨骼动画组件", type: sp.Skeleton })
  41. public body: sp.Skeleton;
  42. @property({ tooltip: "星级", type: BitmapFont })
  43. public starBitmapLabel: BitmapFont;
  44. @property({ tooltip: "阵营图标", type: Sprite })
  45. public campIcon: Sprite;
  46. @property({ tooltip: "所有阵营图标", type: [SpriteFrame] })
  47. public campIconList: SpriteFrame[] = [];
  48. @property({ tooltip: "名字", type: Label })
  49. public nameLabel: Label;
  50. @property({ tooltip: "等级", type: Label })
  51. public lvLabel: Label;
  52. @property({ tooltip: "升级图片", type: SpriteFrame })
  53. public upSpriteFrame: SpriteFrame;
  54. @property({ tooltip: "获取图片", type: SpriteFrame })
  55. public getSpriteFrame: SpriteFrame;
  56. @property({ tooltip: "获取/升级按钮", type: Sprite })
  57. public getButton: Sprite;
  58. @property({ tooltip: "碎片进度", type: Sprite })
  59. public chipProgress: Sprite;
  60. @property({ tooltip: "碎片进度文本", type: Label })
  61. public chipLabel: Label;
  62. @property({ tooltip: "箭头图标", type: Sprite })
  63. public arrowIcon: Sprite;
  64. @property({ tooltip: "在阵形中的节点展示", type: Node })
  65. public inFormationNode: Node;
  66. @property({ tooltip: "攻击文本", type: Label })
  67. public attackLabel: Label;
  68. @property({ tooltip: "攻速文本", type: Label })
  69. public attackSpeedLabel: Label;
  70. @property({ tooltip: "统帅文本", type: Label })
  71. public commandLabel: Label;
  72. @property({ tooltip: "技能图标", type: Sprite })
  73. public skillSprite: Sprite;
  74. @property({ tooltip: "技能描述", type: RichText })
  75. public descLabel: RichText;
  76. @property({ tooltip: "切换的动画序列", type: [HeroAniData] })
  77. public aniList: HeroAniData[] = [];
  78. @property({ tooltip: "初始动画索引" })
  79. public aniDefaultIndex: number = 0;
  80. private data: UserHero;
  81. private userHeroData: UserHeroData;
  82. start() {
  83. let params = this.getComponent(Window).params;
  84. let data = params[0];
  85. this.pullUserHeroData();
  86. this.loadHero(data);
  87. }
  88. private async pullUserHeroData() {
  89. let userHeroData = DataSystem.getData(UserHeroData);
  90. // await userHeroData.pull(this.getComponent(Http));
  91. this.userHeroData = userHeroData;
  92. }
  93. private async loadHero(data: UserHero) {
  94. let isOld = this.data && this.data.server.id == data.server.id;
  95. this.data = data;
  96. let starString = "";
  97. let star = !!data.server ? data.server.star : data.client.initStar;
  98. for (let i = 0; i < star; i++) {
  99. starString += "0";
  100. }
  101. this.starBitmapLabel.string = starString;
  102. this.campIcon.spriteFrame = this.campIconList[data.client.camp - 1];
  103. this.nameLabel.string = data.client.name;
  104. this.lvLabel.string = `Lv.${data.server.lv}`;
  105. let starConfig = DataSystem.getData(ConfigData).get("star")[data.server.star];
  106. if (starConfig) {
  107. this.chipProgress.fillRange = Math.min(1, data.server.chip / starConfig.nextReq);
  108. this.chipLabel.string = `${data.server.chip}/${starConfig.nextReq}`;
  109. if (data.server.chip >= starConfig.nextReq) {
  110. this.getButton.spriteFrame = this.upSpriteFrame;
  111. }
  112. else {
  113. this.getButton.spriteFrame = this.getSpriteFrame;
  114. }
  115. this.getButton.node.active = true;
  116. }
  117. else {//满级
  118. this.chipProgress.fillRange = 1;
  119. this.chipLabel.string = `已满星`;
  120. this.getButton.node.active = false;
  121. }
  122. this.attackLabel.string = this.formateValue(data.client.attack + (data.server.lv - 1) * parseInt(data.client.upRatio.split(",")[data.server.star - 1]));
  123. this.attackSpeedLabel.string = this.formateValue(data.client.attSpeed);
  124. this.commandLabel.string = this.formateValue(data.client.commander);
  125. let skill = DataSystem.getData(ConfigData).get("skill")[data.client.skillID];
  126. this.descLabel.string = StringUtils.getRichText(skill.des);
  127. this.inFormationNode.active = !!DataSystem.getData(FormationData).getIDByHero(data.server.id);
  128. this.skillSprite.spriteFrame = await this.getComponent(ResourceLoader).load(`images/skill/${skill.icon}/spriteFrame`, SpriteFrame);
  129. if (!isOld) {//只有武将编号变化才重新加载骨骼动画
  130. this.body.skeletonData = await this.getComponent(ResourceLoader).load(`spine/hero/${data.client.hero_res}`, sp.SkeletonData);
  131. this.body.setAnimation(0, this.aniList[this.aniDefaultIndex].animation, this.aniList[this.aniDefaultIndex].loop);
  132. }
  133. }
  134. private formateValue(num: number) {
  135. return num > 10000 ? (num / 1000).toFixed(1) + "k" : num + "";
  136. }
  137. public async upStar() {
  138. let starConfig = DataSystem.getData(ConfigData).get("star")[this.data.server.star];
  139. if (this.data.server.chip >= starConfig.nextReq) {//碎片足够则升星
  140. let result = await this.getComponent(Http).send("/api/hero/upStar", { id: this.data.server.id });
  141. if (result && result.code == HttpResponseCode.Success) {//升星成功
  142. let oldStar = this.data.server.star;
  143. this.data.server.chip = result.data;
  144. this.data.server.star++;
  145. DataSystem.getData(UserHeroData).set(this.data.client.id, this.data);
  146. this.loadHero(this.data);
  147. this.setBattleHerosData(this.data.server.id);
  148. ReportThinking.hero_levelup(this.data.client.name, starConfig.nextReq, this.data.server.lv, this.data.server.chip, oldStar, oldStar + 1);
  149. //打开升星界面
  150. WindowSystem.open("prefabs/ui/hero/upstar", WindowOpenMode.NotCloseAndCover, [this.data, oldStar]);
  151. }
  152. else if (result && result.code == HttpErrorCode.LessChip) {//碎片不足则重新同步碎片
  153. this.data.server.chip = result.data;
  154. DataSystem.getData(UserHeroData).set(this.data.client.id, this.data);
  155. }
  156. }
  157. else {//打开招募界面
  158. WindowSystem.open("prefabs/ui/recruit/recruit", WindowOpenMode.NotCloseAndAdd);
  159. }
  160. }
  161. /**更新战斗武将星级*/
  162. private setBattleHerosData(id: number) {
  163. let userData = DataSystem.getData(UserData);
  164. for (let i = 1; i < 6; i++) {
  165. let battleHeroData = DataSystem.getData(BattleData).heros.get(i);
  166. if (battleHeroData && battleHeroData.id && battleHeroData.id == id) {
  167. battleHeroData.starLv++;
  168. }
  169. }
  170. }
  171. public nextHero() {
  172. if (!this.userHeroData) {//如果用户数据没加载完成
  173. return;
  174. }
  175. let formationData = DataSystem.getData(FormationData);
  176. this.loadHero(this.userHeroData.get(formationData.nextHexo(this.data.server.id)));
  177. }
  178. public preHero() {
  179. if (!this.userHeroData) {//如果用户数据没加载完成
  180. return;
  181. }
  182. let formationData = DataSystem.getData(FormationData);
  183. this.loadHero(this.userHeroData.get(formationData.preHero(this.data.server.id)));
  184. }
  185. private lastTime: number = 0;
  186. update(dt: number) {
  187. this.lastTime += dt;
  188. if (this.lastTime > this.aniList[this.aniDefaultIndex].changeAniInterval) {
  189. this.playNextAni();
  190. }
  191. if (DataSystem.watch(UserHeroData, this.data.client.id)) {
  192. this.loadHero(DataSystem.getData(UserHeroData).get(this.data.client.id));
  193. }
  194. }
  195. private playNextAni() {
  196. this.lastTime = 0;
  197. this.aniDefaultIndex++;
  198. if (this.aniDefaultIndex >= this.aniList.length) {
  199. this.aniDefaultIndex = 0;
  200. }
  201. this.body.setAnimation(0, this.aniList[this.aniDefaultIndex].animation, this.aniList[this.aniDefaultIndex].loop);
  202. this.body.setCompleteListener(() => {
  203. if (!this.aniList[this.aniDefaultIndex].loop) {
  204. this.playNextAni();
  205. }
  206. });
  207. }
  208. public changeHero() {
  209. let windowList = WindowSystem.getWindowList();
  210. let formationIsOpen = false;
  211. for (let i = 0; i < windowList.length; i++) {
  212. const _window = windowList[i];
  213. let pageView = _window.getComponent(PageView);
  214. if (pageView && pageView.pages[1] && pageView.pages[1].getComponent(Formation)) {
  215. formationIsOpen = true;
  216. pageView.selectedIndex = 1;
  217. this.getComponent(Window).close();
  218. }
  219. }
  220. !formationIsOpen && WindowSystem.open("prefabs/ui/hero/heroUI", WindowOpenMode.CloseAndAdd, [1]);
  221. }
  222. }