Formation.ts 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. import { _decorator, Component, Toggle, Node, EventTouch, SystemEventType, Sprite, SpriteFrame, Label, Animation, RichText, ToggleContainer } from "cc";
  2. import { DataSystem } from "../../core/data/DataSystem";
  3. import { Http, HttpResponseCode } from "../../core/net/Http";
  4. import { ResourceLoader } from "../../core/resourceManager/ResourceLoader";
  5. import { Sound } from "../../core/sound/Sound";
  6. import { DragSource } from "../../core/ui/drag/DragSource";
  7. import { DragSystem } from "../../core/ui/drag/DragSystem";
  8. import { DragTarget } from "../../core/ui/drag/DragTarget";
  9. import List from "../../core/ui/virtualList/List";
  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 { PublicLogicData } from "../../main/PublicLogicData";
  15. import { UserHeroData } from ".././UserHeroData";
  16. import { FormationData } from "./FormationData";
  17. import { FormationHeroItem } from "./FormationHeroItem";
  18. import { FormationOne } from "./FormationOne";
  19. const { ccclass, property, requireComponent } = _decorator;
  20. /**
  21. * 阵位
  22. * @author 袁浩
  23. */
  24. @ccclass('Formation')
  25. @requireComponent(Http)
  26. export class Formation extends Component {
  27. @property({ tooltip: "武将列表", type: List })
  28. public list: List;
  29. @property({ tooltip: "所有的阵位", type: [FormationOne] })
  30. public formationOneList: FormationOne[] = [];
  31. @property({ tooltip: "八卦图标", type: Sprite })
  32. public baguaIcon: Sprite;
  33. @property({ tooltip: "八卦动画", type: Animation })
  34. public baguaAni: Animation;
  35. @property({ tooltip: "太极动画", type: Animation })
  36. public taiji: Animation;
  37. @property({ tooltip: "攻击加成文本", type: Label })
  38. public attackLabel: Label;
  39. @property({ tooltip: "暴击加成文本", type: Label })
  40. public criLabel: Label;
  41. @property({ tooltip: "阵营加成文本", type: RichText })
  42. public nationLabel: RichText;
  43. @property({ tooltip: "阵营选项卡", type: ToggleContainer })
  44. public campToggleContainer: ToggleContainer;
  45. private heroList: any[];
  46. start() {
  47. for (let i = 0; i < this.formationOneList.length; i++) {//因为cocos预制体组件属性覆盖的BUG,不得不耦合本组件和单个阵位组件
  48. this.formationOneList[i].formation = this;
  49. }
  50. this.list.node.on(SystemEventType.TOUCH_CANCEL, this.onTouchEnd, this);
  51. this.list.node.on(SystemEventType.TOUCH_END, this.onTouchEnd, this);
  52. this.initUI(DataSystem.getData(FormationData));
  53. this.loadHeros(DataSystem.getData(PublicLogicData).enterFormationType == 1 ? this.getCampByHasHero() : this.getCampByMarshalHero());
  54. }
  55. private async initUI(formationData: FormationData) {
  56. // await formationData.pull(this.getComponent(Http));
  57. let fetter = formationData.getFetter();
  58. let fetterConfig = DataSystem.getData(ConfigData).get("fetter")[fetter.fetterID];
  59. let baguaIcon = !fetterConfig ? 'bagua1' : fetterConfig.icon;
  60. this.taiji.node.active = this.baguaAni.node.active = !!fetterConfig;
  61. this.attackLabel.string = `+${fetter.attackPer / 100}%`;
  62. this.criLabel.string = `+${fetter.cri / 100}%`;
  63. if (fetter.nationID) {
  64. let buffConfig = DataSystem.getData(ConfigData).get("buff")[fetter.nationID];
  65. this.nationLabel.string = StringUtils.getRichText(buffConfig.des2);
  66. }
  67. else {
  68. this.nationLabel.string = "";
  69. }
  70. this.baguaIcon.spriteFrame = await this.getComponent(ResourceLoader).load(`images/general/texture/${baguaIcon}/spriteFrame`, SpriteFrame);
  71. }
  72. private onTouchEnd(event: EventTouch) {
  73. DragSystem.onTouchEnd(event);
  74. }
  75. /**
  76. * 当阵位页面显示
  77. */
  78. public onShow() {
  79. this.loadHeros(DataSystem.getData(PublicLogicData).enterFormationType ? this.getCampByHasHero() : this.getCampByMarshalHero());
  80. for (let i = 0; i < this.formationOneList.length; i++) {
  81. this.formationOneList[i].fixSpine();
  82. }
  83. this.initUI(DataSystem.getData(FormationData));
  84. }
  85. /**
  86. * 当点击阵营项
  87. * @param toggle
  88. */
  89. public onCamp(toggle: Toggle) {
  90. this.loadHeros(toggle._toggleContainer.toggleItems.indexOf(toggle) + 1);
  91. }
  92. /**
  93. * 获取有武将的阵营
  94. * @returns
  95. */
  96. private getCampByHasHero() {
  97. let userHeroData = DataSystem.getData(UserHeroData);
  98. for (let i = 1; i < 5; i++) {
  99. for (let j = 0; j < userHeroData.haveHeros.length; j++) {
  100. if (userHeroData.get(userHeroData.haveHeros[j]).client.camp == i) {
  101. return i;
  102. }
  103. }
  104. }
  105. return 1;
  106. }
  107. /**
  108. * 获取统帅武将阵营 若没有返回1
  109. */
  110. private getCampByMarshalHero() {
  111. let userHeroData = DataSystem.getData(UserHeroData);
  112. let formationData = DataSystem.getData(FormationData);
  113. if (formationData.get(5)) return userHeroData.get(formationData.get(5)).client.camp;
  114. else return 1;
  115. }
  116. /**
  117. * 根据阵营加载已拥有英雄数据并展示到UI
  118. * @param camp 阵营
  119. */
  120. public async loadHeros(camp: number) {
  121. this.campToggleContainer.toggleItems[camp - 1].isChecked = true;
  122. let heroList = [];
  123. let userHeroData = DataSystem.getData(UserHeroData);
  124. // await userHeroData.pull(this.getComponent(Http));
  125. for (let i = 0; i < userHeroData.haveHeros.length; i++) {
  126. const hero = userHeroData.get(userHeroData.haveHeros[i]);
  127. if (camp == 0 || hero.client.camp == camp) {
  128. heroList.push(hero.client.id);
  129. }
  130. }
  131. this.heroList = userHeroData.sort(heroList, camp, heroList);
  132. this.list.numItems = this.heroList.length;
  133. }
  134. /**
  135. * 当列表项渲染
  136. * @param item 项节点
  137. * @param index 项索引
  138. */
  139. public onListRender(item: Node, index: number) {
  140. item.getComponent(FormationHeroItem).onDataChange(this.heroList[index], index);
  141. }
  142. /**
  143. * 当放下拖拽
  144. * @param source
  145. */
  146. public async onDrop(source: DragSource, target: DragTarget) {
  147. let oldFormationOne = source.getComponent(FormationOne);
  148. let heroData = (source.getComponent(FormationHeroItem) || source.getComponent(FormationOne)).data;
  149. if (!heroData) {
  150. return;
  151. }
  152. let formatoinID = 0;
  153. for (let i = 0; i < this.formationOneList.length; i++) {
  154. const formationOne = this.formationOneList[i];
  155. if (formationOne.getComponent(DragTarget) == target) {
  156. formatoinID = formationOne.id;//目标阵位
  157. break;
  158. }
  159. }
  160. let formationData = DataSystem.getData(FormationData);
  161. let formationObj: { [key: number]: number } = formationData.object;
  162. for (let key in formationObj) {
  163. if (formationObj[key] == heroData.client.id) {//如果别的阵位有此武将
  164. formationObj[key] = formationObj[formatoinID];
  165. }
  166. }
  167. oldFormationOne && (formationObj[oldFormationOne.id] = formationObj[formatoinID]);//如果是阵位拖拽到阵位上并且目标阵位有武将
  168. formationObj[formatoinID] = heroData.client.id;//设置阵位武将
  169. this.getComponent(Sound).play();//播放音效
  170. let result = await this.getComponent(Http).send("/api/formation/syncFormation", { formation: formationObj });
  171. if (result && result.code == HttpResponseCode.Success) {//阵位改变成功
  172. formationData.copy(result.data, true);
  173. this.initUI(formationData);
  174. }
  175. else {//如果修改失败则还原阵位
  176. if (result.code == HttpErrorCode.LessQuality) {
  177. WindowSystem.showTips("武将品质不足,无法上阵统帅位");
  178. }
  179. formationData.copy(formationData.object, true);
  180. }
  181. }
  182. }