ConfigDataMgr.ts 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. import { Singleton } from "../Utils/Singleton";
  2. import { attr, consume, exp, fuli_day, task, fuli_watch, nick } from "../Config/ConfigConst";
  3. import { NumberUtil } from "../Utils/NumberUtil";
  4. import { App } from "../../Manager/App";
  5. import { EConfigAttrProp, EAgeGroup } from "../Const/EnumDefine";
  6. /**
  7. * 配置管理类
  8. */
  9. export class ConfigDataMgr extends Singleton {
  10. private _config: any;
  11. constructor() {
  12. super();
  13. }
  14. private get attrs(): attr[] { return this._config["attr"] as attr[]; }
  15. private get consumes(): consume[] { return this._config["consume"] as consume[]; }
  16. private get exps(): exp[] { return this._config["exp"] as exp[]; }
  17. private get tasks(): task[] { return this._config["task"] as task[]; }
  18. private get fuli_days(): fuli_day[] { return this._config["fuli_day"] as fuli_day[]; }
  19. // private get fuli_times(): fuli_time[] { return this._config["fuli_time"] as fuli_time[]; }
  20. private get fuli_watchs(): fuli_watch[] { return this._config["fuli_watch"] as fuli_watch[]; }
  21. private get nicks(): nick[] { return this._config["nick"] as nick[]; }
  22. public set configs(res) {
  23. console.log("解析配置文件");
  24. if (!res) return;
  25. for (let i = 0; i < res.length; i++) {
  26. let arr = [];
  27. let json = res[i]["json"];
  28. for (let j = 0; j < json.length; j++) {
  29. arr.push(json[j]);
  30. }
  31. this._config[res[i].name] = arr;
  32. }
  33. }
  34. public get configs() {
  35. if (this._config)
  36. return this._config;
  37. else {
  38. console.log("config配置文件还未读取!!");
  39. return null;
  40. }
  41. }
  42. /** 导入配置文件 */
  43. public loadConfigs(cb: Function) {
  44. console.log("读取配置文件");
  45. this._config = {};
  46. let url = "configs"
  47. let self = this;
  48. cc.loader.loadResDir(url, cc.Asset, function (err, res) {
  49. self.configs = res;
  50. console.log("配置文件解析完成");
  51. console.log(self.attrs);
  52. App.DataManager.init();
  53. cb();
  54. })
  55. }
  56. /** 随机生成昵称 */
  57. public getNick() {
  58. let range = this.nicks.length;
  59. return this.nicks[NumberUtil.getRandomInt(0, range)];
  60. }
  61. /** 获取消耗列表 */
  62. public getConsumeList(): consume[] {
  63. return this.consumes;
  64. }
  65. /** 获取任务列表 */
  66. public getTaskList(): task[] {
  67. return this.tasks;
  68. }
  69. /** 获取福利Day列表 */
  70. public getFuLiDayList(): fuli_day[] {
  71. return this.fuli_days;
  72. }
  73. /** 获取福利Watch列表 */
  74. public getFuLiWatchList(): fuli_watch[] {
  75. return this.fuli_watchs;
  76. }
  77. /** 获取指定狗的初始属性 */
  78. public getBeginingAttr(prop: EConfigAttrProp): attr {
  79. let result;
  80. for (let i = 0; i < this.attrs.length; i++) {
  81. let item = this.attrs[i];
  82. if (item.prop == EConfigAttrProp[prop]) {
  83. result = item;
  84. break;
  85. }
  86. }
  87. return result;
  88. }
  89. /** 获取年龄段 */
  90. public getAgeGroup(level: number): EAgeGroup {
  91. return level < 11 ? EAgeGroup.PERIOD_KIDS
  92. : level < 31 ? EAgeGroup.PERIOD_YOUNG : EAgeGroup.PERIOD_GROWNUP;
  93. }
  94. /** 获取指定狗的成长属性 */
  95. public getGrowingAttr(level: number): attr {
  96. let prop = level < 11 ? EConfigAttrProp.cq_childhood
  97. : level < 31 ? EConfigAttrProp.cq_youth : EConfigAttrProp.cq_adulthood;
  98. let result: attr;
  99. for (let i = 0; i < this.attrs.length; i++) {
  100. let item = this.attrs[i];
  101. if (item.prop == EConfigAttrProp[prop]) {
  102. result = item;
  103. break;
  104. }
  105. }
  106. return result;
  107. }
  108. /** 通过亲密度等级获取升级所需经验 */
  109. public getExpByLv(lv: number): number {
  110. for (let i = 0; i < this.exps.length; i++) {
  111. let item = this.exps[i];
  112. if (item.lv == lv) {
  113. return item.exp;
  114. }
  115. }
  116. return -1;
  117. }
  118. /** 获取狗的最高等级 */
  119. public get DOG_MAX_LEVEL(): number {
  120. return this.exps.length;
  121. }
  122. /** 获取每日签到奖励 */
  123. public getFuLiDayReward(day: number) {
  124. for (let i = 0; i < this.fuli_days.length; i++) {
  125. if (this.fuli_days[i].day == day)
  126. return this.fuli_days[i].reward;
  127. }
  128. console.log("出错了");
  129. }
  130. }