Newer
Older
DungeonShooting / DungeonShooting_Godot / src / game / manager / PropFragmentRegister.cs
@小李xl 小李xl on 17 Mar 2024 3 KB 重构主动道具中
  1. using System.Collections.Generic;
  2. using System.Linq;
  3. using System.Reflection;
  4. using Godot;
  5.  
  6. /// <summary>
  7. /// buff 属性注册类, 调用 Init() 函数初始化数据
  8. /// </summary>
  9. public class PropFragmentRegister
  10. {
  11. /// <summary>
  12. /// 所有 buff 属性信息
  13. /// </summary>
  14. public static Dictionary<string, PropFragmentInfo> BuffFragmentInfos { get; private set; }
  15. /// <summary>
  16. /// 所有主动道具条件数据
  17. /// </summary>
  18. public static Dictionary<string, PropFragmentInfo> ConditionFragmentInfos { get; private set; }
  19. /// <summary>
  20. /// 所有主动道具效果数据
  21. /// </summary>
  22. public static Dictionary<string, PropFragmentInfo> EffectFragmentInfos { get; private set; }
  23.  
  24. /// <summary>
  25. /// 初始化 buff
  26. /// </summary>
  27. public static void Init()
  28. {
  29. BuffFragmentInfos = new Dictionary<string, PropFragmentInfo>();
  30. ConditionFragmentInfos = new Dictionary<string, PropFragmentInfo>();
  31. EffectFragmentInfos = new Dictionary<string, PropFragmentInfo>();
  32.  
  33. var types = typeof(PropFragmentRegister).Assembly.GetTypes();
  34. //包含[BuffAttribute]特性
  35. var buffs = types.Where(type =>
  36. type.IsClass && !type.IsAbstract && type.IsAssignableTo(typeof(BuffFragment)));
  37. foreach (var type in buffs)
  38. {
  39. var attribute = (BuffFragmentAttribute)type.GetCustomAttribute(typeof(BuffFragmentAttribute), false);
  40. if (attribute != null)
  41. {
  42. if (BuffFragmentInfos.ContainsKey(attribute.BuffName))
  43. {
  44. GD.PrintErr($"Buff '{attribute.BuffName}' 重名!");
  45. continue;
  46. }
  47.  
  48. var buffInfo = new PropFragmentInfo(attribute.BuffName, attribute.Description, type);
  49. BuffFragmentInfos.Add(attribute.BuffName, buffInfo);
  50. }
  51. }
  52. //包含[ConditionAttribute]特性
  53. var conditions = types.Where(type =>
  54. type.IsClass && !type.IsAbstract && type.IsAssignableTo(typeof(ConditionFragment)));
  55. foreach (var type in conditions)
  56. {
  57. var attribute = (ConditionFragmentAttribute)type.GetCustomAttribute(typeof(ConditionFragmentAttribute), false);
  58. if (attribute != null)
  59. {
  60. if (ConditionFragmentInfos.ContainsKey(attribute.ConditionName))
  61. {
  62. GD.PrintErr($"Condition '{attribute.ConditionName}' 重名!");
  63. continue;
  64. }
  65.  
  66. var conditionInfo = new PropFragmentInfo(attribute.ConditionName, attribute.Description, type);
  67. ConditionFragmentInfos.Add(attribute.ConditionName, conditionInfo);
  68. }
  69. }
  70. //包含[EffectAttribute]特性
  71. var effects = types.Where(type =>
  72. type.IsClass && !type.IsAbstract && type.IsAssignableTo(typeof(EffectFragment)));
  73. foreach (var type in effects)
  74. {
  75. var attribute = (EffectFragmentAttribute)type.GetCustomAttribute(typeof(EffectFragmentAttribute), false);
  76. if (attribute != null)
  77. {
  78. if (EffectFragmentInfos.ContainsKey(attribute.EffectName))
  79. {
  80. GD.PrintErr($"Effect '{attribute.EffectName}' 重名!");
  81. continue;
  82. }
  83.  
  84. var effectInfo = new PropFragmentInfo(attribute.EffectName, attribute.Description, type);
  85. EffectFragmentInfos.Add(attribute.EffectName, effectInfo);
  86. }
  87. }
  88. }
  89. }