GameConfigManager.ts 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  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. if(this.ShopMap.has(weaponId)){
  136. return this.ShopMap.get(weaponId);
  137. }
  138. return null;
  139. }
  140. /**
  141. * 获取快速购买的武器ID
  142. * @param weaponId
  143. */
  144. public static GetQuickBuyWeaponId(weaponId:number):number{
  145. if(this.ShopMap.has(weaponId)==false){
  146. throw new Error("找不到快速购买配置:"+weaponId);
  147. }
  148. let config:any=this.ShopMap.get(weaponId);
  149. return config.quickId;
  150. }
  151. /**
  152. * 获取最大关卡数
  153. */
  154. public static get MaxLevel():number{
  155. return this.LevelMap.size;
  156. }
  157. /**
  158. * 获取配置数据
  159. * @param sheet
  160. * @param key
  161. */
  162. public static GetConfig(sheet:string):any{
  163. return this.jsonMap.get(sheet);
  164. }
  165. /**
  166. * 获取全局配置表中的值
  167. * @param key
  168. */
  169. public static getGlobalValue(key:string):any{
  170. return this.GameGlobal[key];
  171. }
  172. /**
  173. * 获取武器ID
  174. * @param id
  175. */
  176. public static GetWeaponConfig(id:number):any{
  177. if(this.WeaponMap.has(id)==false){
  178. console.error("找不到ID为:"+id+"的武器!");
  179. return null;
  180. }
  181. return this.WeaponMap.get(id);
  182. }
  183. /**
  184. * 通过武器等级获取武器配置
  185. * @param level
  186. */
  187. public static GetWeaponConfigByLevel(level:number):any{
  188. if(this.WeaponLevelMap.has(level)==false){
  189. return null;
  190. }
  191. return this.WeaponLevelMap.get(level);
  192. }
  193. /**
  194. * 获取下一级的武器ID
  195. * @param id
  196. */
  197. public static GetNextLevelWeaponId(id:number):number{
  198. let weaponConfig:any=this.GetWeaponConfig(id);
  199. let nextLevel:number=weaponConfig.level+1;
  200. let nextConfig=this.GetWeaponConfigByLevel(nextLevel);
  201. return nextConfig.id;
  202. }
  203. /**
  204. * 武器是否已到达最高等级
  205. * @param id
  206. */
  207. public static WeaponIsMaxLevel(id:number):boolean{
  208. let weaponConfig:any=this.GetWeaponConfig(id);
  209. let trylevel:number=weaponConfig.level+1;
  210. if(this.GetWeaponConfigByLevel(trylevel)==null){
  211. return true;
  212. }
  213. return false;
  214. }
  215. /**
  216. * 获取关卡配置
  217. * @param id
  218. */
  219. public static GetLevelConfig(id:number):any{
  220. let strId:string="level"+id;
  221. if(this.LevelMap.has(strId)==false){
  222. throw new Error("找不到ID为:"+strId+"的关卡配置!");
  223. }
  224. return this.LevelMap.get(strId);
  225. }
  226. /**
  227. * 获取怪物模板配置
  228. * @param id
  229. */
  230. public static GetMonsterConfig(id:number):any{
  231. if(this.MonsterMap.has(id)==false){
  232. throw new Error("找不到ID为:"+id+"的怪物!");
  233. }
  234. return this.MonsterMap.get(id);
  235. }
  236. public static GetFenceConfig(id:number):any{
  237. if(this.FenceMap.has(id)==false){
  238. throw new Error("找不到ID为:"+id+"的栅栏!");
  239. }
  240. return this.FenceMap.get(id);
  241. }
  242. public static GetSkillConfig(id:number):any{
  243. if(this.SkillMap.has(id)==false){
  244. throw new Error("找不到ID为:"+id+"的技能!");
  245. }
  246. return this.SkillMap.get(id);
  247. }
  248. /**
  249. * 通过怪物刷新规则构建怪物配置
  250. */
  251. public static CreateMonsterByList(config:any,currentTime:number):any[]{
  252. if(config==undefined||config==null||config.length==0){
  253. throw new Error("关卡配置中怪物列表为空!");
  254. }
  255. let monsters:any[]=[];
  256. let monsterConfig;
  257. let time:number=currentTime;
  258. config.forEach(element => {
  259. //单个配置
  260. if(element.type==undefined||element.type==0){
  261. monsters.push(element);
  262. }else if(element.type==1){
  263. if(element.count==0){
  264. throw new Error("怪物数量不能为0");
  265. }
  266. for (let index = 0; index < element.count; index++) {
  267. monsterConfig={};
  268. monsterConfig.mId=element.mId+"_"+index;
  269. monsterConfig.monsterId=element.monsterId;
  270. monsterConfig.createTime=time;
  271. monsterConfig.hp=element.hp;
  272. monsterConfig.damage=element.damage;
  273. monsterConfig.anger=element.anger;
  274. monsterConfig.integral=element.integral;
  275. if(element.startPosType==1){
  276. let r:number=Math.random()*9;
  277. if(r<3){
  278. monsterConfig.startPos=0;
  279. }else if(r<6){
  280. monsterConfig.startPos=1;
  281. }else{
  282. monsterConfig.startPos=2;
  283. }
  284. }else{
  285. monsterConfig.startPos=element.startPos;
  286. }
  287. time+=element.intervalTime;
  288. monsters.push(monsterConfig);
  289. }
  290. }
  291. });
  292. return monsters;
  293. }
  294. }