LevelModel.ts 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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 randomLevelList: number[] = [];
  22. /** 输赢*/
  23. public isWin: boolean = false;
  24. /** 是否结束*/
  25. public isEnd: boolean = false;
  26. /** 当前游戏状态*/
  27. public curGameState: TYPE_GAME_STATE = TYPE_GAME_STATE.GAME_STATE_INIT;
  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('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.levelConfig.init(this.level);
  48. this.initLevelColors();
  49. }
  50. public initLevelColors() {
  51. const count = this.levelConfig.color; //获取关卡颜色数
  52. this.levelColors = [];
  53. const allColors = Object.values(WaterColors).filter(v => !isNaN(Number(v))) as WaterColors[];
  54. // Fisher-Yates 洗牌算法
  55. for (let i = allColors.length - 1; i > 0; i--) {
  56. const j = Math.floor(Math.random() * (i + 1));
  57. [allColors[i], allColors[j]] = [allColors[j], allColors[i]];
  58. }
  59. this.levelColors.push(...allColors.slice(0, count))
  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.levelColors = [];
  74. this.isWin = false;
  75. this.isEnd = false;
  76. }
  77. }