GameConfigManager.ts 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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. /**
  10. * 怪物模板配置
  11. */
  12. private static MonsterMap:Map<number,any>=new Map<number,any>();
  13. /**
  14. * 栅栏配置
  15. */
  16. private static FenceMap:Map<number,any>=new Map<number,any>();
  17. /**
  18. * 技能配置
  19. */
  20. private static SkillMap:Map<number,any>=new Map<number,any>();
  21. constructor(){
  22. }
  23. /**
  24. * 初始化
  25. * @param jsons
  26. */
  27. public static Init(jsons:JsonAsset[]):void{
  28. jsons.forEach(element => {
  29. jsons[element.name]=element;
  30. this.jsonMap.set(element.name,element);
  31. });
  32. this.InitWeaponConfig();
  33. this.InitMonsterConfig();
  34. this.InitFenceConfig();
  35. this.InitSkillConfig();
  36. }
  37. /**
  38. * 初始化
  39. */
  40. private static InitWeaponConfig():void{
  41. if(this.jsonMap.has("Weapons")==false){
  42. return;
  43. }
  44. this.GetConfig("Weapons").forEach(element => {
  45. this.WeaponMap.set(element.id,element);
  46. });
  47. }
  48. private static InitMonsterConfig():void{
  49. if(this.jsonMap.has("Monsters")==false){
  50. return;
  51. }
  52. this.GetConfig("Monsters").forEach(element => {
  53. this.MonsterMap.set(element.id,element);
  54. });
  55. }
  56. private static InitFenceConfig():void{
  57. if(this.jsonMap.has("Fence")==false){
  58. return;
  59. }
  60. this.GetConfig("Fence").forEach(element => {
  61. this.FenceMap.set(element.id,element);
  62. });
  63. }
  64. private static InitSkillConfig():void{
  65. if(this.jsonMap.has("Skill")==false){
  66. return;
  67. }
  68. this.GetConfig("Skill").forEach(element => {
  69. this.SkillMap.set(element.id,element);
  70. });
  71. }
  72. /**
  73. * 获取配置数据
  74. * @param sheet
  75. * @param key
  76. */
  77. public static GetConfig(sheet:string):any{
  78. let jsonAsset:JsonAsset=this.jsonMap.get(sheet);
  79. let json:any=jsonAsset.json;
  80. return json.data;
  81. }
  82. /**
  83. * 获取武器ID
  84. * @param id
  85. */
  86. public static GetWeaponConfig(id:number):any{
  87. if(this.WeaponMap.has(id)==false){
  88. throw new Error("找不到ID为:"+id+"的武器!");
  89. }
  90. return this.WeaponMap.get(id);
  91. }
  92. /**
  93. * 获取关卡配置
  94. * @param id
  95. */
  96. public static GetLevelConfig(id:number):any{
  97. return this.GetConfig("Level"+id);
  98. }
  99. /**
  100. * 获取怪物模板配置
  101. * @param id
  102. */
  103. public static GetMonsterConfig(id:number):any{
  104. if(this.MonsterMap.has(id)==false){
  105. throw new Error("找不到ID为:"+id+"的怪物!");
  106. }
  107. return this.MonsterMap.get(id);
  108. }
  109. public static GetFenceConfig(id:number):any{
  110. if(this.FenceMap.has(id)==false){
  111. throw new Error("找不到ID为:"+id+"的栅栏!");
  112. }
  113. return this.FenceMap.get(id);
  114. }
  115. public static GetSkillConfig(id:number):any{
  116. if(this.SkillMap.has(id)==false){
  117. throw new Error("找不到ID为:"+id+"的技能!");
  118. }
  119. return this.SkillMap.get(id);
  120. }
  121. /**
  122. * 通过怪物刷新规则构建怪物配置
  123. */
  124. public static CreateMonsterByList(config:any,currentTime:number):any[]{
  125. let monsters:any[]=[];
  126. let monsterConfig;
  127. let time:number=currentTime;
  128. config.forEach(element => {
  129. //单个配置
  130. if(element.type==undefined||element.type==0){
  131. monsters.push(element);
  132. }else if(element.type==1){
  133. if(element.count==0){
  134. throw new Error("怪物数量不能为0");
  135. }
  136. for (let index = 0; index < element.count; index++) {
  137. monsterConfig={};
  138. monsterConfig.id=element.id+"_"+index;
  139. monsterConfig.monsterId=element.monsterId;
  140. monsterConfig.createTime=time;
  141. monsterConfig.hp=element.hp;
  142. monsterConfig.damage=element.damage;
  143. monsterConfig.anger=element.anger;
  144. if(element.startPosType==1){
  145. let r:number=Math.random()*9;
  146. if(r<3){
  147. monsterConfig.startPos=0;
  148. }if(r<6){
  149. monsterConfig.startPos=1;
  150. }else{
  151. monsterConfig.startPos=2;
  152. }
  153. }else{
  154. monsterConfig.startPos=element.startPos;
  155. }
  156. time+=element.intervalTime;
  157. monsters.push(monsterConfig);
  158. }
  159. }
  160. });
  161. return monsters;
  162. }
  163. }