ソースを参照

行为树修改成每行配置一棵树

tanghai 10 年 前
コミット
165019f99e

+ 2 - 2
CSharp/Game/Controller/BehaviorTreeNode/Not.cs

@@ -9,9 +9,9 @@ namespace Controller
 		{
 		}
 
-		public override bool Run(BlackBoard blackBoard)
+		public override bool Run(Env env)
 		{
-			return !this.children[0].Run(blackBoard);
+			return !this.children[0].Run(env);
 		}
 	}
 }

+ 2 - 2
CSharp/Game/Controller/BehaviorTreeNode/Selector.cs

@@ -9,11 +9,11 @@ namespace Controller
 		{
 		}
 
-		public override bool Run(BlackBoard blackBoard)
+		public override bool Run(Env env)
 		{
 			foreach (var child in this.children)
 			{
-				if (child.Run(blackBoard))
+				if (child.Run(env))
 				{
 					return true;
 				}

+ 2 - 2
CSharp/Game/Controller/BehaviorTreeNode/Sequence.cs

@@ -9,11 +9,11 @@ namespace Controller
 		{
 		}
 
-		public override bool Run(BlackBoard blackBoard)
+		public override bool Run(Env env)
 		{
 			foreach (var child in this.children)
 			{
-				if (!child.Run(blackBoard))
+				if (!child.Run(env))
 				{
 					return false;
 				}

+ 8 - 8
CSharp/Game/Controller/ConfigCategory/NodeCategory.cs → CSharp/Game/Controller/ConfigCategory/TreeCategory.cs

@@ -1,9 +1,9 @@
-using Model;
-
-namespace Controller
-{
-	[Config(ServerType.All)]
-	public class NodeCategory: ACategory<NodeConfig>
-	{
-	}
+using Model;
+
+namespace Controller
+{
+	[Config(ServerType.All)]
+	public class TreeCategory: ACategory<TreeConfig>
+	{
+	}
 }

+ 1 - 1
CSharp/Game/Controller/Controller.csproj

@@ -39,6 +39,7 @@
   </ItemGroup>
   <ItemGroup>
     <Compile Include="AddressHelper.cs" />
+    <Compile Include="ConfigCategory\TreeCategory.cs" />
     <Compile Include="Event\SleepTimeoutEvent.cs" />
     <Compile Include="Event\BuffTimeoutEvent.cs" />
     <Compile Include="BehaviorTreeNode\Not.cs" />
@@ -46,7 +47,6 @@
     <Compile Include="BehaviorTreeNode\Sequence.cs" />
     <Compile Include="ConfigCategory\BuffCategory.cs" />
     <Compile Include="ConfigCategory\GlobalCategory.cs" />
-    <Compile Include="ConfigCategory\NodeCategory.cs" />
     <Compile Include="ConfigCategory\ServerInfoCategory.cs" />
     <Compile Include="ConfigCategory\UnitCategory.cs" />
     <Compile Include="Factory\UnitPlayerFactory.cs" />

+ 3 - 3
CSharp/Game/Model/BehaviorTree/BehaviorTree.cs

@@ -3,15 +3,15 @@
 	public class BehaviorTree
 	{
 		private readonly Node node;
-
+		
 		public BehaviorTree(Node node)
 		{
 			this.node = node;
 		}
 
-		public bool Run(BlackBoard blackBoard)
+		public bool Run(Env env)
 		{
-			return this.node.Run(blackBoard);
+			return this.node.Run(env);
 		}
 	}
 }

+ 0 - 76
CSharp/Game/Model/BehaviorTree/BehaviorTreeFactory.cs

@@ -1,76 +0,0 @@
-using System;
-using System.Collections.Generic;
-using System.Reflection;
-
-namespace Model
-{
-	public class BehaviorTreeFactory
-	{
-		private static readonly BehaviorTreeFactory instance = new BehaviorTreeFactory();
-
-		public static BehaviorTreeFactory Instance
-		{
-			get
-			{
-				return instance;
-			}
-		}
-
-		private Dictionary<NodeType, Func<NodeConfig, Node>> dictionary;
-
-		private BehaviorTreeFactory()
-		{
-		}
-
-		public void Load(Assembly assembly)
-		{
-			this.dictionary = new Dictionary<NodeType, Func<NodeConfig, Node>>();
-
-			Type[] types = assembly.GetTypes();
-			foreach (var type in types)
-			{
-				object[] attrs = type.GetCustomAttributes(typeof (NodeAttribute), false);
-				if (attrs.Length == 0)
-				{
-					continue;
-				}
-				NodeAttribute attribute = (NodeAttribute) attrs[0];
-
-				Type classType = type;
-				this.dictionary.Add(attribute.Type,
-						config => (Node) Activator.CreateInstance(classType, new object[] { config }));
-			}
-		}
-
-		private Node CreateNode(NodeConfig config)
-		{
-			if (!this.dictionary.ContainsKey((NodeType)config.Id))
-			{
-				throw new KeyNotFoundException(string.Format("CreateNode cannot found: {0}", config.Id));
-			}
-			return this.dictionary[(NodeType)config.Id](config);
-		}
-
-		private Node CreateTreeNode(NodeConfig config)
-		{
-			var node = this.CreateNode(config);
-			if (config.SubConfigs == null)
-			{
-				return node;
-			}
-
-			foreach (var subConfig in config.SubConfigs)
-			{
-				var subNode = this.CreateTreeNode(subConfig);
-				node.AddChild(subNode);
-			}
-			return node;
-		}
-
-		public BehaviorTree CreateTree(NodeConfig config)
-		{
-			var node = this.CreateTreeNode(config);
-			return new BehaviorTree(node);
-		}
-	}
-}

+ 0 - 8
CSharp/Game/Model/BehaviorTree/BlackBoard.cs

@@ -1,8 +0,0 @@
-using Common.Base;
-
-namespace Model
-{
-	public class BlackBoard: Object
-	{
-	}
-}

+ 36 - 3
CSharp/Game/Model/BehaviorTree/Node.cs

@@ -4,13 +4,43 @@ namespace Model
 {
 	public abstract class Node
 	{
-		public NodeConfig Config { get; private set; }
+		private readonly NodeConfig config;
 
 		protected readonly List<Node> children = new List<Node>();
 
 		protected Node(NodeConfig config)
 		{
-			this.Config = config;
+			this.config = config;
+		}
+
+		/// <summary>
+		/// 策划配置的id
+		/// </summary>
+		public int Id
+		{
+			get
+			{
+				return this.config.Id;
+			}
+		}
+
+		/// <summary>
+		/// 节点的类型例如: NodeType.Not
+		/// </summary>
+		public NodeType Type
+		{
+			get
+			{
+				return this.config.Type;
+			}
+		}
+
+		public List<string> Args
+		{
+			get
+			{
+				return this.config.Args;
+			}
 		}
 
 		public void AddChild(Node child)
@@ -18,6 +48,9 @@ namespace Model
 			this.children.Add(child);
 		}
 
-		public abstract bool Run(BlackBoard blackBoard);
+		public virtual bool Run(Env env)
+		{
+			return true;
+		}
 	}
 }

+ 2 - 2
CSharp/Game/Model/BehaviorTree/NodeAttribute.cs

@@ -7,9 +7,9 @@ namespace Model
 	{
 		public NodeType Type { get; private set; }
 
-		public NodeAttribute(NodeType nodeType)
+		public NodeAttribute(NodeType type)
 		{
-			this.Type = nodeType;
+			this.Type = type;
 		}
 	}
 }

+ 0 - 0
CSharp/Game/Model/NodeType.cs → CSharp/Game/Model/BehaviorTree/NodeType.cs


+ 70 - 10
CSharp/Game/Model/Component/BehaviorTreeComponent.cs

@@ -1,23 +1,46 @@
-using System.Collections.Generic;
+using System;
+using System.Collections.Generic;
 using System.Reflection;
 using Common.Base;
 
 namespace Model
 {
-	public class BehaviorTreeComponent: Component<World>, IAssemblyLoader
+	public class BehaviorTreeComponent : Component<World>, IAssemblyLoader, IStart
 	{
-		private readonly Dictionary<int, BehaviorTree> trees = new Dictionary<int, BehaviorTree>();
+		private Dictionary<int, BehaviorTree> behaviorTrees;
+
+		private Dictionary<NodeType, Func<NodeConfig, Node>> dictionary =
+				new Dictionary<NodeType, Func<NodeConfig, Node>>();
 
 		public void Load(Assembly assembly)
 		{
-			BehaviorTreeFactory behaviorTreeFactory = BehaviorTreeFactory.Instance;
-			behaviorTreeFactory.Load(assembly);
+			this.behaviorTrees = new Dictionary<int, BehaviorTree>();
+			dictionary = new Dictionary<NodeType, Func<NodeConfig, Node>>();
+			Type[] types = assembly.GetTypes();
+			foreach (Type type in types)
+			{
+				object[] attrs = type.GetCustomAttributes(typeof(NodeAttribute), false);
+				if (attrs.Length == 0)
+				{
+					continue;
+				}
+
+				NodeAttribute attribute = attrs[0] as NodeAttribute;
+				Type classType = type;
+				if (this.dictionary.ContainsKey(attribute.Type))
+				{
+					throw new GameException(string.Format("已经存在同类节点: {0}", attribute.Type));
+				}
+				this.dictionary.Add(attribute.Type, config => (Node)Activator.CreateInstance(classType, config));
+			}
+		}
 
-			NodeConfig[] nodeConfigs = World.Instance.GetComponent<ConfigComponent>().GetAll<NodeConfig>();
-			foreach (NodeConfig nodeConfig in nodeConfigs)
+		public void Start()
+		{
+			TreeConfig[] configs = World.Instance.GetComponent<ConfigComponent>().GetAll<TreeConfig>();
+			foreach (TreeConfig proto in configs)
 			{
-				BehaviorTree behaviorTree = behaviorTreeFactory.CreateTree(nodeConfig);
-				this.trees[nodeConfig.Id] = behaviorTree;
+				behaviorTrees[proto.Id] = CreateTree(proto);
 			}
 		}
 
@@ -25,8 +48,45 @@ namespace Model
 		{
 			get
 			{
-				return this.trees[id];
+				BehaviorTree behaviorTree;
+				if (!this.behaviorTrees.TryGetValue(id, out behaviorTree))
+				{
+					throw new GameException(string.Format("无法找到行为树: {0}", id));
+				}
+				return behaviorTree;
+			}
+		}
+
+		private Node CreateOneNode(NodeConfig proto)
+		{
+			NodeType nodeType = (NodeType)proto.Type;
+			if (!this.dictionary.ContainsKey(nodeType))
+			{
+				throw new KeyNotFoundException(string.Format("NodeType没有定义该节点: {0}", nodeType));
 			}
+			return this.dictionary[nodeType](proto);
+		}
+
+		private Node CreateTreeNode(NodeConfig proto)
+		{
+			Node node = this.CreateOneNode(proto);
+			if (proto.Children == null)
+			{
+				return node;
+			}
+
+			foreach (NodeConfig nodeProto in proto.Children)
+			{
+				Node childNode = this.CreateTreeNode(nodeProto);
+				node.AddChild(childNode);
+			}
+			return node;
+		}
+
+		private BehaviorTree CreateTree(TreeConfig treeConfig)
+		{
+			Node node = this.CreateTreeNode(treeConfig.Root);
+			return new BehaviorTree(node);
 		}
 	}
 }

+ 1 - 0
CSharp/Game/Model/Component/BuffComponent.cs

@@ -3,6 +3,7 @@ using System.Collections.Generic;
 using Common.Base;
 using MongoDB.Bson;
 using MongoDB.Bson.Serialization.Attributes;
+#pragma warning disable 4014
 
 namespace Model
 {

+ 2 - 0
CSharp/Game/Model/Component/TimerComponent.cs

@@ -79,7 +79,9 @@ namespace Model
 						continue;
 					}
 					this.timers.Remove(id);
+#pragma warning disable 4014
 					World.Instance.GetComponent<EventComponent<EventAttribute>>().RunAsync(timer.CallbackEvent, timer.Env);
+#pragma warning restore 4014
 				}
 			}
 		}

