Jelajahi Sumber

整理代码

tanghai 11 tahun lalu
induk
melakukan
04e7c92c6f

+ 6 - 3
CSharp/App/Modules/BehaviorTreeModule/TreeLayout.cs

@@ -26,7 +26,8 @@
             {
                 TreeNodeViewModel child = this.treeViewModel.Get(treeNodeViewModel.Children[i]);
                 child.AncestorModify = treeNodeViewModel.Modify + treeNodeViewModel.AncestorModify;
-                TreeNodeViewModel offspring = this.LeftMostOffspring(child, currentLevel + 1, searchLevel);
+                TreeNodeViewModel offspring = this.LeftMostOffspring(child, currentLevel + 1,
+                        searchLevel);
                 if (offspring == null)
                 {
                     continue;
@@ -47,7 +48,8 @@
             {
                 TreeNodeViewModel child = this.treeViewModel.Get(treeNodeViewModel.Children[i]);
                 child.AncestorModify = treeNodeViewModel.Modify + treeNodeViewModel.AncestorModify;
-                TreeNodeViewModel offspring = this.RightMostOffspring(child, currentLevel + 1, searchLevel);
+                TreeNodeViewModel offspring = this.RightMostOffspring(child, currentLevel + 1,
+                        searchLevel);
                 if (offspring == null)
                 {
                     continue;
@@ -66,7 +68,8 @@
             right.AncestorModify = 0;
             for (int i = 0; tLeft != null && tRight != null; ++i)
             {
-                double tGap = (tRight.Prelim + tRight.AncestorModify) - (tLeft.Prelim + tLeft.AncestorModify);
+                double tGap = (tRight.Prelim + tRight.AncestorModify) -
+                              (tLeft.Prelim + tLeft.AncestorModify);
                 if (XGap + TreeNodeViewModel.Width - tGap > offset)
                 {
                     offset = XGap + TreeNodeViewModel.Width - tGap;

+ 14 - 5
CSharp/Game/BehaviorTree/BehaviorTreeFactory.cs

@@ -1,5 +1,6 @@
 using System;
 using System.Collections.Generic;
+using System.Reflection;
 
 namespace BehaviorTree
 {
@@ -20,7 +21,12 @@ namespace BehaviorTree
 
         private BehaviorTreeFactory()
         {
-            var assembly = typeof(BehaviorTreeFactory).Assembly;
+            Assembly assembly = typeof(BehaviorTreeFactory).Assembly;
+            this.RegisterNodes(assembly);
+        }
+
+        public void RegisterNodes(Assembly assembly)
+        {
             Type[] types = assembly.GetTypes();
             foreach (var type in types)
             {
@@ -31,16 +37,19 @@ namespace BehaviorTree
                 }
                 NodeAttribute attribute = (NodeAttribute)attrs[0];
 
-                dictionary.Add(attribute.NodeType, config => (Node)Activator.CreateInstance(attribute.ClassType, new object[] { config }));
-            }
+                Type classType = type;
+                this.dictionary.Add(attribute.NodeType,
+                        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));
+                throw new KeyNotFoundException(string.Format("CreateNode cannot found: {0}",
+                        config.Id));
             }
             return this.dictionary[(NodeType) config.Id](config);
         }

+ 3 - 5
CSharp/Game/BehaviorTree/NodeAttribute.cs

@@ -3,15 +3,13 @@
 namespace BehaviorTree
 {
     [AttributeUsage(AttributeTargets.Class)]
-    public class NodeAttribute : Attribute
+    public class NodeAttribute: Attribute
     {
         public NodeType NodeType { get; private set; }
-        public Type ClassType { get; private set; }
 
-        public NodeAttribute(NodeType nodeType, Type classType)
+        public NodeAttribute(NodeType nodeType)
         {
             this.NodeType = nodeType;
-            this.ClassType = classType;
         }
     }
-}
+}

+ 1 - 1
CSharp/Game/BehaviorTree/NodeConfig.cs

@@ -16,7 +16,7 @@ namespace BehaviorTree
     }
 
     [Config]
-    public class NodeCategory : ACategory<NodeConfig>
+    public class NodeCategory: ACategory<NodeConfig>
     {
     }
 }

+ 1 - 1
CSharp/Game/BehaviorTree/NodeType.cs

@@ -6,4 +6,4 @@
         Sequence = 2,
         Not = 10,
     }
-}
+}

+ 1 - 1
CSharp/Game/BehaviorTree/Not.cs

