GameOver.ts 4.8 KB

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