+ 0 - 10
CSharp/Game/Model/Config/NodeConfig.cs

@@ -1,10 +0,0 @@
-using System.Collections.Generic;
-
-namespace Model
-{
-	public class NodeConfig: AConfig
-	{
-		public List<string> Args { get; set; }
-		public List<NodeConfig> SubConfigs { get; set; }
-	}
-}

+ 32 - 0
CSharp/Game/Model/Config/TreeConfig.cs

@@ -0,0 +1,32 @@
+using System.Collections.Generic;
+using MongoDB.Bson.Serialization.Attributes;
+
+namespace Model
+{
+	public class NodeConfig
+	{
+		public int Id { get; set; }
+		public NodeType Type { get; set; }
+		public List<string> Args { get; set; }
+		public List<NodeConfig> Children { get; set; }
+	}
+
+	public class TreeConfig : AConfig
+	{
+		[BsonElement, BsonIgnoreIfNull]
+		private NodeConfig root;
+
+		[BsonIgnore]
+		public NodeConfig Root
+		{
+			get
+			{
+				return this.root;
+			}
+			set
+			{
+				this.root = value;
+			}
+		}
+	}
+}

+ 1 - 0
CSharp/Game/Model/IEvent.cs

@@ -1,4 +1,5 @@
 using System.Threading.Tasks;
