LevelModel.ts 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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 { WaterColors } from "../TakeGobletGlobalInstance";
  6. export enum TYPE_GAME_STATE {
  7. GAME_STATE_INIT = 0, //准备阶段
  8. GAME_STATE_START = 1, //开始
  9. GAME_STATE_END = 2, //结束(倒计时结束)
  10. GAME_STATE_RESULT = 3, //结算
  11. GAME_STATE_PAUSE = 4, //暂停
  12. }
  13. /**关卡数据模型
  14. */
  15. export class LevelModel {
  16. public levelConfig: Tablelevels_config;
  17. public levelColors: WaterColors[] = [];
  18. /** 当前关卡等级*/
  19. public level: number = 1;
  20. /** 创建问号水的水量*/
  21. public createQuestionCount: number = 0;
  22. /** 保存可随机的关卡*/
  23. public randomLevelList: number[] = [];
  24. /** 输赢*/
  25. public isWin: boolean = false;
  26. /** 是否结束*/
  27. public isEnd: boolean = false;
  28. /** 当前游戏状态*/
  29. public curGameState: TYPE_GAME_STATE = TYPE_GAME_STATE.GAME_STATE_INIT;
  30. constructor() {
  31. this.levelConfig = new Tablelevels_config();
  32. this.getRandomLevelList();
  33. const isDebug = GlobalConfig.isDebug;
  34. if (isDebug) {
  35. this.level = GlobalConfig.initilizeLevel;
  36. } else {
  37. const level = sys.localStorage.getItem('level');
  38. if (!level) {
  39. this.level = 1;
  40. } else {
  41. if (level > GlobalConfig.levelTotal) {
  42. const randomLevel = this.randomLevelList[Math.floor(Math.random() * this.randomLevelList.length - 1)];
  43. this.level = randomLevel;
  44. } else {
  45. this.level = parseInt(level);
  46. }
  47. }
  48. };
  49. this.levelConfig.init(this.level);
  50. this.initLevelColors();
  51. }
  52. public initLevelColors() {
  53. const count = this.levelConfig.color; //获取关卡颜色数
  54. this.levelColors = [];
  55. const allColors = Object.values(WaterColors).filter(v => !isNaN(Number(v)) && v != WaterColors.Black) as WaterColors[];
  56. // Fisher-Yates 洗牌算法
  57. for (let i = allColors.length - 2; i > 0; i--) {
  58. const j = Math.floor(Math.random() * (i + 1));
  59. [allColors[i], allColors[j]] = [allColors[j], allColors[i]];
  60. }
  61. this.levelColors.push(...allColors.slice(0, count))
  62. }
  63. /** 可随机的关卡合集*/
  64. getRandomLevelList() {
  65. const table = JsonUtil.get(Tablelevels_config.TableName);
  66. if (!table) {
  67. console.warn('Get level table is fail!');
  68. }
  69. this.randomLevelList = Object.values(table).filter(item => item['random'] == 1)
  70. .map(item => item['level']);
  71. // console.log('随机关卡列表:', this.randomLevelList);
  72. }
  73. /** 清除关卡数据*/
  74. clearLevel() {
  75. this.levelColors = [];
  76. this.createQuestionCount = 0;
  77. this.isWin = false;
  78. this.isEnd = false;
  79. }
  80. }