GameConfigManager.ts 10 KB

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