GameConfigManager.ts 11 KB

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