@@ -1,6 +1,6 @@
 namespace BehaviorTree
 {
-    [NodeAttribute(NodeType.Not, typeof(Not))]
+    [Node(NodeType.Not)]
     public class Not: Node
     {
         public Not(NodeConfig config): base(config)

+ 1 - 1
CSharp/Game/BehaviorTree/Selector.cs

@@ -1,6 +1,6 @@
 namespace BehaviorTree
 {
-    [NodeAttribute(NodeType.Selector, typeof(Selector))]
+    [Node(NodeType.Selector)]
     public class Selector: Node
     {
         public Selector(NodeConfig config): base(config)

+ 1 - 1
CSharp/Game/BehaviorTree/Sequence.cs

@@ -1,6 +1,6 @@
 namespace BehaviorTree
 {
-    [NodeAttribute(NodeType.Sequence, typeof(Sequence))]
+    [Node(NodeType.Sequence)]
     internal class Sequence: Node
     {
         public Sequence(NodeConfig config): base(config)

+ 2 - 8
CSharp/Game/Controller/BuffController.cs

@@ -1,10 +1,4 @@
-using System;
-using System.Collections.Generic;
-using System.Linq;
-using System.Text;
-using System.Threading.Tasks;
-
-namespace Controller
+namespace Controller
 {
     /// <summary>
     /// 控制复杂的buff逻辑,可以reload
@@ -12,4 +6,4 @@ namespace Controller
     public class BuffController
     {
     }
-}
+}

+ 5 - 2
CSharp/Game/Controller/Properties/AssemblyInfo.cs

@@ -1,10 +1,10 @@
 using System.Reflection;
-using System.Runtime.CompilerServices;
 using System.Runtime.InteropServices;
 
 // 有关程序集的常规信息通过以下
 // 特性集控制。更改这些特性值可修改
 // 与程序集关联的信息。
+
 [assembly: AssemblyTitle("Controller")]
 [assembly: AssemblyDescription("")]
 [assembly: AssemblyConfiguration("")]
@@ -17,9 +17,11 @@ using System.Runtime.InteropServices;
 // 将 ComVisible 设置为 false 使此程序集中的类型
 // 对 COM 组件不可见。  如果需要从 COM 访问此程序集中的类型,
 // 则将该类型上的 ComVisible 特性设置为 true。
+
 [assembly: ComVisible(false)]
 
 // 如果此项目向 COM 公开,则下列 GUID 用于类型库的 ID
+
 [assembly: Guid("2857e4a1-f2c1-4c17-8b67-6d25f98265ed")]
 
 // 程序集的版本信息由下面四个值组成: 
@@ -32,5 +34,6 @@ using System.Runtime.InteropServices;
 // 可以指定所有这些值,也可以使用“生成号”和“修订号”的默认值,
 // 方法是按如下所示使用“*”: 
 // [assembly: AssemblyVersion("1.0.*")]
+
 [assembly: AssemblyVersion("1.0.0.0")]
-[assembly: AssemblyFileVersion("1.0.0.0")]
+[assembly: AssemblyFileVersion("1.0.0.0")]

+ 14 - 17
CSharp/Game/Model/BuffManager.cs

@@ -1,11 +1,13 @@
 using System.Collections.Generic;
 using System.ComponentModel;
+using Common.Base;
 using MongoDB.Bson;
 using MongoDB.Bson.Serialization.Attributes;
+using Component = Common.Base.Component;
 
 namespace Model
 {
-    public class BuffManager: ISupportInitialize
+    public class BuffManager: Component, ISupportInitialize
     {
         public HashSet<Buff> Buffs { get; private set; }
 
@@ -13,13 +15,13 @@ namespace Model
         public Dictionary<ObjectId, Buff> BuffGuidDict { get; private set; }
 
         [BsonIgnore]
-        public Dictionary<int, Buff> BuffTypeDict { get; private set; }
+        public MultiMap<int, Buff> BuffTypeDict { get; private set; }
 
         public BuffManager()
         {
             this.Buffs = new HashSet<Buff>();
             this.BuffGuidDict = new Dictionary<ObjectId, Buff>();
-            this.BuffTypeDict = new Dictionary<int, Buff>();
+            this.BuffTypeDict = new MultiMap<int, Buff>();
         }
 
         void ISupportInitialize.BeginInit()
@@ -51,7 +53,7 @@ namespace Model
                 return false;
             }
 
-            if (this.BuffTypeDict.ContainsKey(buff.Type))
+            if (this.BuffTypeDict.Get(buff.Type) != null)
             {
                 return false;
             }
@@ -63,24 +65,19 @@ namespace Model
             return true;
         }
 
-        public Buff GetById(ObjectId id)
+        public Buff GetByGuid(ObjectId guid)
         {
-            if (!this.BuffGuidDict.ContainsKey(id))
+            if (!this.BuffGuidDict.ContainsKey(guid))
             {
                 return null;
             }
 
-            return this.BuffGuidDict[id];
+            return this.BuffGuidDict[guid];
         }
 
         public Buff GetByType(int type)
         {
-            if (!this.BuffTypeDict.ContainsKey(type))
-            {
-                return null;
-            }
-
-            return this.BuffTypeDict[type];
+            return this.BuffTypeDict.Get(type);
         }
 
         private bool Remove(Buff buff)
@@ -92,20 +89,20 @@ namespace Model
 
             this.Buffs.Remove(buff);
             this.BuffGuidDict.Remove(buff.Guid);
-            this.BuffTypeDict.Remove(buff.Type);
+            this.BuffTypeDict.Remove(buff.Type, buff);
 
             return true;
         }
 
-        public bool RemoveById(ObjectId id)
+        public bool RemoveByGuid(ObjectId guid)
         {
-            var buff = this.GetById(id);
+            Buff buff = this.GetByGuid(guid);
             return this.Remove(buff);
         }
 
         public bool RemoveByType(int type)
         {
-            var buff = this.GetByType(type);
+            Buff buff = this.GetByType(type);
             return this.Remove(buff);
         }
     }

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

@@ -1,9 +0,0 @@
-namespace Model
-{
-    /// <summary>
-    /// 角色对象
-    /// </summary>
-    public class Character: GameObject
-    {
-    }
-}

+ 1 - 11
CSharp/Game/Model/GameObject.cs

@@ -2,18 +2,8 @@
 
 namespace Model
 {
-    public class GameObject: Object
+    public class GameObject: Entity
     {
-        private readonly BuffManager buffManager = new BuffManager();
-
         public int Type { get; set; }
-
-        public BuffManager BuffManager
-        {
-            get
-            {
-                return this.buffManager;
-            }
-        }
     }
 }

+ 0 - 1
CSharp/Game/Model/Model.csproj

@@ -47,7 +47,6 @@
   <ItemGroup>
     <Compile Include="Buff.cs" />
     <Compile Include="BuffManager.cs" />
-    <Compile Include="Character.cs" />
     <Compile Include="GameObject.cs" />
     <Compile Include="GameObjectManager.cs" />
   </ItemGroup>

+ 1 - 1
CSharp/Game/World/World.cs

@@ -21,7 +21,7 @@ namespace World
 
         private World()
         {
-            configManager.Load(typeof(World).Assembly);
+            this.configManager.Load(typeof (World).Assembly);
         }
 
         public ConfigManager ConfigManager

+ 1 - 1
CSharp/Platform/Common/Base/Component.cs

@@ -4,4 +4,4 @@
     {
         public Entity Owner { get; set; }
     }
-}
+}

+ 6 - 16
CSharp/Platform/Common/Base/Entity.cs

@@ -1,38 +1,28 @@
-using System;
-using System.Collections.Generic;
+using System.Collections.Generic;
 using System.Linq;
 
 namespace Common.Base
 {
     public class Entity: Object
     {
-        private readonly Dictionary<string, Component> components = new Dictionary<string, Component>();
+        private readonly Dictionary<string, Component> components =
+                new Dictionary<string, Component>();
 
         public void AddComponent<T>() where T : Component, new()
         {
             T t = new T { Owner = this };
-            this.components.Add(typeof(T).Name, t);
+            this.components.Add(typeof (T).Name, t);
         }
 
         public void RemoveComponent<T>() where T : Component
         {
-            this.components.Remove(typeof(T).Name);
+            this.components.Remove(typeof (T).Name);
         }
 
         public T GetComponent<T>() where T : Component
         {
             Component t;
             if (!this.components.TryGetValue(typeof (T).Name, out t))
-            {
-                throw new Exception(string.Format("not found component: {0}", typeof(T).Name));
-            }
-            return (T) t;
-        }
-
-        public T TryGetComponent<T>() where T : Component
-        {
-            Component t;
-            if (!this.components.TryGetValue(typeof(T).Name, out t))
             {
                 return null;
             }
@@ -44,4 +34,4 @@ namespace Common.Base
             return this.components.Values.ToArray();
         }
     }
-}
+}

