LevelModel.ts 2.7 KB

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