LevelModel.ts 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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 currentOriginCup: number = 0;
  24. /** 剩余杯子数量*/
  25. public remainCupCount: number = 0;
  26. /** 保存可随机的关卡*/
  27. public randomLevelList: number[] = [];
  28. /** 输赢*/
  29. public isWin: boolean = false;
  30. /** 是否结束*/
  31. public isEnd: boolean = false;
  32. /** 当前游戏状态*/
  33. public curGameState: TYPE_GAME_STATE = TYPE_GAME_STATE.GAME_STATE_INIT;
  34. constructor() {
  35. this.levelConfig = new Tablelevels_config();
  36. this.getRandomLevelList();
  37. const isDebug = GlobalConfig.isDebug;
  38. if (isDebug) {
  39. this.level = GlobalConfig.initilizeLevel;
  40. } else {
  41. const level = sys.localStorage.getItem('level');
  42. if (!level) {
  43. this.level = 1;
  44. } else {
  45. if (level > GlobalConfig.levelTotal) {
  46. const randomLevel = this.randomLevelList[Math.floor(Math.random() * this.randomLevelList.length - 1)];
  47. this.level = randomLevel;
  48. } else {
  49. this.level = parseInt(level);
  50. }
  51. }
  52. };
  53. this.levelConfig.init(this.level);
  54. this.initLevelColors();
  55. }
  56. public initLevelColors() {
  57. const count = this.levelConfig.color; //获取关卡颜色数
  58. this.levelColors = [];
  59. const allColors = Object.values(WaterColors).filter(v => !isNaN(Number(v)) && v != WaterColors.Black) as WaterColors[];
  60. // Fisher-Yates 洗牌算法
  61. for (let i = allColors.length - 2; i > 0; i--) {
  62. const j = Math.floor(Math.random() * (i + 1));
  63. [allColors[i], allColors[j]] = [allColors[j], allColors[i]];
  64. }
  65. this.levelColors.push(...allColors.slice(0, count))
  66. }
  67. /** 可随机的关卡合集*/
  68. getRandomLevelList() {
  69. const table = JsonUtil.get(Tablelevels_config.TableName);
  70. if (!table) {
  71. console.warn('Get level table is fail!');
  72. }
  73. this.randomLevelList = Object.values(table).filter(item => item['random'] == 1)
  74. .map(item => item['level']);
  75. // console.log('随机关卡列表:', this.randomLevelList);
  76. }
  77. /** 清除关卡数据*/
  78. clearLevel() {
  79. this.levelColors = [];
  80. this.createQuestionCount = 0;
  81. this.remainCupCount = 0;
  82. this.currentOriginCup = 0;
  83. this.isWin = false;
  84. this.isEnd = false;
  85. }
  86. }