+ 112 - 0
CSharp/Platform/Common/Base/MultiMap.cs

@@ -0,0 +1,112 @@
+using System.Collections.Generic;
+
+namespace Common.Base
+{
+    public class MultiMap<T, K>
+    {
+        private readonly SortedDictionary<T, List<K>> dictionary =
+                new SortedDictionary<T, List<K>>();
+
+        public SortedDictionary<T, List<K>>.KeyCollection Keys
+        {
+            get
+            {
+                return this.dictionary.Keys;
+            }
+        }
+
+        public void Add(T t, K k)
+        {
+            List<K> list;
+            this.dictionary.TryGetValue(t, out list);
+            if (list == null)
+            {
+                list = new List<K>();
+            }
+            list.Add(k);
+            this.dictionary[t] = list;
+        }
+
+        public bool Remove(T t, K k)
+        {
+            List<K> list;
+            this.dictionary.TryGetValue(t, out list);
+            if (list == null)
+            {
+                return false;
+            }
+            if (!list.Remove(k))
+            {
+                return false;
+            }
+            if (list.Count == 0)
+            {
+                this.dictionary.Remove(t);
+            }
+            return true;
+        }
+
+        public bool Remove(T t)
+        {
+            return this.dictionary.Remove(t);
+        }
+
+        /// <summary>
+        /// 不返回内部的list,copy一份出来
+        /// </summary>
+        /// <param name="t"></param>
+        /// <returns></returns>
+        public List<K> GetAll(T t)
+        {
+            List<K> list;
+            this.dictionary.TryGetValue(t, out list);
+            if (list == null)
+            {
+                return new List<K>();
+            }
+            var newList = new List<K>();
+            foreach (K k in list)
+            {
+                newList.Add(k);
+            }
+            return newList;
+        }
+
+        /// <summary>
+        /// 返回内部的list
+        /// </summary>
+        /// <param name="t"></param>
+        /// <returns></returns>
+        public List<K> this[T t]
+        {
+            get
+            {
+                List<K> list;
+                this.dictionary.TryGetValue(t, out list);
+                return list;
+            }
+        }
+
+        public K Get(T t)
+        {
+            List<K> list;
+            this.dictionary.TryGetValue(t, out list);
+            if ((list != null) && (list.Count > 0))
+            {
+                return list[0];
+            }
+            return default(K);
+        }
+
+        public bool Contains(T t, K k)
+        {
+            List<K> list;
+            this.dictionary.TryGetValue(t, out list);
+            if (list == null)
+            {
+                return false;
+            }
+            return list.Contains(k);
+        }
+    }
+}

