GameDataCenter.ts 3.5 KB

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