Эх сурвалжийг харах

动态加载Controller Assembly,以后可以方便的Reload

tanghai 11 жил өмнө
parent
commit
a594cc505d
30 өөрчлөгдсөн 325 нэмэгдсэн , 274 устгасан
  1. 17 15
      CSharp/CSharp.sln
  2. 0 82
      CSharp/Game/BehaviorTree/BehaviorTree.csproj
  3. 0 9
      CSharp/Game/BehaviorTree/NodeType.cs
  4. 1 1
      CSharp/Game/Controller/BehaviorTreeNode/Not.cs
  5. 1 1
      CSharp/Game/Controller/BehaviorTreeNode/Selector.cs
  6. 1 1
      CSharp/Game/Controller/BehaviorTreeNode/Sequence.cs
  7. 26 0
      CSharp/Game/Controller/Controller.csproj
  8. 15 0
      CSharp/Game/Controller/Factories/UnitFactory.cs
  9. 9 0
      CSharp/Game/Controller/NodeType.cs
  10. 9 0
      CSharp/Game/Controller/UnitType.cs
  11. 1 1
      CSharp/Game/Controller/packages.config
  12. 1 1
      CSharp/Game/Model/BehaviorTree/BehaviorTree.cs
  13. 8 11
      CSharp/Game/Model/BehaviorTree/BehaviorTreeFactory.cs
  14. 1 1
      CSharp/Game/Model/BehaviorTree/BlackBoard.cs
  15. 1 2
      CSharp/Game/Model/BehaviorTree/Node.cs
  16. 3 3
      CSharp/Game/Model/BehaviorTree/NodeAttribute.cs
  17. 24 0
      CSharp/Game/Model/Component/BehaviorTreeComponent.cs
  18. 18 18
      CSharp/Game/Model/Component/BuffComponent.cs
  19. 17 28
      CSharp/Game/Model/Component/ConfigComponent.cs
  20. 49 0
      CSharp/Game/Model/Component/FactoryComponent.cs
  21. 3 3
      CSharp/Game/Model/Component/UnitComponent.cs
  22. 1 1
      CSharp/Game/Model/Config/UnitConfig.cs
  23. 0 52
      CSharp/Game/Model/FactoryComponent.cs
  24. 10 6
      CSharp/Game/Model/Model.csproj
  25. 0 21
      CSharp/Game/Model/UnitFactory.cs
  26. 0 9
      CSharp/Game/Model/UnitType.cs
  27. 0 7
      CSharp/Game/Model/World.cs
  28. 21 1
      CSharp/Game/MongoDBTest/MongoDBTest.cs
  29. 52 0
      CSharp/Server/Login/Login.csproj
  30. 36 0
      CSharp/Server/Login/Properties/AssemblyInfo.cs

+ 17 - 15
CSharp/CSharp.sln

@@ -30,8 +30,6 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ENetTest", "Platform\ENetTe
 EndProject
 Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Game", "Game", "{D0CC1FF4-2747-4278-A51F-BE9AA959175B}"
 EndProject
-Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "BehaviorTree", "Game\BehaviorTree\BehaviorTree.csproj", "{C4E7A34A-095C-4983-AB63-FC2D20CD6824}"
-EndProject
 Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ENetExe", "Platform\ENetExe\ENetExe.csproj", "{CBA52DC8-1C80-4A79-9AC5-73514EBBD749}"
 EndProject
 Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{2814EB55-E1C2-4751-AC14-94116DA9D22B}"
@@ -50,6 +48,10 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "BehaviorTreeModule", "App\M
 EndProject
 Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MongoDBTest", "Game\MongoDBTest\MongoDBTest.csproj", "{93FB20CF-827A-4CED-A273-90117EB7192F}"
 EndProject
+Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Server", "Server", "{AAD111B8-4746-4445-A428-97A72A04FCCD}"
+EndProject
+Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Login", "Server\Login\Login.csproj", "{2B3EC6BE-0920-4C93-934F-41DC76108818}"
+EndProject
 Global
 	GlobalSection(SolutionConfigurationPlatforms) = preSolution
 		Debug|Any CPU = Debug|Any CPU
@@ -126,18 +128,6 @@ Global
 		{901A8E5C-C4C6-4C3C-8E18-068D75119F5D}.Release|Mixed Platforms.Build.0 = Release|Any CPU
 		{901A8E5C-C4C6-4C3C-8E18-068D75119F5D}.Release|Win32.ActiveCfg = Release|Any CPU
 		{901A8E5C-C4C6-4C3C-8E18-068D75119F5D}.Release|x86.ActiveCfg = Release|Any CPU