+ 1 - 0
CSharp/Platform/Common/Common.csproj

@@ -60,6 +60,7 @@
   <ItemGroup>
     <Compile Include="Base\Entity.cs" />
     <Compile Include="Base\Component.cs" />
+    <Compile Include="Base\MultiMap.cs" />
     <Compile Include="Config\ICategory.cs" />
     <Compile Include="Event\EventTrigger.cs" />
     <Compile Include="Event\Env.cs" />

+ 5 - 4
CSharp/Platform/Common/Config/ConfigManager.cs

@@ -19,13 +19,14 @@ namespace Common.Config
                 {
                     continue;
                 }
-                
+
                 object obj = (Activator.CreateInstance(type));
 
                 ICategory iCategory = obj as ICategory;
                 if (iCategory == null)
                 {
-                    throw new Exception(string.Format("class: {0} not inherit from ACategory", type.Name));
+                    throw new Exception(string.Format("class: {0} not inherit from ACategory",
+                            type.Name));
                 }
                 iCategory.BeginInit();
                 iCategory.EndInit();
@@ -37,13 +38,13 @@ namespace Common.Config
 
         public T Get<T>(int type) where T : IConfig
         {
-            var configCategory = (ACategory<T>)this.allConfig[typeof(T).Name];
+            var configCategory = (ACategory<T>) this.allConfig[typeof (T).Name];
             return configCategory[type];
         }
 
         public T[] GetAll<T>() where T : IConfig
         {
-            var configCategory = (ACategory<T>)this.allConfig[typeof(T).Name];
+            var configCategory = (ACategory<T>) this.allConfig[typeof (T).Name];
             return configCategory.GetAll();
         }
 

+ 2 - 2
CSharp/Platform/Common/Config/ICategory.cs

@@ -2,8 +2,8 @@
 
 namespace Common.Config
 {
-    public interface ICategory : ISupportInitialize
+    public interface ICategory: ISupportInitialize
     {
         string Name { get; }
     }
-}
+}

+ 3 - 2
CSharp/Platform/Common/Event/EventTrigger.cs

@@ -24,7 +24,8 @@ namespace Common.Event
                 IEvent iEvent = obj as IEvent;
                 if (iEvent == null)
                 {
-                    throw new Exception(string.Format("event not inherit IEvent interface: {0}", obj.GetType().FullName));
+                    throw new Exception(string.Format("event not inherit IEvent interface: {0}",
+                            obj.GetType().FullName));
                 }
 
                 IEventAttribute iEventAttribute = (T) attrs[0];
@@ -51,4 +52,4 @@ namespace Common.Event
             }
         }
     }
-}
+}