GameOver.ts 5.0 KB

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