PVP.ts 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. import { _decorator, Component, Prefab, Node, instantiate, Sprite, SpriteFrame, Label } from 'cc';
  2. import { MissionData } from '../battle/MissionData';
  3. import { DataSystem } from '../core/data/DataSystem';
  4. import { Http, HttpResponseCode } from '../core/net/Http';
  5. import { Window } from '../core/ui/window/Window';
  6. import { ReportThinking } from '../ReportThinking';
  7. import { PVPCardArray } from './PVPCardArray';
  8. import { PVPHero } from './PVPHero';
  9. import { PVPHeroUI } from './PVPHeroUI';
  10. import { PVPHeroData, PVPLogic, PVPPage, PVPPageType } from './PVPLogic';
  11. import { PVPResult } from './PVPResult';
  12. const { ccclass, property } = _decorator;
  13. @ccclass('PVP')
  14. export class PVP extends Component {
  15. @property({ type: PVPCardArray, tooltip: "底部卡牌组" }) bottomCards: PVPCardArray;
  16. @property({ type: PVPCardArray, tooltip: "顶部卡牌组" }) topCards: PVPCardArray;
  17. @property({ type: [Node], tooltip: "移动坐标点集合" }) posArray: Array<Node> = [];
  18. @property({ type: Prefab, tooltip: "pvp英雄预制体" }) pvpHeroPrefab: Prefab;
  19. @property({ type: Prefab, tooltip: "pvp英雄UI预制体" }) pvpHeroUiPrefab: Prefab;
  20. @property({ type: Node, tooltip: "战斗层" }) battleLayer: Node;
  21. @property({ type: Sprite, tooltip: "八卦图" }) bagua: Sprite;
  22. @property({ type: [SpriteFrame], tooltip: "八卦图合集" }) baguaSpriteList: Array<SpriteFrame> = [];
  23. @property({ type: Label, tooltip: "攻击加成文本" }) gjLabel: Label;
  24. @property({ type: Label, tooltip: "暴击加成文本" }) bjLabel: Label;
  25. @property({ type: PVPResult, tooltip: "结算界面" }) pvpResult: PVPResult;
  26. @property({ type: Http, tooltip: "http组件" }) http: Http;
  27. private g1: Array<PVPCardData> = [];
  28. private g2: Array<PVPCardData> = [];
  29. private key: number;
  30. private isWin: boolean = false;
  31. private pages: Array<PVPPage> = [];
  32. private index = 0;
  33. private g1Node: Node;
  34. private g2Node: Node;
  35. private isSkip = false;
  36. start() {
  37. DataSystem.getData(MissionData).isInPvp = true;
  38. this.pvpResult.closehandler = this.close.bind(this);
  39. this.setData(this.getComponent(Window).params[0]);
  40. }
  41. onDestroy() {
  42. DataSystem.getData(MissionData).isInPvp = false;
  43. }
  44. public setData(data: { g1: Array<PVPCardData>, g2: Array<PVPCardData>, key: number, baseHit: number, buff: any }) {
  45. this.g1 = data.g1.concat();
  46. this.g2 = data.g2.concat();
  47. this.key = data.key;
  48. let logic = new PVPLogic(this.g1, this.g2, data.key, data.baseHit, data.buff);
  49. let logicData = logic.begin();
  50. this.pages = logicData.pages.concat();
  51. this.bagua.spriteFrame = this.baguaSpriteList[logic.fetterID];
  52. this.gjLabel.string = `+ ${logic.g1Camp.attack / 100}%`;
  53. this.bjLabel.string = `+ ${logic.g1Camp.cri / 100}%`;
  54. this.isWin = logicData.winner == 1;
  55. this.bottomCards.setData(this.g1);
  56. this.topCards.setData(this.g2);
  57. ReportThinking.fight_pvp(this.g1.concat(), this.isWin);
  58. this.doBattle(this.index);
  59. }
  60. /**
  61. * 对战递归
  62. * @param index 战报索引
  63. */
  64. private doBattle(index: number): void {
  65. if (this.isSkip) {
  66. return;
  67. }
  68. switch (this.pages[index].type) {
  69. case PVPPageType.intoBattle:
  70. this.intoBattle(this.pages[index]);
  71. break;
  72. case PVPPageType.attack:
  73. this.attack(this.pages[index]);
  74. break;
  75. case PVPPageType.die:
  76. this.die(this.pages[index]);
  77. break;
  78. case PVPPageType.end:
  79. this.end(this.pages[index]);
  80. break;
  81. }
  82. }
  83. /**加入战斗 */
  84. private intoBattle(page: PVPPage): void {
  85. let uiNode = instantiate(this.pvpHeroUiPrefab);
  86. uiNode.setPosition(10000, 10000, 0);
  87. uiNode.setParent(this.battleLayer);
  88. this['g' + page.formID + 'Node'] = instantiate(this.pvpHeroPrefab);
  89. this['g' + page.formID + 'Node'].setPosition(this.posArray[page.formID == 1 ? 0 : 2].position);
  90. this['g' + page.formID + 'Node'].getComponent(PVPHero).setUI(uiNode.getComponent(PVPHeroUI));
  91. this['g' + page.formID + 'Node'].getComponent(PVPHero).setData(this['g' + page.formID][0], page.formID == 1 ? -1 : 1, this.posArray[page.formID == 1 ? 1 : 3].position, this.actionComplete.bind(this));
  92. this['g' + page.formID + 'Node'].setParent(this.battleLayer);
  93. }
  94. /**攻击 */
  95. private attack(page: PVPPage): void {
  96. this['g' + page.formID + 'Node'].getComponent(PVPHero).attack(this['g' + (page.formID == 1 ? 2 : 1) + 'Node'], this.attackComplete.bind(this));
  97. }
  98. /**死亡 */
  99. private die(page: PVPPage): void {
  100. let node = this['g' + this.pages[this.index].formID + 'Node'] as Node;
  101. (this['g' + this.pages[this.index].formID] as Array<PVPCardData>).shift();
  102. if (this.pages[this.index].formID == 1) {
  103. this.bottomCards.setData(this.g1);
  104. } else {
  105. this.topCards.setData(this.g2);
  106. }
  107. node.getComponent(PVPHero).die(this.actionComplete.bind(this));
  108. }
  109. /**战斗结束 */
  110. private async end(page: PVPPage) {
  111. let result = await this.http.send("/api/pvp/EndPVP", { isWin: this.isWin, key: this.key })
  112. if (result && result.code == HttpResponseCode.Success && result.data) {
  113. this.pvpResult.node.active = true;
  114. this.pvpResult.setData(result.data);
  115. }
  116. }
  117. /**当攻击结束时 */
  118. private attackComplete(): void {
  119. let node = this['g' + (this.pages[this.index].formID == 1 ? 2 : 1) + 'Node'] as Node;
  120. node.getComponent(PVPHero).onHit(this.pages[this.index].hit, this.actionComplete.bind(this));
  121. }
  122. /**战报动作完成 */
  123. private actionComplete(): void {
  124. this.index++;
  125. if (this.index < this.pages.length) {
  126. this.doBattle(this.index);
  127. }
  128. }
  129. /**跳过 */
  130. private skip(): void {
  131. this.doBattle(this.pages.length - 1);
  132. this.isSkip = true;
  133. }
  134. private close(): void {
  135. this.node.getComponent(Window).close();
  136. }
  137. }
  138. export class PVPCardData extends PVPHeroData {
  139. public url: string;
  140. constructor(id: number, lv: number, star: number, color: number, camp: number, url?: string) {
  141. super(id, lv, star, color, camp);
  142. this.url = url;
  143. }
  144. }