Newer
Older
DungeonShooting / DungeonShooting_Godot / src / config / ExcelConfig.cs
@小李xl 小李xl on 22 Jun 2023 5 KB 添加武器自动上膛配置
  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. /// ActivityObject.xlsx表数据集合, 以 List 形式存储, 数据顺序与 Excel 表相同
  12. /// </summary>
  13. public static List<ActivityObject> ActivityObject_List { get; private set; }
  14. /// <summary>
  15. /// ActivityObject.xlsx表数据集合, 里 Map 形式存储, key 为 Id
  16. /// </summary>
  17. public static Dictionary<string, ActivityObject> ActivityObject_Map { get; private set; }
  18.  
  19. /// <summary>
  20. /// Sound.xlsx表数据集合, 以 List 形式存储, 数据顺序与 Excel 表相同
  21. /// </summary>
  22. public static List<Sound> Sound_List { get; private set; }
  23. /// <summary>
  24. /// Sound.xlsx表数据集合, 里 Map 形式存储, key 为 Id
  25. /// </summary>
  26. public static Dictionary<string, Sound> Sound_Map { get; private set; }
  27.  
  28. /// <summary>
  29. /// Weapon.xlsx表数据集合, 以 List 形式存储, 数据顺序与 Excel 表相同
  30. /// </summary>
  31. public static List<Weapon> Weapon_List { get; private set; }
  32. /// <summary>
  33. /// Weapon.xlsx表数据集合, 里 Map 形式存储, key 为 Id
  34. /// </summary>
  35. public static Dictionary<string, Weapon> Weapon_Map { get; private set; }
  36.  
  37.  
  38. private static bool _init = false;
  39. /// <summary>
  40. /// 初始化所有配置表数据
  41. /// </summary>
  42. public static void Init()
  43. {
  44. if (_init) return;
  45. _init = true;
  46.  
  47. _InitActivityObjectConfig();
  48. _InitSoundConfig();
  49. _InitWeaponConfig();
  50.  
  51. _InitWeaponRef();
  52. }
  53. private static void _InitActivityObjectConfig()
  54. {
  55. try
  56. {
  57. var text = _ReadConfigAsText("res://resource/config/ActivityObject.json");
  58. ActivityObject_List = JsonSerializer.Deserialize<List<ActivityObject>>(text);
  59. ActivityObject_Map = new Dictionary<string, ActivityObject>();
  60. foreach (var item in ActivityObject_List)
  61. {
  62. ActivityObject_Map.Add(item.Id, item);
  63. }
  64. }
  65. catch (Exception e)
  66. {
  67. GD.PrintErr(e.ToString());
  68. throw new Exception("初始化表'ActivityObject'失败!");
  69. }
  70. }
  71. private static void _InitSoundConfig()
  72. {
  73. try
  74. {
  75. var text = _ReadConfigAsText("res://resource/config/Sound.json");
  76. Sound_List = JsonSerializer.Deserialize<List<Sound>>(text);
  77. Sound_Map = new Dictionary<string, Sound>();
  78. foreach (var item in Sound_List)
  79. {
  80. Sound_Map.Add(item.Id, item);
  81. }
  82. }
  83. catch (Exception e)
  84. {
  85. GD.PrintErr(e.ToString());
  86. throw new Exception("初始化表'Sound'失败!");
  87. }
  88. }
  89. private static void _InitWeaponConfig()
  90. {
  91. try
  92. {
  93. var text = _ReadConfigAsText("res://resource/config/Weapon.json");
  94. Weapon_List = new List<Weapon>(JsonSerializer.Deserialize<List<Ref_Weapon>>(text));
  95. Weapon_Map = new Dictionary<string, Weapon>();
  96. foreach (var item in Weapon_List)
  97. {
  98. Weapon_Map.Add(item.Id, item);
  99. }
  100. }
  101. catch (Exception e)
  102. {
  103. GD.PrintErr(e.ToString());
  104. throw new Exception("初始化表'Weapon'失败!");
  105. }
  106. }
  107.  
  108. private static void _InitWeaponRef()
  109. {
  110. foreach (Ref_Weapon item in Weapon_List)
  111. {
  112. try
  113. {
  114. if (!string.IsNullOrEmpty(item.__ShootSound))
  115. {
  116. item.ShootSound = Sound_Map[item.__ShootSound];
  117. }
  118.  
  119. if (!string.IsNullOrEmpty(item.__BeginReloadSound))
  120. {
  121. item.BeginReloadSound = Sound_Map[item.__BeginReloadSound];
  122. }
  123.  
  124. if (!string.IsNullOrEmpty(item.__ReloadSound))
  125. {
  126. item.ReloadSound = Sound_Map[item.__ReloadSound];
  127. }
  128.  
  129. if (!string.IsNullOrEmpty(item.__ReloadFinishSound))
  130. {
  131. item.ReloadFinishSound = Sound_Map[item.__ReloadFinishSound];
  132. }
  133.  
  134. if (!string.IsNullOrEmpty(item.__BeLoadedSound))
  135. {
  136. item.BeLoadedSound = Sound_Map[item.__BeLoadedSound];
  137. }
  138.  
  139. if (item.__OtherSoundMap != null)
  140. {
  141. item.OtherSoundMap = new Dictionary<string, Sound>();
  142. foreach (var pair in item.__OtherSoundMap)
  143. {
  144. item.OtherSoundMap.Add(pair.Key, Sound_Map[pair.Value]);
  145. }
  146. }
  147.  
  148. if (!string.IsNullOrEmpty(item.__AiUseAttribute))
  149. {
  150. item.AiUseAttribute = Weapon_Map[item.__AiUseAttribute];
  151. }
  152.  
  153. }
  154. catch (Exception e)
  155. {
  156. GD.PrintErr(e.ToString());
  157. throw new Exception("初始化'Weapon'引用其他表数据失败, 当前行id: " + item.Id);
  158. }
  159. }
  160. }
  161. private static string _ReadConfigAsText(string path)
  162. {
  163. var file = FileAccess.Open(path, FileAccess.ModeFlags.Read);
  164. var asText = file.GetAsText();
  165. file.Dispose();
  166. return asText;
  167. }
  168. }