GameRoot.ts 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. import { _decorator, Camera, Component, JsonAsset, Material, Node, Sprite } from 'cc';
  2. import { rootMgr } from './RootMgr';
  3. import { AssetType, ResUtil } from '../util/ResUtil';
  4. import { CfgMgr } from '../util/CfgMgr';
  5. import { GameControl } from '../GameContorl/GameControl';
  6. import { FMSType } from '../GameContorl/fms/FMSGameInit';
  7. import { MySave } from '../../scripts/uitl/baseData/saveCompent/MySave';
  8. import { GameCompent } from '../GameContorl/GameComponent';
  9. import { aa } from '../../scripts/aa';
  10. import { SaveComponent } from './GameConfing';
  11. import { UI_Notify } from '../ui/ui_notify/UI_Notify';
  12. import { Player } from '../data/player/Player';
  13. const { ccclass, property } = _decorator;
  14. @ccclass('GameRoot')
  15. export class GameRoot extends Component {
  16. start() {
  17. this.initData()
  18. this.loadData()
  19. aa.uIMgr.showUI(UI_Notify)
  20. ResUtil.loadResToMap(AssetType.Json, (json: JsonAsset[]) => {
  21. let res = {}
  22. json.forEach((mat) => {
  23. res[mat.name] = mat.json
  24. CfgMgr.setCfgData(mat.name, mat.json)
  25. })
  26. return res
  27. }, () => {
  28. rootMgr.game.FMS.Change(FMSType.home)
  29. })
  30. rootMgr.game.evt.on("save", this.doSave, this)
  31. }
  32. doSave(force: boolean) {
  33. // 保存
  34. this.isNeedSave = true
  35. if (force) {
  36. this.saveData()
  37. }
  38. }
  39. protected onDestroy(): void {
  40. rootMgr.game?.clear()
  41. rootMgr.gameCompent?.clear()
  42. rootMgr.rooView = null
  43. rootMgr.game = null
  44. rootMgr.dataControl = null
  45. rootMgr.gameCompent = null
  46. }
  47. rePlay() {
  48. this.scheduleOnce(() => {
  49. rootMgr.game.init()
  50. }, .1)
  51. }
  52. initData() {
  53. rootMgr.rooView = this
  54. rootMgr.game = new GameControl
  55. rootMgr.dataControl = new MySave
  56. rootMgr.gameCompent = new GameCompent
  57. }
  58. loadData() {
  59. let data = aa.data.get(SaveComponent.getGameData())
  60. rootMgr.dataControl.init(data)
  61. rootMgr.dataControl.getCompent(Player).init()
  62. }
  63. async loadInfo(): Promise<boolean> {
  64. return new Promise(
  65. async (resolve) => {
  66. // aa.uIMgr.showUI(UIWaiting_Impl);
  67. // let k = await chsdk.getUserInfo();
  68. // if (k) {
  69. // resolve(true);
  70. // } else {
  71. // resolve(false);
  72. // Notify("需要授权");
  73. // }
  74. // aa.uIMgr.closeUI(UIWaiting_Impl);
  75. }
  76. )
  77. }
  78. isNeedSave = true
  79. saveData() {
  80. let data = rootMgr.dataControl.serialize()
  81. aa.data.set(SaveComponent.getGameData(), data)
  82. this.isNeedSave = false
  83. }
  84. protected update(dt: number): void {
  85. if (!this.isNeedSave) return
  86. this.saveData()
  87. }
  88. }