GameOver.ts 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. import { _decorator, director, 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. this.getSprite("_gameOverUITitle").spriteFrame =
  54. GameInfo.Instance.getOverWin() ?
  55. resMgr.getSpriteFrame("GameWin") :
  56. resMgr.getSpriteFrame("GameOver");
  57. this.getNode("_btnRight").getComponentInChildren(Label).string =
  58. GameInfo.Instance.getOverWin() ? "下一关" : "重新开始";
  59. this.getLabel("_lifePercent").string = `${GameInfo.Instance.getLifePecent()}%`;
  60. this.getLabel("_killCount").string = `${GameInfo.Instance.getKillCount()}`;
  61. const kill: number = this.calculateReward(GameInfo.Instance.getKillCount(), this.KillRewardConfig);
  62. const life: number = this.calculateReward(GameInfo.Instance.getLifePecent(), this.LifeRewardConfig);
  63. const stageClear: number = this.calculateReward(GameInfo.Instance.getCurlv(), this.newresultRewardConfig);
  64. this.xp = GameInfo.Instance.getCurlv() * 30;
  65. this.totalGold = kill + life + stageClear;
  66. const lifeDia: number = Math.floor(life * 0.08);
  67. const stageClearDia: number = Math.floor(stageClear * 0.07);
  68. this.totalDia = lifeDia + stageClearDia;
  69. this.getLabel("_kill").string = `${kill}`;
  70. this.getLabel("_life").string = `${life}`;
  71. this.getLabel("_stageClear").string = `${stageClear}`;
  72. this.getLabel("_total").string = `${this.totalGold}`;
  73. this.getLabel("_xp").string = `${this.xp}`;
  74. this.getLabel("_lifeDia").string = `${lifeDia}`;
  75. this.getLabel("_stageClearDia").string = `${stageClearDia}`;
  76. this.getLabel("_totalDia").string = `${this.totalDia}`;
  77. }
  78. private _onBtnBack() {
  79. director.resume()
  80. director.purgeDirector();
  81. this.addReward();
  82. this.clearDt();
  83. this.hide(false);
  84. director.loadScene("StartScene");
  85. }
  86. private _onBtnRight() {
  87. director.resume();
  88. if (GameInfo.Instance.getOverWin()) {
  89. //初始化场景
  90. this._initGameScene();
  91. console.log("下一关")
  92. } else {
  93. //director.loadScene("GameScene")
  94. }
  95. }
  96. //计算奖励
  97. private calculateReward(lifeReward: number, rewardConfig: any) {
  98. const matchRule = rewardConfig.find(rule =>
  99. lifeReward >= rule.min && lifeReward <= rule.max
  100. )
  101. return matchRule ? matchRule.reward : 0;
  102. }
  103. //清除数据
  104. private clearDt() {
  105. GameInfo.Instance.setKillCount(0);
  106. GameInfo.Instance.setLifePecent(0);
  107. GameInfo.Instance.setOverWin(false);
  108. GameInfo.Instance.setIsGameOver(false);
  109. }
  110. private addReward() {
  111. const map: Map<string, number> = new Map();
  112. map.set("GameOverRewardGold", this.totalGold + localDt.getGold());
  113. map.set("GameOverRewardDia", this.totalDia + GameInfo.Instance.getDiamond());
  114. map.set("Xp", this.xp + GameInfo.Instance.getCurGradeExp());
  115. if (GameInfo.Instance.getOverWin()) {
  116. GameInfo.Instance.setCurLv(GameInfo.Instance.getCurlv() + 1);
  117. map.set("CurLv", GameInfo.Instance.getCurlv());
  118. }
  119. GameInfo.Instance.setGameOverReward(map);
  120. //GameInfo.Instance.setGold(this.totalGold + GameInfo.Instance.getGold());
  121. localDt.saveGold(this.totalGold + localDt.getGold());
  122. GameInfo.Instance.setDiamond(this.totalDia + GameInfo.Instance.getDiamond());
  123. localDt.saveDiamond(GameInfo.Instance.getDiamond())
  124. }
  125. //初始化场景 重置数据
  126. private _initGameScene() {
  127. this.addReward();
  128. this.clearDt();
  129. GameMgr.Instance.getModuler(EnemyMgr).node.removeAllChildren();
  130. this.node.parent.getChildByPath("Road/Roles").removeAllChildren();
  131. this.node.parent.getChildByPath("BulletLayer").removeAllChildren();
  132. GameMgr.Instance.getModuler(EnemyMgr).init();
  133. this.node.parent.getComponentInChildren(EnemyTower).initData();
  134. this.node.parent.getComponentInChildren(MyTower).initData();
  135. GameMgr.Instance.getModuler(Bottom).initData();
  136. //隐藏
  137. this.hide(false);
  138. }
  139. }