-		{C4E7A34A-095C-4983-AB63-FC2D20CD6824}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
-		{C4E7A34A-095C-4983-AB63-FC2D20CD6824}.Debug|Any CPU.Build.0 = Debug|Any CPU
-		{C4E7A34A-095C-4983-AB63-FC2D20CD6824}.Debug|Mixed Platforms.ActiveCfg = Debug|Any CPU
-		{C4E7A34A-095C-4983-AB63-FC2D20CD6824}.Debug|Mixed Platforms.Build.0 = Debug|Any CPU
-		{C4E7A34A-095C-4983-AB63-FC2D20CD6824}.Debug|Win32.ActiveCfg = Debug|Any CPU
-		{C4E7A34A-095C-4983-AB63-FC2D20CD6824}.Debug|x86.ActiveCfg = Debug|Any CPU
-		{C4E7A34A-095C-4983-AB63-FC2D20CD6824}.Release|Any CPU.ActiveCfg = Release|Any CPU
-		{C4E7A34A-095C-4983-AB63-FC2D20CD6824}.Release|Any CPU.Build.0 = Release|Any CPU
-		{C4E7A34A-095C-4983-AB63-FC2D20CD6824}.Release|Mixed Platforms.ActiveCfg = Release|Any CPU
-		{C4E7A34A-095C-4983-AB63-FC2D20CD6824}.Release|Mixed Platforms.Build.0 = Release|Any CPU
-		{C4E7A34A-095C-4983-AB63-FC2D20CD6824}.Release|Win32.ActiveCfg = Release|Any CPU
-		{C4E7A34A-095C-4983-AB63-FC2D20CD6824}.Release|x86.ActiveCfg = Release|Any CPU
 		{CBA52DC8-1C80-4A79-9AC5-73514EBBD749}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
 		{CBA52DC8-1C80-4A79-9AC5-73514EBBD749}.Debug|Any CPU.Build.0 = Debug|Any CPU
 		{CBA52DC8-1C80-4A79-9AC5-73514EBBD749}.Debug|Mixed Platforms.ActiveCfg = Debug|Any CPU
@@ -234,6 +224,18 @@ Global
 		{93FB20CF-827A-4CED-A273-90117EB7192F}.Release|Mixed Platforms.Build.0 = Release|Any CPU
 		{93FB20CF-827A-4CED-A273-90117EB7192F}.Release|Win32.ActiveCfg = Release|Any CPU
 		{93FB20CF-827A-4CED-A273-90117EB7192F}.Release|x86.ActiveCfg = Release|Any CPU
+		{2B3EC6BE-0920-4C93-934F-41DC76108818}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
+		{2B3EC6BE-0920-4C93-934F-41DC76108818}.Debug|Any CPU.Build.0 = Debug|Any CPU
+		{2B3EC6BE-0920-4C93-934F-41DC76108818}.Debug|Mixed Platforms.ActiveCfg = Debug|Any CPU
+		{2B3EC6BE-0920-4C93-934F-41DC76108818}.Debug|Mixed Platforms.Build.0 = Debug|Any CPU
+		{2B3EC6BE-0920-4C93-934F-41DC76108818}.Debug|Win32.ActiveCfg = Debug|Any CPU
+		{2B3EC6BE-0920-4C93-934F-41DC76108818}.Debug|x86.ActiveCfg = Debug|Any CPU
+		{2B3EC6BE-0920-4C93-934F-41DC76108818}.Release|Any CPU.ActiveCfg = Release|Any CPU
+		{2B3EC6BE-0920-4C93-934F-41DC76108818}.Release|Any CPU.Build.0 = Release|Any CPU
+		{2B3EC6BE-0920-4C93-934F-41DC76108818}.Release|Mixed Platforms.ActiveCfg = Release|Any CPU
+		{2B3EC6BE-0920-4C93-934F-41DC76108818}.Release|Mixed Platforms.Build.0 = Release|Any CPU
+		{2B3EC6BE-0920-4C93-934F-41DC76108818}.Release|Win32.ActiveCfg = Release|Any CPU
+		{2B3EC6BE-0920-4C93-934F-41DC76108818}.Release|x86.ActiveCfg = Release|Any CPU
 	EndGlobalSection
 	GlobalSection(SolutionProperties) = preSolution
 		HideSolutionNode = FALSE
@@ -245,7 +247,6 @@ Global
 		{C9992B7C-313E-4C9F-A954-640D01EDFB58} = {ADBF5F67-B480-4A93-9D50-C81856FC61A9}
 		{D0B4CFAC-A368-4742-9863-68776CFA9938} = {ADBF5F67-B480-4A93-9D50-C81856FC61A9}
 		{901A8E5C-C4C6-4C3C-8E18-068D75119F5D} = {ADBF5F67-B480-4A93-9D50-C81856FC61A9}
-		{C4E7A34A-095C-4983-AB63-FC2D20CD6824} = {D0CC1FF4-2747-4278-A51F-BE9AA959175B}
 		{CBA52DC8-1C80-4A79-9AC5-73514EBBD749} = {ADBF5F67-B480-4A93-9D50-C81856FC61A9}
 		{B42D431A-3A54-4649-942A-C5356D7F9FBC} = {ADBF5F67-B480-4A93-9D50-C81856FC61A9}
 		{F176D1FA-63E5-4B89-9A03-D44CCCCC069A} = {ADBF5F67-B480-4A93-9D50-C81856FC61A9}
@@ -254,5 +255,6 @@ Global
 		{E69C1F22-E004-4D69-8667-787BD243454B} = {D0CC1FF4-2747-4278-A51F-BE9AA959175B}
 		{6CD185D1-08E0-4729-A999-2D5B57BA8193} = {C4C64188-4FAE-4CC3-A9E6-D9D4AF7429B6}
 		{93FB20CF-827A-4CED-A273-90117EB7192F} = {D0CC1FF4-2747-4278-A51F-BE9AA959175B}
+		{2B3EC6BE-0920-4C93-934F-41DC76108818} = {AAD111B8-4746-4445-A428-97A72A04FCCD}
 	EndGlobalSection
 EndGlobal

+ 0 - 82
CSharp/Game/BehaviorTree/BehaviorTree.csproj