+#pragma warning disable 1998
 
 namespace Model
 {

+ 2 - 4
CSharp/Game/Model/Model.csproj

@@ -49,8 +49,6 @@
     <Compile Include="AConfig.cs" />
     <Compile Include="AEventAttribute.cs" />
     <Compile Include="BehaviorTree\BehaviorTree.cs" />
-    <Compile Include="BehaviorTree\BehaviorTreeFactory.cs" />
-    <Compile Include="BehaviorTree\BlackBoard.cs" />
     <Compile Include="BehaviorTree\Node.cs" />
     <Compile Include="BehaviorTree\NodeAttribute.cs" />
     <Compile Include="Buff.cs" />
@@ -71,8 +69,8 @@
     <Compile Include="ConfigAttribute.cs" />
     <Compile Include="Config\BuffConfig.cs" />
     <Compile Include="Config\GlobalConfig.cs" />
-    <Compile Include="Config\NodeConfig.cs" />
     <Compile Include="Config\ServerInfoConfig.cs" />
+    <Compile Include="Config\TreeConfig.cs" />
     <Compile Include="Config\UnitConfig.cs" />
     <Compile Include="Env.cs" />
     <Compile Include="EnvKey.cs" />
@@ -89,7 +87,7 @@
     <Compile Include="IStart.cs" />
     <Compile Include="IUpdate.cs" />
     <Compile Include="MessageAttribute.cs" />
-    <Compile Include="NodeType.cs" />
+    <Compile Include="BehaviorTree\NodeType.cs" />
     <Compile Include="Opcode.cs" />
     <Compile Include="NumDefine.cs" />
     <Compile Include="OpcodeHelper.cs" />