GameOver.ts 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. import { _decorator, director, EAxisDirection, Label, log, PhysicsSystem, } from 'cc';
  2. import { ModulerBase } from '../../GameFrameWork/ModulerBase';
  3. import { resMgr } from '../../../Frames/ResourcesMgr';
  4. import { GameInfo } from '../../../GameInfo';
  5. import { localDt } from '../../../Frames/LocalDt';
  6. import { GameMgr } from '../../GameFrameWork/GameMgr';
  7. import { EnemyMgr } from './EnemyMgr';
  8. import { MyTower } from './MyTower';
  9. import { Bottom } from './Bottom';
  10. import { EnemyTower } from './EnemyTower';
  11. const { ccclass, property } = _decorator;
  12. @ccclass('GameOver')
  13. export class GameOver extends ModulerBase {
  14. //击杀奖励
  15. KillRewardConfig = [
  16. { min: 0, max: 5, reward: 100 },
  17. { min: 6, max: 10, reward: 200 },
  18. { min: 11, max: 15, reward: 300 },
  19. { min: 16, max: 20, reward: 400 },
  20. { min: 21, max: 25, reward: 500 },
  21. { min: 26, max: 30, reward: 600 },
  22. { min: 31, max: Infinity, reward: 700 },
  23. ]
  24. //生命奖励
  25. LifeRewardConfig = [
  26. { min: 0, max: 20, reward: 100 },
  27. { min: 21, max: 40, reward: 200 },
  28. { min: 41, max: 60, reward: 300 },
  29. { min: 61, max: 80, reward: 400 },
  30. { min: 81, max: 100, reward: 500 },
  31. ]
  32. //通关奖励
  33. newresultRewardConfig = [
  34. { min: 1, max: 5, reward: 100 },
  35. { min: 6, max: 10, reward: 150 },
  36. { min: 11, max: 15, reward: 200 },
  37. { min: 16, max: 20, reward: 250 },
  38. { min: 21, max: 25, reward: 300 },
  39. { min: 26, max: Infinity, reward: 350 },
  40. ]
  41. private xp: number = null;
  42. private totalGold: number = null;
  43. private totalDia: number = null;
  44. protected onEnable(): void {
  45. this.config();
  46. }
  47. protected onStart(): void {
  48. this.onBtnClick("_btnLeft", this._onBtnBack, this);
  49. this.onBtnClick("_btnRight", this._onBtnRight, this);
  50. director.preloadScene("StartScene")
  51. }
  52. config() {
  53. GameInfo.Instance.setIsGameOver(true);
  54. this.getSprite("_gameOverUITitle").spriteFrame =
  55. GameInfo.Instance.getOverWin() ?
  56. resMgr.getSpriteFrame("GameWin") :
  57. resMgr.getSpriteFrame("GameOver");
  58. this.getNode("_btnRight").getComponentInChildren(Label).string =
  59. GameInfo.Instance.getOverWin() ? "下一关" : "重新开始";
  60. this.getLabel("_lifePercent").string = `${GameInfo.Instance.getLifePecent()}%`;
  61. this.getLabel("_killCount").string = `${GameInfo.Instance.getKillCount()}`;
  62. const kill: number = this.calculateReward(GameInfo.Instance.getKillCount(), this.KillRewardConfig);
  63. const life: number = this.calculateReward(GameInfo.Instance.getLifePecent(), this.LifeRewardConfig);
  64. const stageClear: number = this.calculateReward(Number(GameInfo.Instance.getCurlv()), this.newresultRewardConfig);
  65. this.xp = Number(GameInfo.Instance.getCurlv()) * 30;
  66. this.totalGold = kill + life + stageClear;
  67. const lifeDia: number = Math.floor(life * 0.08);
  68. const stageClearDia: number = Math.floor(stageClear * 0.07);
  69. this.totalDia = lifeDia + stageClearDia;
  70. this.getLabel("_kill").string = `${kill}`;
  71. this.getLabel("_life").string = `${life}`;
  72. this.getLabel("_stageClear").string = `${stageClear}`;
  73. this.getLabel("_total").string = `${this.totalGold}`;
  74. this.getLabel("_xp").string = `${this.xp}`;
  75. this.getLabel("_lifeDia").string = `${lifeDia}`;
  76. this.getLabel("_stageClearDia").string = `${stageClearDia}`;
  77. this.getLabel("_totalDia").string = `${this.totalDia}`;
  78. }
  79. private _onBtnBack() {
  80. //director.purgeDirector();
  81. this.addReward();
  82. this.clearDt();
  83. this.hide(false);
  84. director.loadScene("StartScene", () => {
  85. director.resume();
  86. GameInfo.Instance.setIsGameOver(false);
  87. });
  88. }
  89. private _onBtnRight() {
  90. director.resume();
  91. GameInfo.Instance.setIsGameOver(false);
  92. if (GameInfo.Instance.getOverWin()) {
  93. //初始化场景
  94. this._initGameScene();
  95. } else {
  96. //初始化场景
  97. this._initGameScene();
  98. }
  99. }
  100. //计算奖励
  101. private calculateReward(lifeReward: number, rewardConfig: any) {
  102. const matchRule = rewardConfig.find(rule =>
  103. lifeReward >= rule.min && lifeReward <= rule.max
  104. )
  105. return matchRule ? matchRule.reward : 0;
  106. }
  107. //清除数据
  108. private clearDt() {
  109. GameInfo.Instance.setKillCount(0);
  110. GameInfo.Instance.setLifePecent(0);
  111. GameInfo.Instance.setOverWin(false);
  112. }
  113. private addReward() {
  114. const map: Map<string, number> = new Map();
  115. map.set("GameOverRewardGold", (this.totalGold + Number(localDt.getData("Gold"))));
  116. map.set("GameOverRewardDia", this.totalDia + GameInfo.Instance.getDiamond());
  117. map.set("Xp", this.xp + GameInfo.Instance.getCurGradeExp());
  118. if (GameInfo.Instance.getOverWin()) {
  119. GameInfo.Instance.setCurLv(Number(GameInfo.Instance.getCurlv()) + 1);
  120. map.set("CurLv", Number(GameInfo.Instance.getCurlv()));
  121. }
  122. GameInfo.Instance.setGameOverReward(map);
  123. //GameInfo.Instance.setGold(this.totalGold + GameInfo.Instance.getGold());
  124. localDt.saveData("Gold", this.totalGold + Number(localDt.getData("Gold")));
  125. GameInfo.Instance.setDiamond(this.totalDia + GameInfo.Instance.getDiamond());
  126. localDt.saveData("Diamond", GameInfo.Instance.getDiamond())
  127. }
  128. //初始化场景 重置数据
  129. private _initGameScene() {
  130. this.addReward();
  131. this.clearDt();
  132. GameMgr.Instance.getModuler(EnemyMgr).node.removeAllChildren();
  133. this.node.parent.getChildByPath("Road/Roles").removeAllChildren();
  134. this.node.parent.getChildByPath("BulletLayer").removeAllChildren();
  135. GameMgr.Instance.getModuler(EnemyMgr).init();
  136. this.node.parent.getComponentInChildren(EnemyTower).initData();
  137. this.node.parent.getComponentInChildren(MyTower).initData();
  138. GameMgr.Instance.getModuler(Bottom).initData();
  139. //隐藏
  140. this.hide(false);
  141. }
  142. }