Просмотр исходного кода

Component和Entity之间增加泛型约束,例如:
BuffComponent继承于Component<Unit>,所以BuffComponent只能挂在Unit上,不能挂在World上

tanghai 11 лет назад
Родитель
Сommit
d2208d6cad

+ 2 - 3
CSharp/Game/Model/Buff.cs

@@ -1,9 +1,8 @@
-using Common.Config;
-using MongoDB.Bson.Serialization.Attributes;
+using MongoDB.Bson.Serialization.Attributes;
 
 namespace Model
 {
-    public class Buff: GameObject
+    public class Buff: GameObject<Buff>
     {
         [BsonElement]
         private int configId { get; set; }

+ 10 - 2
CSharp/Game/Model/BuffComponent.cs

@@ -3,11 +3,10 @@ using System.Collections.Generic;
 using Common.Base;
 using MongoDB.Bson;
 using MongoDB.Bson.Serialization.Attributes;
-using Component = Common.Base.Component;
 
 namespace Model
 {
-    public class BuffComponent: Component
+    public class BuffComponent: Component<Unit>
     {
         [BsonElement]
         private HashSet<Buff> buffs { get; set; }
@@ -23,6 +22,15 @@ namespace Model
             this.buffTypeMMap = new MultiMap<BuffType, Buff>();
         }
 
+        public override void BeginInit()
+        {
+            base.BeginInit();
+
+            this.buffs = new HashSet<Buff>();
+            this.buffIdDict = new Dictionary<ObjectId, Buff>();
+            this.buffTypeMMap = new MultiMap<BuffType, Buff>();
+        }
+
         public override void EndInit()
         {
             base.EndInit();

+ 3 - 3
CSharp/Platform/Common/Config/ConfigComponent.cs → CSharp/Game/Model/ConfigComponent.cs

@@ -2,11 +2,11 @@
 using System.Collections.Generic;
 using System.Reflection;
 using Common.Base;
-using Common.Logger;
+using Common.Config;
 
-namespace Common.Config
+namespace Model
 {
-    public class ConfigComponent: Component
+    public class ConfigComponent: Component<World>
     {
         public Dictionary<Type, ICategory> allConfig;
 

+ 1 - 1
CSharp/Platform/Common/Factory/FactoryAttribute.cs → CSharp/Game/Model/FactoryAttribute.cs

@@ -1,6 +1,6 @@
 using System;
 
-namespace Common.Factory
+namespace Model
 {
     [AttributeUsage(AttributeTargets.Class)]
     public class FactoryAttribute : Attribute

+ 14 - 13
CSharp/Platform/Common/Factory/FactoryComponent.cs → CSharp/Game/Model/FactoryComponent.cs

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

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

@@ -2,7 +2,7 @@
 
 namespace Model
 {
-    public abstract class GameObject: Entity
+    public abstract class GameObject<K>: Entity<K> where K : Entity<K>
     {
     }
 }

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

@@ -0,0 +1,9 @@
+using Common.Base;
+
+namespace Model
+{
+    public interface IFactory<out T> where T : Entity<T>
+    {
+        T Create(int configId);
+    }
+}

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

@@ -47,8 +47,12 @@
   <ItemGroup>
     <Compile Include="Buff.cs" />
     <Compile Include="BuffComponent.cs" />
+    <Compile Include="ConfigComponent.cs" />
     <Compile Include="Config\UnitConfig.cs" />
     <Compile Include="Config\BuffConfig.cs" />
+    <Compile Include="FactoryAttribute.cs" />
+    <Compile Include="FactoryComponent.cs" />
+    <Compile Include="IFactory.cs" />
     <Compile Include="UnitFactory.cs" />
     <Compile Include="UnitType.cs" />
     <Compile Include="BuffType.cs" />

+ 2 - 3
CSharp/Game/Model/Unit.cs

@@ -1,9 +1,8 @@
-using Common.Config;
-using MongoDB.Bson.Serialization.Attributes;
+using MongoDB.Bson.Serialization.Attributes;
 
 namespace Model
 {
-    public class Unit : GameObject
+    public class Unit : GameObject<Unit>
     {
         [BsonElement]
         public int configId { get; set; }

+ 1 - 1
CSharp/Game/Model/UnitComponent.cs

@@ -6,7 +6,7 @@ using MongoDB.Bson;
 
 namespace Model
 {
-    public class UnitComponent: Component
+    public class UnitComponent: Component<World>
     {
         private readonly Dictionary<ObjectId, Unit> units =
                 new Dictionary<ObjectId, Unit>();

+ 5 - 8
CSharp/Game/Model/UnitFactory.cs

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

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

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

+ 3 - 6
CSharp/Game/Model/World.cs

@@ -1,9 +1,6 @@
-using Common.Config;
-using Common.Factory;
-
-namespace Model
+namespace Model
 {
-    public class World : GameObject
+    public class World : GameObject<World>
     {
         private static readonly World instance = new World();
 
@@ -22,7 +19,7 @@ namespace Model
             ConfigComponent configComponent = this.AddComponent<ConfigComponent>();
             configComponent.Load(new[] { typeof (World).Assembly });
 
-            FactoryComponent factoryComponent = this.AddComponent<FactoryComponent>();
+            FactoryComponent<Unit> factoryComponent = this.AddComponent<FactoryComponent<Unit>>();
             factoryComponent.Load(new[] { typeof(World).Assembly });
         }
     }

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

@@ -19,7 +19,7 @@ namespace MongoDBTest
             var database = server.GetDatabase("test");
             var collection = database.GetCollection<Unit>("Unit");
 
-            Unit player1 = UnitFactory.Create(UnitType.Player, 1);
+            Unit player1 = UnitFactory.Create(1);
             Buff buff = new Buff(1);
             player1.GetComponent<BuffComponent>().Add(buff);
             player1["hp"] = 10;

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

@@ -5,12 +5,12 @@ namespace Common.Base
     /// <summary>
     /// Component的Id与Owner Entity Id一样
     /// </summary>
-    public abstract class Component : Object
+    public abstract class Component<T>: Object where T: Entity<T>
     {
-        private Entity owner;
+        private T owner;
 
         [BsonIgnore]
-        public Entity Owner
+        public T Owner
         {
             get
             {

+ 15 - 15
CSharp/Platform/Common/Base/Entity.cs

@@ -5,15 +5,15 @@ using MongoDB.Bson.Serialization.Attributes;
 
 namespace Common.Base
 {
-    public abstract class Entity : Object
+    public abstract class Entity<K> : Object where K : Entity<K>
     {
         [BsonElement] 
         [BsonIgnoreIfNull]
-        private HashSet<Component> components;
+        private HashSet<Component<K>> components;
 
-        private Dictionary<Type, Component> componentDict = new Dictionary<Type, Component>();
+        private Dictionary<Type, Component<K>> componentDict = new Dictionary<Type, Component<K>>();
 
-        public T AddComponent<T>() where T : Component, new()
+        public T AddComponent<T>() where T : Component<K>, new()
         {
             if (this.componentDict.ContainsKey(typeof (T)))
             {
@@ -24,18 +24,18 @@ namespace Common.Base
 
             if (this.components == null)
             {
-                this.components = new HashSet<Component>();
+                this.components = new HashSet<Component<K>>();
             }
 
-            T t = new T { Owner = this };
+            T t = new T { Owner = (K)this };
             this.components.Add(t);
             this.componentDict.Add(typeof (T), t);
             return t;
         }
 
-        public void RemoveComponent<T>() where T : Component
+        public void RemoveComponent<T>() where T : Component<K>
         {
-            Component t;
+            Component<K> t;
             if (!this.componentDict.TryGetValue(typeof (T), out t))
             {
                 throw new Exception(
@@ -52,9 +52,9 @@ namespace Common.Base
             }
         }
 
-        public T GetComponent<T>() where T : Component
+        public T GetComponent<T>() where T : Component<K>
         {
-            Component t;
+            Component<K> t;
             if (!this.componentDict.TryGetValue(typeof (T), out t))
             {
                 throw new Exception(
@@ -64,7 +64,7 @@ namespace Common.Base
             return (T) t;
         }
 
-        public Component[] GetComponents()
+        public Component<K>[] GetComponents()
         {
             return this.components.ToArray();
         }
@@ -72,8 +72,8 @@ namespace Common.Base
         public override void BeginInit()
         {
             base.BeginInit();
-            this.components = new HashSet<Component>();
-            this.componentDict = new Dictionary<Type, Component>();
+            this.components = new HashSet<Component<K>>();
+            this.componentDict = new Dictionary<Type, Component<K>>();
         }
 
         public override void EndInit()
@@ -84,9 +84,9 @@ namespace Common.Base
                 this.components = null;
                 return;
             }
-            foreach (Component component in this.components)
+            foreach (var component in this.components)
             {
-                component.Owner = this;
+                component.Owner = (K)this;
                 this.componentDict.Add(component.GetType(), component);
             }
         }

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

@@ -69,11 +69,7 @@
     <Compile Include="Base\Object.cs" />
     <Compile Include="Config\ConfigAttribute.cs" />
     <Compile Include="Config\ACategory.cs" />
-    <Compile Include="Config\ConfigComponent.cs" />
     <Compile Include="Config\AConfig.cs" />
-    <Compile Include="Factory\FactoryComponent.cs" />
-    <Compile Include="Factory\IFactory.cs" />
-    <Compile Include="Factory\FactoryAttribute.cs" />
     <Compile Include="Helper\BigIntegerHelper.cs" />
     <Compile Include="Helper\ByteHelper.cs" />
     <Compile Include="Helper\EnumHelper.cs" />

+ 0 - 9
CSharp/Platform/Common/Factory/IFactory.cs

@@ -1,9 +0,0 @@
-using Common.Base;
-
-namespace Common.Factory
-{
-    public interface IFactory
-    {
-        Entity Create(int configId);
-    }
-}