LocalData.ts 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. //本地数据管理
  2. import EventName from "./EventName/EventName";
  3. import { Global } from "./Global";
  4. export default class LocalData {
  5. private static _instance: LocalData = null;
  6. public static getInstance() {
  7. if (!this._instance) {
  8. this._instance = new LocalData();
  9. this._instance.init();
  10. }
  11. return this._instance;
  12. }
  13. private readonly $localkey: string = "柑橘味";
  14. //玩家金币
  15. private $goldnum: number = Global.defaultGold;
  16. //玩家最高纪录
  17. private $MaxRecord: number = Global.defaultMaxRecord;
  18. //拥有的抛物线皮肤
  19. private $HasParabolaSkin: number[] = Global.defaultParabolaSkin;
  20. ///拥有的人物皮肤
  21. private $HasCharacterSkin: number[] = Global.defaultCharacterSkin;
  22. ///当前使用的人物皮肤
  23. private $useParabolaSkin: number = Global.useParabolaSkin;
  24. ///当前使用的人物皮肤
  25. private $useCharacterSkin: number = Global.useCharacterSkin;
  26. private $lastLoginTime: number = 0;
  27. public init() {
  28. this.load();
  29. let now = Date.now();
  30. this.$lastLoginTime = now;
  31. this.save();
  32. this.setCurrentParabolaSkin(1001)
  33. }
  34. public setGold(num: number, Action: string) {
  35. this.$goldnum = this.Action(this.$goldnum, num, Action)
  36. this.save();
  37. cc.systemEvent.emit(EventName.changeGold);
  38. }
  39. public getGold() {
  40. return this.$goldnum;
  41. }
  42. public setMaxRecord(num: number, Action: string) {
  43. this.$MaxRecord = this.Action(this.$MaxRecord, num, Action)
  44. this.save();
  45. cc.systemEvent.emit(EventName.changeMaxRecord);
  46. }
  47. public getMaxRecord() {
  48. return this.$MaxRecord;
  49. }
  50. public setHasCharacterSkin(num: number) {
  51. this.$HasCharacterSkin.push(num)
  52. this.save();
  53. }
  54. public getHasCharacterSkin() {
  55. return this.$HasCharacterSkin;
  56. }
  57. public setHasParabolaSkin(num: number) {
  58. this.$HasParabolaSkin.push(num)
  59. this.save();
  60. }
  61. public getHasParabolaSkin() {
  62. return this.$HasParabolaSkin;
  63. }
  64. public setCurrentCharacterSkin(num: number) {
  65. this.$useCharacterSkin = num
  66. this.save();
  67. }
  68. public getCurrentCharacterSkin() {
  69. return this.$useCharacterSkin;
  70. }
  71. public setCurrentParabolaSkin(num: number) {
  72. this.$useParabolaSkin = num
  73. this.save();
  74. }
  75. public getCurrentParabolaSkin() {
  76. return this.$useParabolaSkin;
  77. }
  78. /////////////////////////////////////////////设置
  79. private $Music: boolean = true;
  80. private $Effects: boolean = true;
  81. public setMusic(Temp: boolean) {
  82. this.$Music = Temp
  83. this.save();
  84. }
  85. public getMusic() {
  86. return this.$Music;
  87. }
  88. public setEffects(Temp: boolean) {
  89. this.$Effects = Temp
  90. this.save();
  91. }
  92. public getEffects() {
  93. return this.$Effects;
  94. }
  95. //保存玩家本地数据
  96. public save() {
  97. let data: any = {};
  98. for (const key in this) {
  99. if (Object.prototype.hasOwnProperty.call(this, key)) {
  100. if (key.charAt(0) == '$') {
  101. data[key] = this[key];
  102. }
  103. }
  104. }
  105. cc.sys.localStorage.setItem(this.$localkey, JSON.stringify(data));
  106. }
  107. //加载玩家本地数据
  108. private load() {
  109. let data = cc.sys.localStorage.getItem(this.$localkey);
  110. if (data) {
  111. data = JSON.parse(data);
  112. for (const key in data) {
  113. this[key] = data[key];
  114. }
  115. }
  116. }
  117. private Action(data: number, num: number, str: string) {
  118. switch (str) {
  119. case '+':
  120. return data += num
  121. case '-':
  122. return data -= num
  123. case '=':
  124. return data = num
  125. default:
  126. cc.error('出错了')
  127. return 0
  128. }
  129. }
  130. }