GameConfigManager.ts 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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. constructor(){
  18. }
  19. /**
  20. * 初始化
  21. * @param jsons
  22. */
  23. public static Init(jsons:JsonAsset[]):void{
  24. jsons.forEach(element => {
  25. jsons[element.name]=element;
  26. this.jsonMap.set(element.name,element);
  27. });
  28. this.InitWeaponConfig();
  29. this.InitMonsterConfig();
  30. this.InitFenceConfig();
  31. }
  32. /**
  33. * 初始化
  34. */
  35. private static InitWeaponConfig():void{
  36. if(this.jsonMap.has("Weapons")==false){
  37. return;
  38. }
  39. this.GetConfig("Weapons").forEach(element => {
  40. this.WeaponMap.set(element.id,element);
  41. });
  42. }
  43. private static InitMonsterConfig():void{
  44. if(this.jsonMap.has("Monsters")==false){
  45. return;
  46. }
  47. this.GetConfig("Monsters").forEach(element => {
  48. this.MonsterMap.set(element.id,element);
  49. });
  50. }
  51. private static InitFenceConfig():void{
  52. if(this.jsonMap.has("Fence")==false){
  53. return;
  54. }
  55. this.GetConfig("Fence").forEach(element => {
  56. this.FenceMap.set(element.id,element);
  57. });
  58. }
  59. /**
  60. * 获取配置数据
  61. * @param sheet
  62. * @param key
  63. */
  64. public static GetConfig(sheet:string):any{
  65. let jsonAsset:JsonAsset=this.jsonMap.get(sheet);
  66. let json:any=jsonAsset.json;
  67. return json.data;
  68. }
  69. /**
  70. * 获取武器ID
  71. * @param id
  72. */
  73. public static GetWeaponConfig(id:number):any{
  74. if(this.WeaponMap.has(id)==false){
  75. throw new Error("找不到ID为:"+id+"的武器!");
  76. }
  77. return this.WeaponMap.get(id);
  78. }
  79. /**
  80. * 获取关卡配置
  81. * @param id
  82. */
  83. public static GetLevelConfig(id:number):any{
  84. return this.GetConfig("Level"+id);
  85. }
  86. /**
  87. * 获取怪物模板配置
  88. * @param id
  89. */
  90. public static GetMonsterConfig(id:number):any{
  91. if(this.MonsterMap.has(id)==false){
  92. throw new Error("找不到ID为:"+id+"的怪物!");
  93. }
  94. return this.MonsterMap.get(id);
  95. }
  96. public static GetFenceConfig(id:number):any{
  97. if(this.FenceMap.has(id)==false){
  98. throw new Error("找不到ID为:"+id+"的栅栏!");
  99. }
  100. return this.FenceMap.get(id);
  101. }
  102. }