GameDataCenter.ts 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. import IDataModel from "../framework/data/model/IDataModel";
  2. import Global from "../framework/constant/Global";
  3. import { SingletonFactory } from "../framework/utils/SingletonFactory";
  4. import AccountModel from "../data/Account/AccountModel";
  5. import SystemModel from "../data/System/SystemModel";
  6. import Utils from "../framework/utils/utils";
  7. export default class GameDataCenter {
  8. static singleInstance: GameDataCenter = null;
  9. static getInstance(): GameDataCenter {
  10. if (GameDataCenter.singleInstance == null) {
  11. GameDataCenter.singleInstance = new GameDataCenter();
  12. }
  13. return GameDataCenter.singleInstance;
  14. }
  15. private _tModel: Array<IDataModel> = [];
  16. account: AccountModel = null; //用户数据
  17. system: SystemModel = null;
  18. staticData = {}; //表格数据保存
  19. constructor() {
  20. }
  21. newModel<T extends IDataModel>(c: { new(): T }): T {
  22. let obj = SingletonFactory.getInstance(c);
  23. this._tModel.push(obj);
  24. return obj
  25. }
  26. clear() {
  27. this._tModel.forEach(m => {
  28. m.clear();
  29. });
  30. }
  31. initModule() {
  32. AccountModel.getInstance();
  33. SystemModel.getInstance();
  34. }
  35. /**
  36. * 提前加载静态数据
  37. * @param callback
  38. */
  39. loadJsonData(callback) {
  40. // let resArr = [
  41. // "res/res_json/monster",
  42. // "res/res_json/monsterOut",
  43. // "res/res_json/stage",
  44. // "res/res_json/hero",
  45. // "res/res_json/weapon",
  46. // ]
  47. // bb.UILoader.loadResArr(resArr, function (asset) {
  48. // asset.forEach(element => {
  49. // let monsterName = element.name;
  50. // let monsterJson = element.json;
  51. // this.staticData[monsterName] = monsterJson;
  52. // });
  53. // this.pareData();
  54. // if (callback) {
  55. // callback()
  56. // }
  57. // }.bind(this))
  58. }
  59. /**
  60. * 初次 需要解析的数据
  61. */
  62. pareData() {
  63. for (const key in this.staticData[Global.ExcelJson.Stage]) {
  64. if (this.staticData[Global.ExcelJson.Stage].hasOwnProperty(key)) {
  65. const element = this.staticData[Global.ExcelJson.Stage][key];
  66. let stageId = element.id;
  67. element["monsterOut"] = {};
  68. for (const key1 in this.staticData[Global.ExcelJson.MonsterOut]) {
  69. if (this.staticData[Global.ExcelJson.MonsterOut].hasOwnProperty(key1)) {
  70. const element1 = this.staticData[Global.ExcelJson.MonsterOut][key1];
  71. if (element1.stageId == stageId) {
  72. if (!element["monsterOut"][element1.waves]) {
  73. element["monsterOut"][element1.waves] = [];
  74. }
  75. for (let index = 0; index < element1.monsterNum; index++) {
  76. element["monsterOut"][element1.waves].push(Utils.clone(element1))
  77. }
  78. }
  79. }
  80. }
  81. }
  82. }
  83. for (const key in this.staticData[Global.ExcelJson.Stage]) {
  84. if (this.staticData[Global.ExcelJson.Stage].hasOwnProperty(key)) {
  85. const element = this.staticData[Global.ExcelJson.Stage][key];
  86. let monsterNum = 0;
  87. for (const key1 in element["monsterOut"]) {
  88. if (element["monsterOut"].hasOwnProperty(key1)) {
  89. const element1 = element["monsterOut"][key1];
  90. monsterNum = monsterNum + element1.length;
  91. }
  92. }
  93. element.monsterNum = monsterNum;
  94. }
  95. }
  96. }
  97. /**
  98. *
  99. */
  100. GetDataByName(key: string) {
  101. return this.staticData[key];
  102. }
  103. }