levelsData.ts 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. import { _decorator, Prefab, sys } from "cc";
  2. import { constants } from "./constants";
  3. import { utils } from "./utils";
  4. import { localConfig } from "../manager/localConfig";
  5. import Data from "../scripts/core/manager/Data";
  6. import Utils from "../scripts/core/utils/Utils";
  7. const { ccclass, property } = _decorator;
  8. @ccclass("levelsData")
  9. export class levelsData {
  10. public data: any = null;
  11. public static instance: levelsData = null;
  12. //关卡数据
  13. public _levelTable: Array<any> = null;
  14. public get levelTable(){
  15. if(!this._levelTable){
  16. this._levelTable = localConfig.instance.getTableArr('levels');
  17. }
  18. return this._levelTable;
  19. }
  20. public _modelsTable: Array<any> = null;
  21. public get modelsTable(){
  22. if(!this._modelsTable){
  23. this._modelsTable = localConfig.instance.getTableArr('model_names');
  24. }
  25. return this._modelsTable;
  26. }
  27. /**单例*/
  28. public static ins() {
  29. if (!this.instance) {
  30. this.instance = new levelsData();
  31. }
  32. return this.instance;
  33. }
  34. /**
  35. * 读取数据缓存
  36. */
  37. public loadFromCache(){
  38. let l = sys.localStorage.getItem(constants.localCache.settingData);
  39. if(l){
  40. this.data = JSON.parse(utils.decrypt(l));
  41. }else{
  42. //没有数据保存一份默认的初始数据
  43. this.data = {};
  44. this.saveToCache();
  45. }
  46. }
  47. /**
  48. * 得到解锁的模型名字数组
  49. */
  50. public getDeblockDatas():Array<any>{
  51. return this.modelsTable.map(item => item.name);
  52. }
  53. /**
  54. * 得到标准模型名字数组
  55. */
  56. public getNormModelDatas():Array<any>{
  57. return this.modelsTable.map(item => item.name);
  58. }
  59. /**
  60. * 得到当前关卡下的数据
  61. */
  62. public getCurLevelInfo(){
  63. let datas:Array<any> = this.levelTable;
  64. let lv = Data.user.lv - 1;
  65. if(lv > datas.length){
  66. return datas[datas.length -1];
  67. }else{
  68. return datas[lv];
  69. }
  70. }
  71. public async LoadItemPrefabByName(name: string) {
  72. if(utils.isNull(name))return null;
  73. return Utils.loadRes(name, "item_bundle", Prefab);
  74. }
  75. /**
  76. * 保存到缓存中
  77. */
  78. public saveToCache(){
  79. const data = JSON.stringify(this.data);
  80. //序列化JSON字符串过后加密存储
  81. sys.localStorage.setItem(constants.localCache.levelsData, utils.encrypt(data));
  82. }
  83. /**
  84. * 删除缓存
  85. */
  86. public remove(){
  87. sys.localStorage.removeItem(constants.localCache.levelsData);
  88. this.loadFromCache();
  89. }
  90. }
  91. export default levelsData.ins();