LevelModel.ts 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. import { JsonUtil } from "db://assets/core_tgx/base/utils/JsonUtil";
  2. import { Tablelevels_config } from "../../../module_basic/table/Tablelevels_config";
  3. import { GlobalConfig } from "../../../start/Config/GlobalConfig";
  4. import { sys } from "cc";
  5. import { AliensGlobalInstance } from "../AliensGlobalInstance";
  6. import { GameUtil } from "../GameUtil";
  7. /**关卡数据模型
  8. */
  9. export class LevelModel {
  10. public levelConfig: Tablelevels_config;
  11. /**关卡时长*/
  12. public levelTime: number = 0;
  13. /**关卡用时*/
  14. public levelTimeUsed: number = 0;
  15. /**关卡奖励*/
  16. public levelReward: number = 0;
  17. /** 关卡怪物总数*/
  18. public levelMonsterCount: number = 0;
  19. /** 射击次数*/
  20. public shootCount: number = 0;
  21. /** 击中次数*/
  22. public hitCount: number = 0;
  23. /** 爆头次数*/
  24. public headshotCount: number = 0;
  25. /** 当前关卡等级*/
  26. public level: number = 1;
  27. /** 保存可随机的关卡*/
  28. public randomLevelList: number[] = [];
  29. /** 输赢*/
  30. public isWin: boolean = false;
  31. /** 是否结束*/
  32. public isEnd: boolean = false;
  33. constructor() {
  34. this.levelConfig = new Tablelevels_config();
  35. this.getRandomLevelList();
  36. const isDebug = GlobalConfig.isDebug;
  37. if (isDebug) {
  38. this.level = GlobalConfig.initilizeLevel;
  39. } else {
  40. const level = sys.localStorage.getItem('alient_level');
  41. if (!level) {
  42. this.level = 1;
  43. } else {
  44. if (level > GlobalConfig.levelTotal) {
  45. let randomLevel = this.randomLevelList[Math.floor(Math.random() * this.randomLevelList.length - 1)];
  46. randomLevel = GameUtil.getLastNumber(randomLevel.toString())!;
  47. // console.log('超出最大关卡,随机关卡:', randomLevel);
  48. this.level = randomLevel;
  49. } else {
  50. this.level = parseInt(level);
  51. }
  52. }
  53. };
  54. this.setLevelConfig(this.level);
  55. }
  56. setLevelConfig(level: number) {
  57. this.levelConfig.init(level);
  58. this.levelTime = this.levelConfig.eliminate;
  59. this.levelReward = this.levelConfig.target;
  60. }
  61. /** 可随机的关卡合集*/
  62. getRandomLevelList() {
  63. const table = JsonUtil.get(Tablelevels_config.TableName);
  64. if (!table) {
  65. console.warn('Get level table is fail!');
  66. }
  67. this.randomLevelList = Object.values(table).filter(item => item['random'] == 1)
  68. .map(item => item['level']);
  69. console.log('随机关卡列表:', this.randomLevelList);
  70. }
  71. /** 清除关卡数据*/
  72. clearLevel() {
  73. this.isWin = false;
  74. this.isEnd = false;
  75. this.shootCount = 0;
  76. this.hitCount = 0;
  77. this.headshotCount = 0;
  78. this.levelTimeUsed = 0;
  79. this.levelMonsterCount = 0;
  80. }
  81. }