Mission.ts 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. import { _decorator, Component, Node, sp, color, director } from 'cc';
  2. import { DataSystem } from '../core/data/DataSystem';
  3. import { Http, HttpResponseCode } from '../core/net/Http';
  4. import { WindowSystem } from '../core/ui/window/WindowSystem';
  5. import { ConfigData } from '../data/ConfigData';
  6. import { DeviceData } from '../data/jsb/DeviceData';
  7. import { UserData } from '../data/UserData';
  8. import { UserHeroData } from '../hero/UserHeroData';
  9. import { GM } from '../launch/GM';
  10. import { ReportThinking } from '../ReportThinking';
  11. import { BattleData, BattleState } from './BattleData';
  12. import { MissionData } from './MissionData';
  13. import { BattleResult } from './ui/BattleResult';
  14. import { BattleUIData } from './ui/BattleUIData';
  15. const { ccclass, property } = _decorator;
  16. @ccclass('Mission')
  17. export class Mission extends Component {
  18. @property({ type: Http, tooltip: "Http组件" }) http: Http;
  19. @property({ type: Node, tooltip: "完成窗口" }) openComplete: Node;
  20. @property({ type: Node, tooltip: "失败窗口" }) openFail: Node;
  21. @property({ type: Node, tooltip: "挑战boss按钮" }) fightBossButton: Node;
  22. private data: BattleData;
  23. /**boss关卡是否失败过 */
  24. private isfailed = false;
  25. /**过关奖励 */
  26. private resultData: any;
  27. /**下一关数值 */
  28. private nextMission = 0;
  29. async start() {
  30. DataSystem.createData(MissionData);
  31. this.data = DataSystem.getData(BattleData);
  32. }
  33. update() {
  34. DataSystem.watch(BattleUIData, "_bannerRunOver") && this.missionChange();
  35. DataSystem.watch(MissionData, "_missionOver") && this.missionComplete();
  36. DataSystem.watch(MissionData, "_isResultWindowClose") && this.resultWindowClose();
  37. }
  38. private missionChange(): void {
  39. if (DataSystem.getData(BattleUIData)._bannerRunOver) {
  40. if (this.isfailed && this.fightBossButton.getComponent(sp.Skeleton).color.a == 0) {
  41. this.fightBossButton.getComponent(sp.Skeleton).color = color(255, 255, 255, 255);
  42. }
  43. if (!this.isfailed && this.fightBossButton.getComponent(sp.Skeleton).color.a == 255) {
  44. this.fightBossButton.getComponent(sp.Skeleton).color = color(255, 255, 255, 0);
  45. }
  46. }
  47. }
  48. /**关卡完成 */
  49. private async missionComplete() {
  50. if (this.data.battleState == BattleState.inBattle && DataSystem.getData(MissionData)._missionOver) {
  51. this.fightBossButton.getComponent(sp.Skeleton).color = color(255, 255, 255, 0);
  52. DataSystem.getData(BattleUIData)._bannerRunOver = false;
  53. DataSystem.getData(MissionData)._missionOver = false;
  54. this.data.battleState = BattleState.outBattle;
  55. let result = await this.http.send("/api/battle/endBattle", { isWin: this.data._moveEndCount == 0, monsters: this.data._monsterKilled, addLevel: !this.isfailed });
  56. if (result && result.code == HttpResponseCode.Success) {
  57. this.resultData = result.data;
  58. this.nextMission = result.data.level;
  59. this.addResult();
  60. if (DataSystem.getData(ConfigData)["mission"][this.data.mission]["type"] == "2") {
  61. this.isfailed = !result.data.isWin;
  62. if (result.data.isWin) {
  63. DataSystem.getData(UserData).level = this.nextMission;
  64. DataSystem.getData(UserData).lv = this.nextMission - 1;
  65. if (DataSystem.getData(MissionData).isInPvp) {
  66. this.next();
  67. } else {
  68. // GM.addBattleLog(`打开胜利窗口`);
  69. this.openComplete.active = true;
  70. this.openComplete.getComponent(BattleResult).setData(result.data);
  71. }
  72. } else {
  73. DataSystem.getData(UserData).lv = DataSystem.getData(UserData).level = this.nextMission;
  74. if (DataSystem.getData(MissionData).isInPvp) {
  75. this.next();
  76. } else {
  77. // GM.addBattleLog(`打开失败窗口`);
  78. this.openFail.active = true;
  79. this.openFail.getComponent(BattleResult).setData(result.data);
  80. }
  81. }
  82. } else {
  83. if (result.data.isWin) {
  84. DataSystem.getData(UserData).level = this.nextMission;
  85. DataSystem.getData(UserData).lv = this.nextMission - 1;
  86. } else {
  87. DataSystem.getData(UserData).lv = DataSystem.getData(UserData).level = this.nextMission;
  88. }
  89. this.next();
  90. }
  91. }
  92. else {
  93. localStorage.removeItem("account");
  94. DataSystem.getData(DeviceData).needlogin = true;
  95. director.loadScene("launch");
  96. WindowSystem.showTips("网络异常,请重新登录");
  97. }
  98. this.data._moveEndCount = 0;
  99. this.data._monsterKilled = [];
  100. this.data._monsterKilledNode = [];
  101. // GM.addBattleLog(`===============================关卡:${this.data.mission},关卡完成===============================`);
  102. }
  103. }
  104. private resultWindowClose(): void {
  105. if (DataSystem.getData(MissionData)._isResultWindowClose) {
  106. DataSystem.getData(MissionData)._isResultWindowClose = false;
  107. this.next();
  108. }
  109. }
  110. /**添加奖励 */
  111. private addResult(): void {
  112. if (!this.resultData) {
  113. // GM.addBattleLog("关卡出错,关卡暂停,resultData:" + JSON.stringify(this.resultData));
  114. return;
  115. }
  116. let userData = DataSystem.getData(UserData);
  117. this.resultData.prestige && ReportThinking.currency_increase('prestige', userData.prestige, this.resultData.prestige, this.resultData.prestige + userData.prestige, 'endBattle');
  118. this.resultData.money && ReportThinking.currency_increase('money', userData.money, this.resultData.money, this.resultData.money + userData.money, 'endBattle');
  119. this.resultData.diamond && ReportThinking.currency_increase('diamond', userData.diamond, this.resultData.diamond, this.resultData.diamond + userData.diamond, 'endBattle');
  120. this.resultData.bonus && ReportThinking.currency_increase('bonus', userData.bonus, this.resultData.bonus, this.resultData.bonus + userData.bonus, 'endBattle');
  121. userData.prestige += this.resultData.prestige;
  122. userData.money += this.resultData.autoMoney;
  123. userData.money += this.resultData.money;
  124. userData.diamond += this.resultData.diamond;
  125. userData.bonus += this.resultData.bonus;
  126. for (let i = 0; i < this.resultData.heros.length; i++) {
  127. DataSystem.getData(UserHeroData).addHero(this.resultData.heros[i]);
  128. }
  129. for (let k in this.resultData.chips) {
  130. DataSystem.getData(UserHeroData).addChip(parseInt(k), this.resultData.chips[k]);
  131. }
  132. }
  133. /**开始下一关 */
  134. private async next() {
  135. let battleResult = await this.http.send("/api/battle/startBattle", { level: this.nextMission });
  136. battleResult && battleResult.code == HttpResponseCode.Success && (this.data.mission = this.nextMission);
  137. if (!battleResult) {
  138. localStorage.removeItem("account");
  139. DataSystem.getData(DeviceData).needlogin = true;
  140. director.loadScene("launch");
  141. WindowSystem.showTips("网络异常,请重新登录");
  142. }
  143. // GM.addBattleLog(`===============================开始关卡:${this.nextMission}===============================`);
  144. }
  145. public async fightBoss() {
  146. if (this.fightBossButton.getComponent(sp.Skeleton).color.a == 255) {
  147. this.fightBossButton.getComponent(sp.Skeleton).color = color(255, 255, 255, 0);
  148. let battleResult = await this.http.send("/api/battle/startBattle", { level: this.nextMission + 1 })
  149. battleResult && battleResult.code == HttpResponseCode.Success && (this.data.mission = this.nextMission + 1);
  150. this.isfailed = false;
  151. if (!battleResult) {
  152. localStorage.removeItem("account");
  153. DataSystem.getData(DeviceData).needlogin = true;
  154. director.loadScene("launch");
  155. WindowSystem.showTips("网络异常,请重新登录");
  156. }
  157. }
  158. }
  159. }