LocalData.ts 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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. }
  33. public setGold(num: number, Action: string) {
  34. this.$goldnum = this.Action(this.$goldnum, num, Action)
  35. this.save();
  36. cc.systemEvent.emit(EventName.changeGold);
  37. }
  38. public getGold() {
  39. return this.$goldnum;
  40. }
  41. public setMaxRecord(num: number, Action: string) {
  42. this.$MaxRecord = this.Action(this.$MaxRecord, num, Action)
  43. this.save();
  44. cc.systemEvent.emit(EventName.changeMaxRecord);
  45. }
  46. public getMaxRecord() {
  47. return this.$MaxRecord;
  48. }
  49. public setHasCharacterSkin(num: number) {
  50. this.$HasCharacterSkin.push(num)
  51. this.save();
  52. }
  53. public getHasCharacterSkin() {
  54. return this.$HasCharacterSkin;
  55. }
  56. public setHasParabolaSkin(num: number) {
  57. this.$HasParabolaSkin.push(num)
  58. this.save();
  59. }
  60. public getHasParabolaSkin() {
  61. return this.$HasParabolaSkin;
  62. }
  63. public setCurrentCharacterSkin(num: number) {
  64. this.$useCharacterSkin = num
  65. this.save();
  66. }
  67. public getCurrentCharacterSkin() {
  68. return this.$useCharacterSkin;
  69. }
  70. public setCurrentParabolaSkin(num: number) {
  71. this.$useParabolaSkin = num
  72. this.save();
  73. }
  74. public getCurrentParabolaSkin() {
  75. return this.$useParabolaSkin;
  76. }
  77. //保存玩家本地数据
  78. public save() {
  79. let data: any = {};
  80. for (const key in this) {
  81. if (Object.prototype.hasOwnProperty.call(this, key)) {
  82. if (key.charAt(0) == '$') {
  83. data[key] = this[key];
  84. }
  85. }
  86. }
  87. cc.sys.localStorage.setItem(this.$localkey, JSON.stringify(data));
  88. }
  89. //加载玩家本地数据
  90. private load() {
  91. let data = cc.sys.localStorage.getItem(this.$localkey);
  92. if (data) {
  93. data = JSON.parse(data);
  94. for (const key in data) {
  95. this[key] = data[key];
  96. }
  97. }
  98. }
  99. private Action(data: number, num: number, str: string) {
  100. switch (str) {
  101. case '+':
  102. return data += num
  103. case '-':
  104. return data -= num
  105. case '=':
  106. return data = num
  107. default:
  108. cc.error('出错了')
  109. return 0
  110. }
  111. }
  112. }