LevelModel.ts 2.5 KB

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