@@ -1,82 +0,0 @@
-<?xml version="1.0" encoding="utf-8"?>
-<Project ToolsVersion="12.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
-  <Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
-  <PropertyGroup>
-    <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
-    <Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
-    <ProjectGuid>{C4E7A34A-095C-4983-AB63-FC2D20CD6824}</ProjectGuid>
-    <OutputType>Library</OutputType>
-    <AppDesignerFolder>Properties</AppDesignerFolder>
-    <RootNamespace>BehaviorTree</RootNamespace>
-    <AssemblyName>BehaviorTree</AssemblyName>
-    <TargetFrameworkVersion>v4.5.1</TargetFrameworkVersion>
-    <FileAlignment>512</FileAlignment>
-    <SolutionDir Condition="$(SolutionDir) == '' Or $(SolutionDir) == '*Undefined*'">..\..\</SolutionDir>
-    <RestorePackages>true</RestorePackages>
-    <TargetFrameworkProfile />
-  </PropertyGroup>
-  <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
-    <DebugSymbols>true</DebugSymbols>
-    <DebugType>full</DebugType>
-    <Optimize>false</Optimize>
-    <OutputPath>..\..\Bin\Debug\</OutputPath>
-    <DefineConstants>DEBUG;TRACE</DefineConstants>
-    <ErrorReport>prompt</ErrorReport>
-    <WarningLevel>4</WarningLevel>
-  </PropertyGroup>
-  <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
-    <DebugType>pdbonly</DebugType>
-    <Optimize>true</Optimize>
-    <OutputPath>..\..\Bin\Release\</OutputPath>
-    <DefineConstants>TRACE</DefineConstants>
-    <ErrorReport>prompt</ErrorReport>
-    <WarningLevel>4</WarningLevel>
-  </PropertyGroup>
-  <ItemGroup>
-    <Reference Include="MongoDB.Bson, Version=1.9.2.235, Culture=neutral, PublicKeyToken=f686731cfb9cc103, processorArchitecture=MSIL">
-      <SpecificVersion>False</SpecificVersion>
-      <HintPath>..\..\packages\mongocsharpdriver.1.9.2\lib\net35\MongoDB.Bson.dll</HintPath>
-    </Reference>
-    <Reference Include="MongoDB.Driver">
-      <HintPath>..\..\packages\mongocsharpdriver.1.9.2\lib\net35\MongoDB.Driver.dll</HintPath>
-    </Reference>
-    <Reference Include="System" />
-    <Reference Include="System.Core" />
-  </ItemGroup>
-  <ItemGroup>
-    <Compile Include="BehaviorTree.cs" />
-    <Compile Include="BlackBoard.cs" />
-    <Compile Include="Node.cs" />
-    <Compile Include="BehaviorTreeFactory.cs" />
-    <Compile Include="NodeAttribute.cs" />
-    <Compile Include="NodeType.cs" />
-    <Compile Include="Not.cs" />
-    <Compile Include="Selector.cs" />
-    <Compile Include="Sequence.cs" />
-  </ItemGroup>
-  <ItemGroup>
-    <ProjectReference Include="..\..\Platform\Common\Common.csproj">
-      <Project>{19f8f043-1f99-4550-99df-dea5c7d77e55}</Project>
-      <Name>Common</Name>
-    </ProjectReference>
-    <ProjectReference Include="..\Model\Model.csproj">
-      <Project>{0FA529D1-D0A9-4A8E-90F5-117CE80F2EDE}</Project>
-      <Name>Model</Name>
-    </ProjectReference>
-  </ItemGroup>
-  <ItemGroup>
-    <None Include="packages.config" />
-  </ItemGroup>
-  <ItemGroup>
-    <Folder Include="Properties\" />
-  </ItemGroup>
-  <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
-  <Import Project="$(SolutionDir)\.nuget\NuGet.targets" Condition="Exists('$(SolutionDir)\.nuget\NuGet.targets')" />
-  <!-- To modify your build process, add your task inside one of the targets below and uncomment it. 
-       Other similar extension points exist, see Microsoft.Common.targets.
-  <Target Name="BeforeBuild">
-  </Target>
-  <Target Name="AfterBuild">
-  </Target>
-  -->
-</Project>

+ 0 - 9
CSharp/Game/BehaviorTree/NodeType.cs

@@ -1,9 +0,0 @@
-namespace BehaviorTree
-{
-    public enum NodeType
-    {
-        Selector = 1,
-        Sequence = 2,
-        Not = 10,
-    }
-}

+ 1 - 1
CSharp/Game/BehaviorTree/Not.cs → CSharp/Game/Controller/BehaviorTreeNode/Not.cs

@@ -1,6 +1,6 @@
 using Model;
 
