UserDataMgr.ts 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. export default class UserDataMgr {
  2. private static instance: UserDataMgr = null;
  3. public static getInstance() {
  4. if (!UserDataMgr.instance) {
  5. UserDataMgr.instance = new UserDataMgr();
  6. UserDataMgr.instance.loadLocalData();
  7. }
  8. return UserDataMgr.instance;
  9. }
  10. /**
  11. * 当前解锁等级
  12. */
  13. levelId: number = 0;
  14. /**
  15. * 游戏局数
  16. */
  17. gameCount: number = 0;
  18. /**
  19. * 最高分数
  20. */
  21. maxScore: number = 0;
  22. /**
  23. * 消耗的卡路里数
  24. */
  25. calorie: number = 0;
  26. // 等级数据
  27. levelScoreData: { [key: number]: number } = {};
  28. isFirst: boolean = true;
  29. public loadLocalData() {
  30. //TODO app接口
  31. let data = Laya.LocalStorage.getJSON("gameData");
  32. this.setData(data);
  33. }
  34. public syncSaveData() {
  35. //TODO app存储接口
  36. Laya.LocalStorage.setJSON("gameData", this);
  37. }
  38. private setData(data: any) {
  39. if (data) {
  40. this.levelId = data.levelId;
  41. this.maxScore = data.maxScore;
  42. this.gameCount = data.gameCount;
  43. this.calorie = data.calorie;
  44. this.levelScoreData = data.levelScoreData || {};
  45. this.isFirst = data.isFirst;
  46. } else {
  47. this.syncSaveData();
  48. }
  49. }
  50. }