GameData.ts 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. import { sys } from "cc";
  2. import PlatformMng, { Channel } from "./AdSdk/PlatformMng";
  3. import AudioMng from "./AudioMng";
  4. import GDSave from "./GDSave";
  5. export default class GameData {
  6. static isInited: boolean = false;
  7. static isIdiomInited: boolean = false;
  8. public static game: GDSave = new GDSave();
  9. public static GameFPS: number = 1 / 60;
  10. static FileName: string = "ChessGame_Save";
  11. public static SaveGame(): void {
  12. var jsonString: string = JSON.stringify(GameData.game);
  13. sys.localStorage.setItem(GameData.FileName, jsonString);
  14. }
  15. public static LoadGame(): void {
  16. var jsonString = sys.localStorage.getItem(GameData.FileName);
  17. if (jsonString) {
  18. let saveobj = JSON.parse(jsonString);
  19. this.ContinueGame(saveobj);
  20. } else {
  21. this.NewGame();
  22. this.SaveGame();
  23. }
  24. this.Init()
  25. }
  26. public static Init(){
  27. if(sys.platform==sys.Platform.WECHAT_GAME)
  28. PlatformMng.channo=Channel.wx
  29. PlatformMng.platform.init();
  30. }
  31. //新的游戏
  32. public static NewGame(): void {
  33. GameData.game = new GDSave();
  34. AudioMng.Instance.InitSaveData();
  35. console.log('新的游戏')
  36. }
  37. //继续游戏
  38. public static ContinueGame(saveobj:any){
  39. console.log('继续游戏')
  40. GameData.game = new GDSave();
  41. GameData.game.loadfromSave(saveobj);
  42. console.log(GameData.game);
  43. }
  44. /**
  45. * 获取自定义数据
  46. */
  47. public static GetCustomData(dataname: string): number {
  48. var val = 0;
  49. var customData = GameData.game.customData;
  50. if (customData[dataname]) val = customData[dataname];
  51. return val;
  52. }
  53. /**
  54. * 设置自定义数据
  55. */
  56. public static SetCustomData(dataname: string, val: number): void {
  57. GameData.game.customData[dataname] = val;
  58. GameData.SaveGame();
  59. }
  60. }