-namespace BehaviorTree
+namespace Controller
 {
     [Node(NodeType.Not)]
     public class Not: Node

+ 1 - 1
CSharp/Game/BehaviorTree/Selector.cs → CSharp/Game/Controller/BehaviorTreeNode/Selector.cs

@@ -1,6 +1,6 @@
 using Model;
 
-namespace BehaviorTree
+namespace Controller
 {
     [Node(NodeType.Selector)]
     public class Selector: Node

+ 1 - 1
CSharp/Game/BehaviorTree/Sequence.cs → CSharp/Game/Controller/BehaviorTreeNode/Sequence.cs

@@ -1,6 +1,6 @@
 using Model;
 
-namespace BehaviorTree
+namespace Controller
 {
     [Node(NodeType.Sequence)]
     internal class Sequence: Node

+ 26 - 0
CSharp/Game/Controller/Controller.csproj

@@ -12,6 +12,8 @@
     <TargetFrameworkVersion>v4.5.1</TargetFrameworkVersion>
     <FileAlignment>512</FileAlignment>
     <TargetFrameworkProfile />
+    <SolutionDir Condition="$(SolutionDir) == '' Or $(SolutionDir) == '*Undefined*'">..\..\</SolutionDir>
+    <RestorePackages>true</RestorePackages>
   </PropertyGroup>
   <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
     <DebugSymbols>true</DebugSymbols>
@@ -31,11 +33,25 @@
     <WarningLevel>4</WarningLevel>
   </PropertyGroup>
   <ItemGroup>
+    <Reference Include="MongoDB.Bson, Version=1.9.2.235, Culture=neutral, PublicKeyToken=f686731cfb9cc103, processorArchitecture=MSIL">
+      <SpecificVersion>False</SpecificVersion>
+      <HintPath>..\..\packages\mongocsharpdriver.1.9.2\lib\net35\MongoDB.Bson.dll</HintPath>
+    </Reference>
+    <Reference Include="MongoDB.Driver, Version=1.9.2.235, Culture=neutral, PublicKeyToken=f686731cfb9cc103, processorArchitecture=MSIL">
+      <SpecificVersion>False</SpecificVersion>
+      <HintPath>..\..\packages\mongocsharpdriver.1.9.2\lib\net35\MongoDB.Driver.dll</HintPath>
+    </Reference>
     <Reference Include="System" />
     <Reference Include="System.Core" />
   </ItemGroup>
   <ItemGroup>
+    <Compile Include="BehaviorTreeNode\Not.cs" />
+    <Compile Include="BehaviorTreeNode\Selector.cs" />
+    <Compile Include="BehaviorTreeNode\Sequence.cs" />
     <Compile Include="BuffController.cs" />
+    <Compile Include="NodeType.cs" />
+    <Compile Include="Factories\UnitFactory.cs" />
+    <Compile Include="UnitType.cs" />
   </ItemGroup>
   <ItemGroup>
     <Folder Include="Properties\" />
@@ -50,7 +66,17 @@
       <Name>Model</Name>
     </ProjectReference>
   </ItemGroup>
+  <ItemGroup>
+    <None Include="packages.config" />
+  </ItemGroup>
   <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
+  <Import Project="$(SolutionDir)\.nuget\NuGet.targets" Condition="Exists('$(SolutionDir)\.nuget\NuGet.targets')" />
+  <Target Name="EnsureNuGetPackageBuildImports" BeforeTargets="PrepareForBuild">
+    <PropertyGroup>
+      <ErrorText>这台计算机上缺少此项目引用的 NuGet 程序包。启用“NuGet 程序包还原”可下载这些程序包。有关详细信息,请参阅 http://go.microsoft.com/fwlink/?LinkID=322105。缺少的文件是 {0}。</ErrorText>
+    </PropertyGroup>
+    <Error Condition="!Exists('$(SolutionDir)\.nuget\NuGet.targets')" Text="$([System.String]::Format('$(ErrorText)', '$(SolutionDir)\.nuget\NuGet.targets'))" />
+  </Target>
   <!-- To modify your build process, add your task inside one of the targets below and uncomment it. 
        Other similar extension points exist, see Microsoft.Common.targets.
   <Target Name="BeforeBuild">

+ 15 - 0
CSharp/Game/Controller/Factories/UnitFactory.cs

@@ -0,0 +1,15 @@
+using Model;
+
+namespace Controller
+{
+    [Factory(typeof (Unit), UnitType.Player)]
+    public class UnitPlayerFactory: IFactory<Unit>
+    {
+        public Unit Create(int configId)
+        {
+            Unit player = new Unit(configId);
+            player.AddComponent<BuffComponent>();
+            return player;
+        }
+    }
+}

+ 9 - 0
CSharp/Game/Controller/NodeType.cs

@@ -0,0 +1,9 @@
+namespace Controller
+{
+    public static class NodeType
+    {
+        public const int Selector = 1;
+        public const int Sequence = 2;
+        public const int Not = 10;
+    }
+}

+ 9 - 0
CSharp/Game/Controller/UnitType.cs

@@ -0,0 +1,9 @@
+namespace Controller
+{
+    public static class UnitType
+    {
+        public const int Player = 1;
+        public const int Npc = 2;
+        public const int Dog = 3;
+    }
+}

+ 1 - 1
CSharp/Game/BehaviorTree/Packages.config → CSharp/Game/Controller/packages.config

@@ -1,4 +1,4 @@
 <?xml version="1.0" encoding="utf-8"?>
 <packages>
-  <package id="mongocsharpdriver" version="1.9.2" targetFramework="net45" />
+  <package id="mongocsharpdriver" version="1.9.2" targetFramework="net451" />
 </packages>

+ 1 - 1
CSharp/Game/BehaviorTree/BehaviorTree.cs → CSharp/Game/Model/BehaviorTree/BehaviorTree.cs

@@ -1,4 +1,4 @@
-namespace BehaviorTree
+namespace Model
 {
     public class BehaviorTree
     {

+ 8 - 11
CSharp/Game/BehaviorTree/BehaviorTreeFactory.cs → CSharp/Game/Model/BehaviorTree/BehaviorTreeFactory.cs

@@ -1,9 +1,8 @@
 using System;
 using System.Collections.Generic;
 using System.Reflection;
-using Model;
 
-namespace BehaviorTree
+namespace Model
 {
     public class BehaviorTreeFactory
     {
@@ -17,17 +16,16 @@ namespace BehaviorTree
             }
         }
 
-        private readonly Dictionary<NodeType, Func<NodeConfig, Node>> dictionary =
-                new Dictionary<NodeType, Func<NodeConfig, Node>>();
+        private Dictionary<int, Func<NodeConfig, Node>> dictionary;
 
         private BehaviorTreeFactory()
         {
-            Assembly assembly = typeof(BehaviorTreeFactory).Assembly;
-            this.RegisterNodes(assembly);
         }
 
-        public void RegisterNodes(Assembly assembly)
+        public void Load(Assembly assembly)
         {
+            this.dictionary = new Dictionary<int, Func<NodeConfig, Node>>();
+
             Type[] types = assembly.GetTypes();
             foreach (var type in types)
             {
@@ -40,19 +38,18 @@ namespace BehaviorTree
 
                 Type classType = type;
                 this.dictionary.Add(attribute.NodeType,
-                        config =>
-                                (Node)Activator.CreateInstance(classType, new object[] { config }));
+                        config => (Node)Activator.CreateInstance(classType, new object[] { config }));
             } 
         }
 
         private Node CreateNode(NodeConfig config)
         {
-            if (!this.dictionary.ContainsKey((NodeType) config.Id))
+            if (!this.dictionary.ContainsKey(config.Id))
             {
                 throw new KeyNotFoundException(string.Format("CreateNode cannot found: {0}",
                         config.Id));
             }
-            return this.dictionary[(NodeType) config.Id](config);
+            return this.dictionary[config.Id](config);
         }
 
         private Node CreateTreeNode(NodeConfig config)

+ 1 - 1
CSharp/Game/BehaviorTree/BlackBoard.cs → CSharp/Game/Model/BehaviorTree/BlackBoard.cs

@@ -1,6 +1,6 @@
 using Common.Base;
 
-namespace BehaviorTree
+namespace Model
 {
     public class BlackBoard: Object
     {

+ 1 - 2
CSharp/Game/BehaviorTree/Node.cs → CSharp/Game/Model/BehaviorTree/Node.cs

@@ -1,7 +1,6 @@
 using System.Collections.Generic;
-using Model;
 
-namespace BehaviorTree
+namespace Model
 {
     public abstract class Node
     {

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

@@ -1,13 +1,13 @@
 using System;
 
-namespace BehaviorTree
+namespace Model
 {
     [AttributeUsage(AttributeTargets.Class)]
     public class NodeAttribute: Attribute
     {
-        public NodeType NodeType { get; private set; }
+        public int NodeType { get; private set; }
 
-        public NodeAttribute(NodeType nodeType)
+        public NodeAttribute(int nodeType)
         {
             this.NodeType = nodeType;
         }

+ 24 - 0
CSharp/Game/Model/Component/BehaviorTreeComponent.cs

@@ -0,0 +1,24 @@
+using System.Collections.Generic;
+using System.Reflection;
+using Common.Base;
+
+namespace Model
+{
+    public class BehaviorTreeComponent: Component<World>
+    {
+        private Dictionary<int, BehaviorTree> trees = new Dictionary<int, BehaviorTree>();
+
+        public void Load(Assembly assembly)
+        {
+            BehaviorTreeFactory behaviorTreeFactory = BehaviorTreeFactory.Instance;
+            behaviorTreeFactory.Load(assembly);
+
+            NodeConfig[] nodeConfigs = World.Instance.GetComponent<ConfigComponent>().GetAll<NodeConfig>();
+            foreach (NodeConfig nodeConfig in nodeConfigs)
+            {
+                BehaviorTree behaviorTree = behaviorTreeFactory.CreateTree(nodeConfig);
+                this.trees[nodeConfig.Id] = behaviorTree;
+            }
+        }
+    }
+}

+ 18 - 18
CSharp/Game/Model/BuffComponent.cs → CSharp/Game/Model/Component/BuffComponent.cs

@@ -9,17 +9,17 @@ namespace Model
     public class BuffComponent: Component<Unit>
     {
         [BsonElement]
-        private HashSet<Buff> buffs { get; set; }
+        private HashSet<Buff> buffs;
 
-        private Dictionary<ObjectId, Buff> buffIdDict { get; set; }
+        private Dictionary<ObjectId, Buff> idBuff;
 
-        private MultiMap<BuffType, Buff> buffTypeMMap { get; set; }
+        private MultiMap<BuffType, Buff> typeBuff;
 
         public BuffComponent()
         {
             this.buffs = new HashSet<Buff>();
-            this.buffIdDict = new Dictionary<ObjectId, Buff>();
-            this.buffTypeMMap = new MultiMap<BuffType, Buff>();
+            this.idBuff = new Dictionary<ObjectId, Buff>();
+            this.typeBuff = new MultiMap<BuffType, Buff>();
         }
 
         public override void BeginInit()
@@ -27,8 +27,8 @@ namespace Model
             base.BeginInit();
 
             this.buffs = new HashSet<Buff>();
-            this.buffIdDict = new Dictionary<ObjectId, Buff>();
-            this.buffTypeMMap = new MultiMap<BuffType, Buff>();
+            this.idBuff = new Dictionary<ObjectId, Buff>();
+            this.typeBuff = new MultiMap<BuffType, Buff>();
         }
 
         public override void EndInit()
@@ -37,8 +37,8 @@ namespace Model
 
             foreach (var buff in this.buffs)
             {
-                this.buffIdDict.Add(buff.Id, buff);
-                this.buffTypeMMap.Add(buff.Config.Type, buff);
+                this.idBuff.Add(buff.Id, buff);
+                this.typeBuff.Add(buff.Config.Type, buff);
             }
         }
 
@@ -49,34 +49,34 @@ namespace Model
                 throw new ArgumentException(string.Format("already exist same buff, Id: {0} ConfigId: {1}", buff.Id, buff.Config.Id));
             }
 
-            if (this.buffIdDict.ContainsKey(buff.Id))
+            if (this.idBuff.ContainsKey(buff.Id))
             {
                 throw new ArgumentException(string.Format("already exist same buff, Id: {0} ConfigId: {1}", buff.Id, buff.Config.Id));
             }
 
             this.buffs.Add(buff);
-            this.buffIdDict.Add(buff.Id, buff);
-            this.buffTypeMMap.Add(buff.Config.Type, buff);
+            this.idBuff.Add(buff.Id, buff);
+            this.typeBuff.Add(buff.Config.Type, buff);
         }
 
         public Buff GetById(ObjectId id)
         {
-            if (!this.buffIdDict.ContainsKey(id))
+            if (!this.idBuff.ContainsKey(id))
             {
                 return null;
             }
 
-            return this.buffIdDict[id];
+            return this.idBuff[id];
         }
 
         public Buff GetOneByType(BuffType type)
         {
-            return this.buffTypeMMap.GetOne(type);
+            return this.typeBuff.GetOne(type);
         }
 
         public Buff[] GetByType(BuffType type)
         {
-            return this.buffTypeMMap.GetByKey(type);
+            return this.typeBuff.GetByKey(type);
         }
 
         private bool Remove(Buff buff)
@@ -87,8 +87,8 @@ namespace Model
             }
 
             this.buffs.Remove(buff);
-            this.buffIdDict.Remove(buff.Id);
-            this.buffTypeMMap.Remove(buff.Config.Type, buff);
+            this.idBuff.Remove(buff.Id);
+            this.typeBuff.Remove(buff.Config.Type, buff);
 
             return true;
         }

+ 17 - 28
CSharp/Game/Model/ConfigComponent.cs → CSharp/Game/Model/Component/ConfigComponent.cs

@@ -10,42 +10,31 @@ namespace Model
     {
         public Dictionary<Type, ICategory> allConfig;
 
-        private Assembly[] assemblies;
-
-        public void Load(Assembly[] ass)
+        public void Load(Assembly assembly)
         {
             allConfig = new Dictionary<Type, ICategory>();
-            this.assemblies = ass;
-            foreach (Assembly assembly in assemblies)
+            Type[] types = assembly.GetTypes();
+            foreach (Type type in types)
             {
-                Type[] types = assembly.GetTypes();
-                foreach (Type type in types)
+                object[] attrs = type.GetCustomAttributes(typeof(ConfigAttribute), false);
+                if (attrs.Length == 0)
                 {
-                    object[] attrs = type.GetCustomAttributes(typeof(ConfigAttribute), false);
-                    if (attrs.Length == 0)
-                    {
-                        continue;
-                    }
-
-                    object obj = (Activator.CreateInstance(type));
+                    continue;
+                }
 
-                    ICategory iCategory = obj as ICategory;
-                    if (iCategory == null)
-                    {
-                        throw new Exception(
-                            string.Format("class: {0} not inherit from ACategory", type.Name));
-                    }
-                    iCategory.BeginInit();
-                    iCategory.EndInit();
+                object obj = (Activator.CreateInstance(type));
 
-                    allConfig[iCategory.ConfigType] = iCategory;
+                ICategory iCategory = obj as ICategory;
+                if (iCategory == null)
+                {
+                    throw new Exception(
+                        string.Format("class: {0} not inherit from ACategory", type.Name));
                 }
-            }
-        }
+                iCategory.BeginInit();
+                iCategory.EndInit();
 
-        public void Reload()
-        {
-            this.Load(this.assemblies);
+                allConfig[iCategory.ConfigType] = iCategory;
+            }
         }
 
         public T Get<T>(int id) where T : AConfig

+ 49 - 0
CSharp/Game/Model/Component/FactoryComponent.cs

@@ -0,0 +1,49 @@
+using System;
+using System.Collections.Generic;
+using System.Reflection;
+using Common.Base;
+
+namespace Model
+{
+    public class FactoryComponent<T>: Component<World> where T : Entity<T>
+    {
+        private Dictionary<int, IFactory<T>> allConfig;
+
+        public void Load(Assembly assembly)
+        {
+            allConfig = new Dictionary<int, IFactory<T>>();
+            Type[] types = assembly.GetTypes();
+            foreach (Type type in types)
+            {
+                object[] attrs = type.GetCustomAttributes(typeof(FactoryAttribute), false);
+                if (attrs.Length == 0)
+                {
+                    continue;
+                }
+
+                FactoryAttribute attribute = (FactoryAttribute)attrs[0];
+                if (attribute.ClassType != typeof (T))
+                {
+                    continue;
+                }
+
+                object obj = (Activator.CreateInstance(type));
+
+                IFactory<T> iFactory = obj as IFactory<T>;
+                if (iFactory == null)
+                {
+                    throw new Exception(
+                        string.Format("class: {0} not inherit from IFactory", type.Name));
+                }
+
+                allConfig[attribute.Type] = iFactory;
+            }
+        }
+
+        public T Create(int configId)
+        {
+            int type = (int) World.Instance.GetComponent<ConfigComponent>().Get<UnitConfig>(configId).Type;
+            return this.allConfig[type].Create(configId);
+        }
+    }
+}

+ 3 - 3
CSharp/Game/Model/UnitComponent.cs → CSharp/Game/Model/Component/UnitComponent.cs

@@ -11,8 +11,8 @@ namespace Model
         private readonly Dictionary<ObjectId, Unit> units =
                 new Dictionary<ObjectId, Unit>();
 
-        private readonly Dictionary<UnitType, Dictionary<ObjectId, Unit>> typeUnits =
-                new Dictionary<UnitType, Dictionary<ObjectId, Unit>>();
+        private readonly Dictionary<int, Dictionary<ObjectId, Unit>> typeUnits =
+                new Dictionary<int, Dictionary<ObjectId, Unit>>();
 
         public void Add(Unit unit)
         {
@@ -31,7 +31,7 @@ namespace Model
             return unit;
         }
 
-        public Unit[] GetOneType(UnitType type)
+        public Unit[] GetOneType(int type)
         {
             Dictionary<ObjectId, Unit> oneTypeUnits = null;
             if (!this.typeUnits.TryGetValue(type, out oneTypeUnits))

+ 1 - 1
CSharp/Game/Model/Config/UnitConfig.cs

@@ -4,7 +4,7 @@ namespace Model
 {
     public class UnitConfig: AConfig
     {
-        public UnitType Type { get; set; }
+        public int Type { get; set; }
     }
 
     [Config]

+ 0 - 52
CSharp/Game/Model/FactoryComponent.cs

@@ -1,52 +0,0 @@
-using System;
-using System.Collections.Generic;
-using System.Reflection;
-using Common.Base;
-
-namespace Model
-{
-    public class FactoryComponent<T>: Component<World> where T : Entity<T>
-    {
-        private Dictionary<int, IFactory<T>> allConfig;
-
-        public void Load(IEnumerable<Assembly> assemblies)
-        {
-            allConfig = new Dictionary<int, IFactory<T>>();
-            foreach (Assembly assembly in assemblies)
-            {
-                Type[] types = assembly.GetTypes();
-                foreach (Type type in types)
-                {
-                    object[] attrs = type.GetCustomAttributes(typeof(FactoryAttribute), false);
-                    if (attrs.Length == 0)
-                    {
-                        continue;
-                    }
-
-                    FactoryAttribute attribute = (FactoryAttribute)attrs[0];
-                    if (attribute.ClassType != typeof (T))
-                    {
-                        continue;
-                    }
-
-                    object obj = (Activator.CreateInstance(type));
-
-                    IFactory<T> iFactory = obj as IFactory<T>;
-                    if (iFactory == null)
-                    {
-                        throw new Exception(
-                            string.Format("class: {0} not inherit from IFactory", type.Name));
-                    }
-
-                    allConfig[attribute.Type] = iFactory;
-                }
-            }
-        }
-
-        public T Create(int configId)
-        {
-            int type = (int) World.Instance.GetComponent<ConfigComponent>().Get<UnitConfig>(configId).Type;
-            return this.allConfig[type].Create(configId);
-        }
-    }
-}

+ 10 - 6
CSharp/Game/Model/Model.csproj

@@ -45,19 +45,23 @@
     <Reference Include="System.Core" />
   </ItemGroup>
   <ItemGroup>
+    <Compile Include="Component\BehaviorTreeComponent.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" />
-    <Compile Include="BuffComponent.cs" />
-    <Compile Include="ConfigComponent.cs" />
+    <Compile Include="Component\BuffComponent.cs" />
+    <Compile Include="Component\ConfigComponent.cs" />
     <Compile Include="Config\UnitConfig.cs" />
     <Compile Include="Config\BuffConfig.cs" />
     <Compile Include="FactoryAttribute.cs" />
-    <Compile Include="FactoryComponent.cs" />
+    <Compile Include="Component\FactoryComponent.cs" />
     <Compile Include="IFactory.cs" />
-    <Compile Include="UnitFactory.cs" />
-    <Compile Include="UnitType.cs" />
+    <Compile Include="Component\UnitComponent.cs" />
     <Compile Include="BuffType.cs" />
     <Compile Include="GameObject.cs" />
-    <Compile Include="UnitComponent.cs" />
     <Compile Include="Config\GlobalConfig.cs" />
     <Compile Include="Config\NodeConfig.cs" />
     <Compile Include="Unit.cs" />

+ 0 - 21
CSharp/Game/Model/UnitFactory.cs

@@ -1,21 +0,0 @@
-namespace Model
-{
-    public static class UnitFactory
-    {
-        public static Unit Create(int configId)
-        {
-            return World.Instance.GetComponent<FactoryComponent<Unit>>().Create(configId);
-        }
-    }
-
-    [FactoryAttribute(typeof (Unit), (int) UnitType.Player)]
-    public class UnitPlayerFactory: IFactory<Unit>
-    {
-        public Unit Create(int configId)
-        {
-            Unit player = new Unit(configId);
-            player.AddComponent<BuffComponent>();
-            return player;
-        }
-    }
-}

+ 0 - 9
CSharp/Game/Model/UnitType.cs

@@ -1,9 +0,0 @@
-namespace Model
-{
-    public enum UnitType
-    {
-        Player = 1,
-        Npc = 2,
-        Dog = 3,
-    }
-}

+ 0 - 7
CSharp/Game/Model/World.cs

@@ -14,13 +14,6 @@
 
         private World()
         {
-            this.AddComponent<UnitComponent>();
-
-            ConfigComponent configComponent = this.AddComponent<ConfigComponent>();
-            configComponent.Load(new[] { typeof (World).Assembly });
-
-            FactoryComponent<Unit> factoryComponent = this.AddComponent<FactoryComponent<Unit>>();
-            factoryComponent.Load(new[] { typeof(World).Assembly });
         }
     }
 }

+ 21 - 1
CSharp/Game/MongoDBTest/MongoDBTest.cs

@@ -1,4 +1,6 @@
 using System;
+using System.IO;
+using System.Reflection;
 using Common.Helper;
 using Microsoft.VisualStudio.TestTools.UnitTesting;
 using Model;
@@ -19,7 +21,25 @@ namespace MongoDBTest
             var database = server.GetDatabase("test");
             var collection = database.GetCollection<Unit>("Unit");
 
-            Unit player1 = UnitFactory.Create(1);
+
+            World world = World.Instance;
+
+            // 加载配置
+            ConfigComponent configComponent = world.AddComponent<ConfigComponent>();
+            configComponent.Load(typeof(World).Assembly);
+
+            Assembly assembly = Assembly.Load(File.ReadAllBytes(@"./Controller.dll"));
+
+            // 构造工厂
+            FactoryComponent<Unit> factoryComponent = world.AddComponent<FactoryComponent<Unit>>();
+            factoryComponent.Load(assembly);
+
+            // 构造行为树
+            BehaviorTreeComponent behaviorTreeComponent = world.AddComponent<BehaviorTreeComponent>();
+            behaviorTreeComponent.Load(assembly);
+
+
+            Unit player1 = factoryComponent.Create(1);
             Buff buff = new Buff(1);
             player1.GetComponent<BuffComponent>().Add(buff);
             player1["hp"] = 10;

+ 52 - 0
CSharp/Server/Login/Login.csproj

@@ -0,0 +1,52 @@
+<?xml version="1.0" encoding="utf-8"?>
+<Project ToolsVersion="12.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
+  <Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
+  <PropertyGroup>
+    <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
+    <Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
+    <ProjectGuid>{2B3EC6BE-0920-4C93-934F-41DC76108818}</ProjectGuid>
+    <OutputType>Library</OutputType>
+    <AppDesignerFolder>Properties</AppDesignerFolder>
+    <RootNamespace>Login</RootNamespace>
+    <AssemblyName>Login</AssemblyName>
+    <TargetFrameworkVersion>v4.5</TargetFrameworkVersion>
+    <FileAlignment>512</FileAlignment>
+  </PropertyGroup>
+  <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
+    <DebugSymbols>true</DebugSymbols>
+    <DebugType>full</DebugType>
+    <Optimize>false</Optimize>
+    <OutputPath>bin\Debug\</OutputPath>
+    <DefineConstants>DEBUG;TRACE</DefineConstants>
+    <ErrorReport>prompt</ErrorReport>
+    <WarningLevel>4</WarningLevel>
+  </PropertyGroup>
+  <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
+    <DebugType>pdbonly</DebugType>
+    <Optimize>true</Optimize>
+    <OutputPath>bin\Release\</OutputPath>
+    <DefineConstants>TRACE</DefineConstants>
+    <ErrorReport>prompt</ErrorReport>
+    <WarningLevel>4</WarningLevel>
+  </PropertyGroup>
+  <ItemGroup>
+    <Reference Include="System" />
+    <Reference Include="System.Core" />
+    <Reference Include="System.Xml.Linq" />
+    <Reference Include="System.Data.DataSetExtensions" />
+    <Reference Include="Microsoft.CSharp" />
+    <Reference Include="System.Data" />
+    <Reference Include="System.Xml" />
+  </ItemGroup>
+  <ItemGroup>
+    <Compile Include="Properties\AssemblyInfo.cs" />
+  </ItemGroup>
+  <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
+  <!-- To modify your build process, add your task inside one of the targets below and uncomment it. 
+       Other similar extension points exist, see Microsoft.Common.targets.
+  <Target Name="BeforeBuild">
+  </Target>
+  <Target Name="AfterBuild">
+  </Target>
+  -->
+</Project>

+ 36 - 0
CSharp/Server/Login/Properties/AssemblyInfo.cs

@@ -0,0 +1,36 @@
+using System.Reflection;
+using System.Runtime.CompilerServices;
+using System.Runtime.InteropServices;
+
+// 有关程序集的常规信息通过以下
+// 特性集控制。更改这些特性值可修改
+// 与程序集关联的信息。
+[assembly: AssemblyTitle("Login")]
+[assembly: AssemblyDescription("")]
+[assembly: AssemblyConfiguration("")]
+[assembly: AssemblyCompany("Microsoft")]
+[assembly: AssemblyProduct("Login")]
+[assembly: AssemblyCopyright("Copyright © Microsoft 2014")]
+[assembly: AssemblyTrademark("")]
+[assembly: AssemblyCulture("")]
+
+// 将 ComVisible 设置为 false 使此程序集中的类型
+// 对 COM 组件不可见。  如果需要从 COM 访问此程序集中的类型,
+// 则将该类型上的 ComVisible 特性设置为 true。
+[assembly: ComVisible(false)]
+
+// 如果此项目向 COM 公开,则下列 GUID 用于类型库的 ID
+[assembly: Guid("bef9bfd1-d7ec-45dd-95b1-7b4397ab4812")]
+
+// 程序集的版本信息由下面四个值组成: 
+//
+//      主版本
+//      次版本 
+//      生成号
+//      修订号
+//
+// 可以指定所有这些值,也可以使用“生成号”和“修订号”的默认值,
+// 方法是按如下所示使用“*”: 
+// [assembly: AssemblyVersion("1.0.*")]
+[assembly: AssemblyVersion("1.0.0.0")]
+[assembly: AssemblyFileVersion("1.0.0.0")]