GameConfigManager.ts 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  1. import { JsonAsset } from "cc";
  2. export default class GameConfigManager
  3. {
  4. private static jsonMap:Map<string,JsonAsset>=new Map<string,JsonAsset>();
  5. /**
  6. * 武器配置
  7. */
  8. private static WeaponMap:Map<number,any>=new Map<number,any>();
  9. private static WeaponLevelMap:Map<number,any>=new Map<number,any>();
  10. /**
  11. * 怪物模板配置
  12. */
  13. private static MonsterMap:Map<number,any>=new Map<number,any>();
  14. /**
  15. * 栅栏配置
  16. */
  17. private static FenceMap:Map<number,any>=new Map<number,any>();
  18. /**
  19. * 技能配置
  20. */
  21. private static SkillMap:Map<number,any>=new Map<number,any>();
  22. /**
  23. * 关卡
  24. */
  25. private static LevelMap:Map<string,any>=new Map<string,any>();
  26. /**
  27. * 商城配置
  28. */
  29. private static ShopMap:Map<number,any>=new Map<number,any>();
  30. /**
  31. * 商城列表
  32. */
  33. public static ShopList:any[]=[];
  34. /**
  35. * 全局配置
  36. */
  37. private static GameGlobal:any;
  38. constructor(){
  39. }
  40. /**
  41. * 初始化
  42. * @param jsons
  43. */
  44. public static Init(jsons:JsonAsset[]):void{
  45. jsons.forEach(element => {
  46. jsons[element.name]=element;
  47. this.jsonMap.set(element.name,element);
  48. });
  49. this.InitGlobalConfig();
  50. this.InitWeaponConfig();
  51. this.InitMonsterConfig();
  52. this.InitFenceConfig();
  53. this.InitSkillConfig();
  54. this.InitLevelConfig();
  55. this.initShopConfig();
  56. }
  57. private static InitGlobalConfig():void{
  58. if(this.jsonMap.has("GameGlobal")==false){
  59. return;
  60. }
  61. let jsonAsset:JsonAsset=this.GetConfig("GameGlobal");
  62. let list:any=jsonAsset.json;
  63. this.GameGlobal=list[0];
  64. }
  65. /**
  66. * 初始化
  67. */
  68. private static InitWeaponConfig():void{
  69. if(this.jsonMap.has("Weapons")==false){
  70. return;
  71. }
  72. let jsonAsset:JsonAsset=this.GetConfig("Weapons");
  73. let list:any=jsonAsset.json;
  74. list.forEach(element => {
  75. this.WeaponMap.set(element.id,element);
  76. this.WeaponLevelMap.set(element.level,element);
  77. });
  78. }
  79. private static InitMonsterConfig():void{
  80. if(this.jsonMap.has("Monsters")==false){
  81. return;
  82. }
  83. let jsonAsset:JsonAsset=this.GetConfig("Monsters");
  84. let list:any=jsonAsset.json;
  85. list.forEach(element => {
  86. this.MonsterMap.set(element.id,element);
  87. });
  88. }
  89. private static InitFenceConfig():void{
  90. if(this.jsonMap.has("Fence")==false){
  91. return;
  92. }
  93. let jsonAsset:JsonAsset=this.GetConfig("Fence");
  94. let list:any=jsonAsset.json;
  95. list.forEach(element => {
  96. this.FenceMap.set(element.id,element);
  97. });
  98. }
  99. private static InitSkillConfig():void{
  100. if(this.jsonMap.has("Skill")==false){
  101. return;
  102. }
  103. let jsonAsset:JsonAsset=this.GetConfig("Skill");
  104. let list:any=jsonAsset.json;
  105. list.forEach(element => {
  106. this.SkillMap.set(element.id,element);
  107. });
  108. }
  109. private static InitLevelConfig():void{
  110. if(this.jsonMap.has("Levels")==false){
  111. return;
  112. }
  113. let jsonAsset:JsonAsset=this.GetConfig("Levels");
  114. let list:any=jsonAsset.json;
  115. list.forEach(element => {
  116. this.LevelMap.set(element.id,element);
  117. });
  118. }
  119. private static initShopConfig():void{
  120. if(this.jsonMap.has("Shop")==false){
  121. return;
  122. }
  123. let jsonAsset:JsonAsset=this.GetConfig("Shop");
  124. let list:any=jsonAsset.json;
  125. list.forEach(element => {
  126. this.ShopMap.set(element.id,element);
  127. this.ShopList.push(element);
  128. });
  129. }
  130. /**
  131. * 获取商城配置
  132. * @param weaponId
  133. */
  134. public static GetShopConfig(weaponId:number):any{
  135. let weaponConfig:any=this.GetWeaponConfig(weaponId);
  136. if(this.ShopMap.has(weaponConfig.level)){
  137. return this.ShopMap.get(weaponConfig.level);
  138. }
  139. return null;
  140. }
  141. /**
  142. * 获取快速购买的武器ID
  143. * @param weaponId
  144. */
  145. public static GetQuickBuyWeaponId(weaponId:number):number{
  146. let shopConfig:any=this.GetShopConfig(weaponId);
  147. if(shopConfig==null){
  148. throw new Error("找不到相关商城配置!");
  149. }
  150. return shopConfig.quickId;
  151. }
  152. /**
  153. * 获取最大关卡数
  154. */
  155. public static get MaxLevel():number{
  156. return this.LevelMap.size;
  157. }
  158. /**
  159. * 获取配置数据
  160. * @param sheet
  161. * @param key
  162. */
  163. public static GetConfig(sheet:string):any{
  164. return this.jsonMap.get(sheet);
  165. }
  166. /**
  167. * 获取全局配置表中的值
  168. * @param key
  169. */
  170. public static getGlobalValue(key:string):any{
  171. return this.GameGlobal[key];
  172. }
  173. /**
  174. * 获取武器ID
  175. * @param id
  176. */
  177. public static GetWeaponConfig(id:number):any{
  178. if(this.WeaponMap.has(id)==false){
  179. console.error("找不到ID为:"+id+"的武器!");
  180. return null;
  181. }
  182. return this.WeaponMap.get(id);
  183. }
  184. /**
  185. * 通过武器等级获取武器配置
  186. * @param level
  187. */
  188. public static GetWeaponConfigByLevel(level:number):any{
  189. if(this.WeaponLevelMap.has(level)==false){
  190. return null;
  191. }
  192. return this.WeaponLevelMap.get(level);
  193. }
  194. /**
  195. * 获取下一级的武器ID
  196. * @param id
  197. */
  198. public static GetNextLevelWeaponId(id:number):number{
  199. let weaponConfig:any=this.GetWeaponConfig(id);
  200. let nextLevel:number=weaponConfig.level+1;
  201. let nextConfig=this.GetWeaponConfigByLevel(nextLevel);
  202. return nextConfig.id;
  203. }
  204. /**
  205. * 武器是否已到达最高等级
  206. * @param id
  207. */
  208. public static WeaponIsMaxLevel(id:number):boolean{
  209. let weaponConfig:any=this.GetWeaponConfig(id);
  210. let trylevel:number=weaponConfig.level+1;
  211. if(this.GetWeaponConfigByLevel(trylevel)==null){
  212. return true;
  213. }
  214. return false;
  215. }
  216. /**
  217. * 获取关卡配置
  218. * @param id
  219. */
  220. public static GetLevelConfig(id:number):any{
  221. let strId:string="level"+id;
  222. if(this.LevelMap.has(strId)==false){
  223. throw new Error("找不到ID为:"+strId+"的关卡配置!");
  224. }
  225. return this.LevelMap.get(strId);
  226. }
  227. /**
  228. * 获取怪物模板配置
  229. * @param id
  230. */
  231. public static GetMonsterConfig(id:number):any{
  232. if(this.MonsterMap.has(id)==false){
  233. throw new Error("找不到ID为:"+id+"的怪物!");
  234. }
  235. return this.MonsterMap.get(id);
  236. }
  237. public static GetFenceConfig(id:number):any{
  238. if(this.FenceMap.has(id)==false){
  239. throw new Error("找不到ID为:"+id+"的栅栏!");
  240. }
  241. return this.FenceMap.get(id);
  242. }
  243. public static GetSkillConfig(id:number):any{
  244. if(this.SkillMap.has(id)==false){
  245. throw new Error("找不到ID为:"+id+"的技能!");
  246. }
  247. return this.SkillMap.get(id);
  248. }
  249. /**
  250. * 通过怪物刷新规则构建怪物配置
  251. */
  252. public static CreateMonsterByList(config:any,currentTime:number):any[]{
  253. let monsters:any[]=[];
  254. let monsterConfig;
  255. let time:number=currentTime;
  256. config.forEach(element => {
  257. //单个配置
  258. if(element.type==undefined||element.type==0){
  259. monsters.push(element);
  260. }else if(element.type==1){
  261. if(element.count==0){
  262. throw new Error("怪物数量不能为0");
  263. }
  264. for (let index = 0; index < element.count; index++) {
  265. monsterConfig={};
  266. monsterConfig.mId=element.mId+"_"+index;
  267. monsterConfig.monsterId=element.monsterId;
  268. monsterConfig.createTime=time;
  269. monsterConfig.hp=element.hp;
  270. monsterConfig.damage=element.damage;
  271. monsterConfig.anger=element.anger;
  272. if(element.startPosType==1){
  273. let r:number=Math.random()*9;
  274. if(r<3){
  275. monsterConfig.startPos=0;
  276. }if(r<6){
  277. monsterConfig.startPos=1;
  278. }else{
  279. monsterConfig.startPos=2;
  280. }
  281. }else{
  282. monsterConfig.startPos=element.startPos;
  283. }
  284. time+=element.intervalTime;
  285. monsters.push(monsterConfig);
  286. }
  287. }
  288. });
  289. return monsters;
  290. }
  291. }