BehaviorTreeComponent.cs 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Reflection;
  4. using UnityEngine;
  5. namespace Model
  6. {
  7. [EntityEvent(EntityEventId.BehaviorTreeComponent)]
  8. public class BehaviorTreeComponent : Component
  9. {
  10. private Dictionary<string, Func<NodeProto, Node>> dictionary;
  11. private Dictionary<GameObject, BehaviorTree> treeCache;
  12. public void Awake()
  13. {
  14. this.Load();
  15. }
  16. public void Load()
  17. {
  18. this.dictionary = new Dictionary<string, Func<NodeProto, Node>>();
  19. this.treeCache = new Dictionary<GameObject, BehaviorTree>();
  20. Type[] types = DllHelper.GetMonoTypes();
  21. foreach (Type type in types)
  22. {
  23. object[] attrs = type.GetCustomAttributes(typeof(NodeAttribute), false);
  24. if (attrs.Length == 0)
  25. {
  26. continue;
  27. }
  28. Type classType = type;
  29. if (this.dictionary.ContainsKey(type.Name))
  30. {
  31. throw new GameException($"已经存在同类节点: {classType.Name}");
  32. }
  33. this.dictionary.Add(type.Name, config =>
  34. {
  35. Node node = (Node)Activator.CreateInstance(classType, config);
  36. try
  37. {
  38. InitFieldValue(ref node, config);
  39. }
  40. catch (Exception e)
  41. {
  42. throw new GameException($"InitFieldValue error, node: {node.Id} {node.Type}", e);
  43. }
  44. return node;
  45. });
  46. }
  47. }
  48. private static void InitFieldValue(ref Node node, NodeProto nodeProto)
  49. {
  50. Type type = Game.EntityEventManager.GetAssembly("Model").GetType("Model." + nodeProto.name);
  51. foreach (var args_item in nodeProto.args_dict)
  52. {
  53. FieldInfo fieldInfo = type.GetField(args_item.Key, BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public);
  54. if (fieldInfo == null)
  55. {
  56. continue;
  57. }
  58. NewSetValue(ref node, fieldInfo, args_item.Value, nodeProto.name);
  59. }
  60. }
  61. private static void NewSetValue(ref Node node, FieldInfo field, ValueBase valueBase, string nodeName)
  62. {
  63. field.SetValue(node, valueBase.GetValueByType(field.FieldType));
  64. }
  65. private Node CreateOneNode(NodeProto proto)
  66. {
  67. if (!this.dictionary.ContainsKey(proto.name))
  68. {
  69. throw new KeyNotFoundException($"NodeType没有定义该节点: {proto.name}");
  70. }
  71. return this.dictionary[proto.name](proto);
  72. }
  73. private Node CreateTreeNode(NodeProto proto)
  74. {
  75. Node node = this.CreateOneNode(proto);
  76. node.EndInit(this.GetOwner<Scene>());
  77. if (proto.Children == null)
  78. {
  79. return node;
  80. }
  81. foreach (NodeProto nodeProto in proto.Children)
  82. {
  83. Node childNode = this.CreateTreeNode(nodeProto);
  84. node.AddChild(childNode);
  85. }
  86. return node;
  87. }
  88. public BehaviorTree CreateTree(Scene scene, GameObject treeGo)
  89. {
  90. try
  91. {
  92. BehaviorTree tree;
  93. if (this.treeCache.TryGetValue(treeGo, out tree))
  94. {
  95. return tree;
  96. }
  97. BehaviorTreeConfig behaviorTreeConfig = treeGo.GetComponent<BehaviorTreeConfig>();
  98. Node node = this.CreateTreeNode(behaviorTreeConfig.RootNodeProto);
  99. tree = new BehaviorTree(scene, node);
  100. tree.behaviorTreeConfig = behaviorTreeConfig;
  101. if (Define.LoadResourceType == LoadResourceType.Async)
  102. {
  103. this.treeCache.Add(treeGo, tree);
  104. }
  105. return tree;
  106. }
  107. catch (Exception e)
  108. {
  109. throw new ConfigException($"行为树配置错误: {treeGo.name}", e);
  110. }
  111. }
  112. }
  113. }