DataMgr.ts 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. class ILocalData {
  2. skins: number[];
  3. unlockSkinList: number[]
  4. }
  5. export class DataMgr {
  6. private static _ins: DataMgr;
  7. public static get ins(): DataMgr {
  8. this._ins = this._ins || new DataMgr()
  9. return this._ins;
  10. }
  11. data: ILocalData
  12. constructor() {
  13. this.load()
  14. }
  15. load() {
  16. const local_data_json = localStorage.getItem("local_data_json");
  17. if (!local_data_json) {
  18. this.data = {
  19. skins: [1],
  20. unlockSkinList: [1]
  21. }
  22. } else {
  23. this.data = JSON.parse(local_data_json)
  24. }
  25. }
  26. save() {
  27. const local_data_json = JSON.stringify(this.data)
  28. localStorage.setItem("local_data_json", local_data_json)
  29. }
  30. setSkin(skin: number) {
  31. let idx = this.data.skins.indexOf(skin);
  32. if (idx != -1)
  33. {
  34. this.data.skins.splice(idx, 1);
  35. }else{
  36. this.data.skins.push(skin);
  37. }
  38. if(this.data.skins.length <= 0){
  39. this.data.skins.push(1);
  40. }
  41. this.save()
  42. }
  43. isUnlockSkin(skin: number) {
  44. return this.data.unlockSkinList.includes(skin)
  45. }
  46. unlockSkin(skin: number) {
  47. if (this.data.unlockSkinList.includes(skin)) {
  48. return;
  49. }
  50. this.data.unlockSkinList.push(skin)
  51. this.save()
  52. }
  53. isUseSkin(skin: number):boolean{
  54. return this.data.skins.includes(skin);
  55. }
  56. getRandomId():number{
  57. let index = Math.floor(Math.random()*this.data.skins.length);
  58. return this.data.skins[index];
  59. }
  60. }