GameDataCenter.ts 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. import { SevBack } from "../common/Xys";
  2. import AccountModel from "./model/Account/AccountModel";
  3. import GuideModel from "./model/GuideModel";
  4. import IDataModel from "./model/IDataModel";
  5. import LoadingModel from "./model/LoadingModel";
  6. import LoginModel from "./model/LoginModel";
  7. import PlatformModel from "./model/PlatformModel";
  8. import RedDotModel from "./model/RedDotModel";
  9. import { AudioModel } from "./model/System/AudioModel";
  10. import { SettingModel } from "./model/System/SettingModel";
  11. import SystemModel from "./model/System/SystemModel";
  12. import TaskModel from "./model/TaskModel";
  13. import TimeModel from "./model/TimeModel";
  14. import UserModel from "./model/UserModel";
  15. import WindowModel from "./model/WindowModel";
  16. class GameDataCenter {
  17. _tModel: Array<IDataModel> = [];
  18. _rspModel: Map<string, IDataModel[]> = new Map<string, IDataModel[]>()
  19. sevBack: SevBack = null;
  20. account: AccountModel = null
  21. audio: AudioModel = null
  22. setting: SettingModel = null
  23. system: SystemModel = null
  24. login: LoginModel = null
  25. time: TimeModel = null
  26. window: WindowModel = null
  27. loading: LoadingModel = null
  28. plat: PlatformModel = null
  29. reddot: RedDotModel = null
  30. user: UserModel;
  31. guide: GuideModel;
  32. task: TaskModel;
  33. constructor() {
  34. if (CC_PREVIEW) window["gdc"] = this;
  35. }
  36. newModel<T extends IDataModel>(c: { new(): T }): T {
  37. let obj = new c()
  38. this._tModel.push(obj);
  39. return obj
  40. }
  41. clear() {
  42. this._tModel.forEach(m => {
  43. m.clear();
  44. });
  45. }
  46. //初始化登录需要的模块
  47. initLoadModule() {
  48. this._tModel = []
  49. this.sevBack = null
  50. this.audio = this.newModel(AudioModel)
  51. this.setting = this.newModel(SettingModel)
  52. this.user = this.newModel(UserModel)
  53. this.account = this.newModel(AccountModel)
  54. this.login = this.newModel(LoginModel)
  55. this.plat = this.newModel(PlatformModel)
  56. this.window = this.newModel(WindowModel)
  57. this.loading = this.newModel(LoadingModel)
  58. this.time = this.newModel(TimeModel)
  59. this.initLoadRspModel()
  60. }
  61. initModule() {
  62. this.system = this.newModel(SystemModel)
  63. this.task = this.newModel(TaskModel)
  64. this.guide = this.newModel(GuideModel)
  65. // 红点注册必须在其他model之后
  66. this.reddot = this.newModel(RedDotModel);
  67. }
  68. initLoadRspModel() {
  69. this._rspModel.set("win", [this.window])
  70. this._rspModel.set("time", [this.time])
  71. }
  72. setRspModel(actName: string, model: IDataModel) {
  73. if (this._rspModel.has(actName)) {
  74. //该act已经有model监听,直接push
  75. let modelList = this._rspModel.get(actName)
  76. if (modelList.indexOf(model) < 0) {
  77. modelList.push(model)
  78. this._rspModel.set(actName, modelList)
  79. } else {
  80. console.warn("模块:", model.getModelName(), " 已注册了=>", actName)
  81. }
  82. } else {
  83. this._rspModel.set(actName, [model])
  84. }
  85. }
  86. }
  87. export default new GameDataCenter();