Newer
Older
DungeonShooting / DungeonShooting_Godot / src / config / ExcelConfig.cs
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text.Json;
  4. using Godot;
  5.  
  6. namespace Config;
  7.  
  8. public static partial class ExcelConfig
  9. {
  10. /// <summary>
  11. /// ActivityBase.xlsx表数据集合, 以 List 形式存储, 数据顺序与 Excel 表相同
  12. /// </summary>
  13. public static List<ActivityBase> ActivityBase_List { get; private set; }
  14. /// <summary>
  15. /// ActivityBase.xlsx表数据集合, 里 Map 形式存储, key 为 Id
  16. /// </summary>
  17. public static Dictionary<string, ActivityBase> ActivityBase_Map { get; private set; }
  18.  
  19. /// <summary>
  20. /// AiAttackAttr.xlsx表数据集合, 以 List 形式存储, 数据顺序与 Excel 表相同
  21. /// </summary>
  22. public static List<AiAttackAttr> AiAttackAttr_List { get; private set; }
  23. /// <summary>
  24. /// AiAttackAttr.xlsx表数据集合, 里 Map 形式存储, key 为 Id
  25. /// </summary>
  26. public static Dictionary<string, AiAttackAttr> AiAttackAttr_Map { get; private set; }
  27.  
  28. /// <summary>
  29. /// BulletBase.xlsx表数据集合, 以 List 形式存储, 数据顺序与 Excel 表相同
  30. /// </summary>
  31. public static List<BulletBase> BulletBase_List { get; private set; }
  32. /// <summary>
  33. /// BulletBase.xlsx表数据集合, 里 Map 形式存储, key 为 Id
  34. /// </summary>
  35. public static Dictionary<string, BulletBase> BulletBase_Map { get; private set; }
  36.  
  37. /// <summary>
  38. /// Sound.xlsx表数据集合, 以 List 形式存储, 数据顺序与 Excel 表相同
  39. /// </summary>
  40. public static List<Sound> Sound_List { get; private set; }
  41. /// <summary>
  42. /// Sound.xlsx表数据集合, 里 Map 形式存储, key 为 Id
  43. /// </summary>
  44. public static Dictionary<string, Sound> Sound_Map { get; private set; }
  45.  
  46. /// <summary>
  47. /// WeaponBase.xlsx表数据集合, 以 List 形式存储, 数据顺序与 Excel 表相同
  48. /// </summary>
  49. public static List<WeaponBase> WeaponBase_List { get; private set; }
  50. /// <summary>
  51. /// WeaponBase.xlsx表数据集合, 里 Map 形式存储, key 为 Id
  52. /// </summary>
  53. public static Dictionary<string, WeaponBase> WeaponBase_Map { get; private set; }
  54.  
  55.  
  56. private static bool _init = false;
  57. /// <summary>
  58. /// 初始化所有配置表数据
  59. /// </summary>
  60. public static void Init()
  61. {
  62. if (_init) return;
  63. _init = true;
  64.  
  65. _InitActivityBaseConfig();
  66. _InitAiAttackAttrConfig();
  67. _InitBulletBaseConfig();
  68. _InitSoundConfig();
  69. _InitWeaponBaseConfig();
  70.  
  71. _InitWeaponBaseRef();
  72. }
  73. private static void _InitActivityBaseConfig()
  74. {
  75. try
  76. {
  77. var text = _ReadConfigAsText("res://resource/config/ActivityBase.json");
  78. ActivityBase_List = JsonSerializer.Deserialize<List<ActivityBase>>(text);
  79. ActivityBase_Map = new Dictionary<string, ActivityBase>();
  80. foreach (var item in ActivityBase_List)
  81. {
  82. ActivityBase_Map.Add(item.Id, item);
  83. }
  84. }
  85. catch (Exception e)
  86. {
  87. GD.PrintErr(e.ToString());
  88. throw new Exception("初始化表'ActivityBase'失败!");
  89. }
  90. }
  91. private static void _InitAiAttackAttrConfig()
  92. {
  93. try
  94. {
  95. var text = _ReadConfigAsText("res://resource/config/AiAttackAttr.json");
  96. AiAttackAttr_List = JsonSerializer.Deserialize<List<AiAttackAttr>>(text);
  97. AiAttackAttr_Map = new Dictionary<string, AiAttackAttr>();
  98. foreach (var item in AiAttackAttr_List)
  99. {
  100. AiAttackAttr_Map.Add(item.Id, item);
  101. }
  102. }
  103. catch (Exception e)
  104. {
  105. GD.PrintErr(e.ToString());
  106. throw new Exception("初始化表'AiAttackAttr'失败!");
  107. }
  108. }
  109. private static void _InitBulletBaseConfig()
  110. {
  111. try
  112. {
  113. var text = _ReadConfigAsText("res://resource/config/BulletBase.json");
  114. BulletBase_List = JsonSerializer.Deserialize<List<BulletBase>>(text);
  115. BulletBase_Map = new Dictionary<string, BulletBase>();
  116. foreach (var item in BulletBase_List)
  117. {
  118. BulletBase_Map.Add(item.Id, item);
  119. }
  120. }
  121. catch (Exception e)
  122. {
  123. GD.PrintErr(e.ToString());
  124. throw new Exception("初始化表'BulletBase'失败!");
  125. }
  126. }
  127. private static void _InitSoundConfig()
  128. {
  129. try
  130. {
  131. var text = _ReadConfigAsText("res://resource/config/Sound.json");
  132. Sound_List = JsonSerializer.Deserialize<List<Sound>>(text);
  133. Sound_Map = new Dictionary<string, Sound>();
  134. foreach (var item in Sound_List)
  135. {
  136. Sound_Map.Add(item.Id, item);
  137. }
  138. }
  139. catch (Exception e)
  140. {
  141. GD.PrintErr(e.ToString());
  142. throw new Exception("初始化表'Sound'失败!");
  143. }
  144. }
  145. private static void _InitWeaponBaseConfig()
  146. {
  147. try
  148. {
  149. var text = _ReadConfigAsText("res://resource/config/WeaponBase.json");
  150. WeaponBase_List = new List<WeaponBase>(JsonSerializer.Deserialize<List<Ref_WeaponBase>>(text));
  151. WeaponBase_Map = new Dictionary<string, WeaponBase>();
  152. foreach (var item in WeaponBase_List)
  153. {
  154. WeaponBase_Map.Add(item.Id, item);
  155. }
  156. }
  157. catch (Exception e)
  158. {
  159. GD.PrintErr(e.ToString());
  160. throw new Exception("初始化表'WeaponBase'失败!");
  161. }
  162. }
  163.  
  164. private static void _InitWeaponBaseRef()
  165. {
  166. foreach (Ref_WeaponBase item in WeaponBase_List)
  167. {
  168. try
  169. {
  170. if (!string.IsNullOrEmpty(item.__ShootSound))
  171. {
  172. item.ShootSound = Sound_Map[item.__ShootSound];
  173. }
  174.  
  175. if (!string.IsNullOrEmpty(item.__BeginReloadSound))
  176. {
  177. item.BeginReloadSound = Sound_Map[item.__BeginReloadSound];
  178. }
  179.  
  180. if (!string.IsNullOrEmpty(item.__ReloadSound))
  181. {
  182. item.ReloadSound = Sound_Map[item.__ReloadSound];
  183. }
  184.  
  185. if (!string.IsNullOrEmpty(item.__ReloadFinishSound))
  186. {
  187. item.ReloadFinishSound = Sound_Map[item.__ReloadFinishSound];
  188. }
  189.  
  190. if (!string.IsNullOrEmpty(item.__BeLoadedSound))
  191. {
  192. item.BeLoadedSound = Sound_Map[item.__BeLoadedSound];
  193. }
  194.  
  195. if (item.__OtherSoundMap != null)
  196. {
  197. item.OtherSoundMap = new Dictionary<string, Sound>();
  198. foreach (var pair in item.__OtherSoundMap)
  199. {
  200. item.OtherSoundMap.Add(pair.Key, Sound_Map[pair.Value]);
  201. }
  202. }
  203.  
  204. if (!string.IsNullOrEmpty(item.__AiUseAttribute))
  205. {
  206. item.AiUseAttribute = WeaponBase_Map[item.__AiUseAttribute];
  207. }
  208.  
  209. if (!string.IsNullOrEmpty(item.__AiAttackAttr))
  210. {
  211. item.AiAttackAttr = AiAttackAttr_Map[item.__AiAttackAttr];
  212. }
  213.  
  214. }
  215. catch (Exception e)
  216. {
  217. GD.PrintErr(e.ToString());
  218. throw new Exception("初始化'WeaponBase'引用其他表数据失败, 当前行id: " + item.Id);
  219. }
  220. }
  221. }
  222. private static string _ReadConfigAsText(string path)
  223. {
  224. var file = FileAccess.Open(path, FileAccess.ModeFlags.Read);
  225. var asText = file.GetAsText();
  226. file.Dispose();
  227. return asText;
  228. }
  229. }